summaryrefslogtreecommitdiff
path: root/webob/compat.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-09-15 22:24:40 -0400
committerChris McDonough <chrism@plope.com>2011-09-15 22:24:40 -0400
commit37bd9b5b167684ce70a2ee4fe4340f0ef80717d8 (patch)
tree0f9d276117b72ce713edbba4ded10db49e268bd0 /webob/compat.py
parent157647c4580583ba7065567bb876303362d60387 (diff)
downloadwebob-37bd9b5b167684ce70a2ee4fe4340f0ef80717d8.tar.gz
work towards creating and using 'compat' module and removing syntax errors when code is run under py3k; all tests pass under py2, but most tests still fail under py3k
Diffstat (limited to 'webob/compat.py')
-rw-r--r--webob/compat.py145
1 files changed, 145 insertions, 0 deletions
diff --git a/webob/compat.py b/webob/compat.py
new file mode 100644
index 0000000..14f3910
--- /dev/null
+++ b/webob/compat.py
@@ -0,0 +1,145 @@
+# code stolen from "six"
+
+import sys
+import types
+
+# True if we are running on Python 3.
+PY3 = sys.version_info[0] == 3
+
+if PY3:
+ string_types = str,
+ integer_types = int,
+ class_types = type,
+ text_type = str
+ binary_type = bytes
+else:
+ string_types = basestring,
+ integer_types = (int, long)
+ class_types = (type, types.ClassType)
+ text_type = unicode
+ binary_type = str
+
+if PY3: # pragma: no cover
+ from urllib import parse
+ urlparse = parse
+ long = int
+else:
+ import urlparse
+
+if PY3: # pragma: no cover
+ from collections import MutableMapping as DictMixin
+else:
+ from UserDict import DictMixin
+
+if PY3: # pragma: no cover
+ from urllib.parse import quote as url_quote
+ from urllib.parse import unquote as url_unquote
+ from urllib.parse import urlencode as url_encode
+ from urllib.request import urlopen as url_open
+else:
+ from urllib import quote as url_quote
+ from urllib import unquote as url_unquote
+ from urllib import urlencode as url_encode
+ from urllib2 import urlopen as url_open
+
+
+
+def _add_doc(func, doc):
+ """Add documentation to a function."""
+ func.__doc__ = doc
+
+if PY3: # pragma: no cover
+ def b(s):
+ return s.encode("latin-1")
+ def u(s):
+ return s
+ import io
+ StringIO = io.StringIO
+ BytesIO = io.BytesIO
+else:
+ def b(s):
+ return s
+ def u(s):
+ return unicode(s, "unicode_escape")
+ import StringIO
+ StringIO = BytesIO = StringIO.StringIO
+_add_doc(b, """Byte literal""")
+_add_doc(u, """Text literal""")
+
+if PY3:
+ import builtins
+ exec_ = getattr(builtins, "exec")
+
+
+ def reraise(tp, value, tb=None):
+ if value.__traceback__ is not tb:
+ raise value.with_traceback(tb)
+ raise value
+
+
+ print_ = getattr(builtins, "print")
+ del builtins
+
+else:
+ def exec_(code, globs=None, locs=None):
+ """Execute code in a namespace."""
+ if globs is None:
+ frame = sys._getframe(1)
+ globs = frame.f_globals
+ if locs is None:
+ locs = frame.f_locals
+ del frame
+ elif locs is None:
+ locs = globs
+ exec("""exec code in globs, locs""")
+
+
+ exec_("""def reraise(tp, value, tb=None):
+ raise tp, value, tb
+""")
+
+
+ def print_(*args, **kwargs):
+ """The new-style print function."""
+ fp = kwargs.pop("file", sys.stdout)
+ if fp is None:
+ return
+ def write(data):
+ if not isinstance(data, basestring):
+ data = str(data)
+ fp.write(data)
+ want_unicode = False
+ sep = kwargs.pop("sep", None)
+ if sep is not None:
+ if isinstance(sep, unicode):
+ want_unicode = True
+ elif not isinstance(sep, str):
+ raise TypeError("sep must be None or a string")
+ end = kwargs.pop("end", None)
+ if end is not None:
+ if isinstance(end, unicode):
+ want_unicode = True
+ elif not isinstance(end, str):
+ raise TypeError("end must be None or a string")
+ if kwargs:
+ raise TypeError("invalid keyword arguments to print()")
+ if not want_unicode:
+ for arg in args:
+ if isinstance(arg, unicode):
+ want_unicode = True
+ break
+ if want_unicode:
+ newline = unicode("\n")
+ space = unicode(" ")
+ else:
+ newline = "\n"
+ space = " "
+ if sep is None:
+ sep = space
+ if end is None:
+ end = newline
+ for i, arg in enumerate(args):
+ if i:
+ write(sep)
+ write(arg)
+ write(end)