summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-06-07 18:36:26 +0200
committerClaude Paroz <claude@2xlibre.net>2012-06-07 18:36:53 +0200
commit5e6ded2e58597fa324c550bad35a30ee630ce223 (patch)
treef3ca5fe9c7a811d0757b8849476f090f08a6359e /django
parent87ff89d12d109066ff355ed267787d32a6b5aeb7 (diff)
Fixed #18363 -- Added Python 3 compatibility layer.
Thanks Vinay Sajip for the support of his django3 branch and Alex Gaynor, kezabelle, YorikSar for the review.
Diffstat (limited to 'django')
-rw-r--r--django/utils/py3.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/django/utils/py3.py b/django/utils/py3.py
new file mode 100644
index 0000000000..8a5c37e976
--- /dev/null
+++ b/django/utils/py3.py
@@ -0,0 +1,109 @@
+# Compatibility layer for running Django both in 2.x and 3.x
+
+import sys
+
+if sys.version_info[0] < 3:
+ PY3 = False
+ # Changed module locations
+ from urlparse import (urlparse, urlunparse, urljoin, urlsplit, urlunsplit,
+ urldefrag, parse_qsl)
+ from urllib import (quote, unquote, quote_plus, urlopen, urlencode,
+ url2pathname, urlretrieve, unquote_plus)
+ from urllib2 import (Request, OpenerDirector, UnknownHandler, HTTPHandler,
+ HTTPSHandler, HTTPDefaultErrorHandler, FTPHandler,
+ HTTPError, HTTPErrorProcessor)
+ import urllib2
+ import Cookie as cookies
+ try:
+ import cPickle as pickle
+ except ImportError:
+ import pickle
+ try:
+ import thread
+ except ImportError:
+ import dummy_thread as thread
+ from htmlentitydefs import name2codepoint
+ import HTMLParser
+ from os import getcwdu
+ from itertools import izip as zip
+ unichr = unichr
+ xrange = xrange
+ maxsize = sys.maxint
+
+ # Type aliases
+ string_types = basestring,
+ text_type = unicode
+ integer_types = int, long
+ long_type = long
+
+ from io import BytesIO as OutputIO
+
+ # Glue code for syntax differences
+ def reraise(tp, value, tb=None):
+ exec("raise tp, value, tb")
+
+ def with_metaclass(meta, base=object):
+ class _DjangoBase(base):
+ __metaclass__ = meta
+ return _DjangoBase
+
+ iteritems = lambda o: o.iteritems()
+ itervalues = lambda o: o.itervalues()
+ iterkeys = lambda o: o.iterkeys()
+
+ # n() is useful when python3 needs a str (unicode), and python2 str (bytes)
+ n = lambda s: s.encode('utf-8')
+
+else:
+ PY3 = True
+ import builtins
+
+ # Changed module locations
+ from urllib.parse import (urlparse, urlunparse, urlencode, urljoin,
+ urlsplit, urlunsplit, quote, unquote,
+ quote_plus, unquote_plus, parse_qsl,
+ urldefrag)
+ from urllib.request import (urlopen, url2pathname, Request, OpenerDirector,
+ UnknownHandler, HTTPHandler, HTTPSHandler,
+ HTTPDefaultErrorHandler, FTPHandler,
+ HTTPError, HTTPErrorProcessor, urlretrieve)
+ import urllib.request as urllib2
+ import http.cookies as cookies
+ import pickle
+ try:
+ import _thread as thread
+ except ImportError:
+ import _dummy_thread as thread
+ from html.entities import name2codepoint
+ import html.parser as HTMLParser
+ from os import getcwd as getcwdu
+ zip = zip
+ unichr = chr
+ xrange = range
+ maxsize = sys.maxsize
+
+ # Type aliases
+ string_types = str,
+ text_type = str
+ integer_types = int,
+ long_type = int
+
+ from io import StringIO as OutputIO
+
+ # Glue code for syntax differences
+ def reraise(tp, value, tb=None):
+ if value.__traceback__ is not tb:
+ raise value.with_traceback(tb)
+ raise value
+
+ def with_metaclass(meta, base=object):
+ ns = dict(base=base, meta=meta)
+ exec("""class _DjangoBase(base, metaclass=meta):
+ pass""", ns)
+ return ns["_DjangoBase"]
+
+ iteritems = lambda o: o.items()
+ itervalues = lambda o: o.values()
+ iterkeys = lambda o: o.keys()
+
+ n = lambda s: s