summaryrefslogtreecommitdiff
path: root/waitress/compat.py
diff options
context:
space:
mode:
authorAndrew Shadura <andrew@shadura.me>2013-10-02 20:23:41 +0200
committerAndrew Shadura <andrew@shadura.me>2013-10-02 20:23:41 +0200
commit744dd21c8cb5ca0a5626592cec67644f026bc6b9 (patch)
treeaf78dd3cc7cc7a0f9341d6e7f720542905e0d6ed /waitress/compat.py
parent154cfcd6bde9d2acec5892a713bf9f31fb547db8 (diff)
mercurial-importorig imported waitress version 0.8.1
--HG-- branch : upstream
Diffstat (limited to 'waitress/compat.py')
-rw-r--r--waitress/compat.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/waitress/compat.py b/waitress/compat.py
new file mode 100644
index 0000000..39327a5
--- /dev/null
+++ b/waitress/compat.py
@@ -0,0 +1,114 @@
+import sys
+import types
+
+try:
+ import urlparse
+except ImportError: # pragma: no cover
+ from urllib import parse as urlparse
+
+# True if we are running on Python 3.
+PY3 = sys.version_info[0] == 3
+
+if PY3: # pragma: no cover
+ string_types = str,
+ integer_types = int,
+ class_types = type,
+ text_type = str
+ binary_type = bytes
+ long = int
+else:
+ string_types = basestring,
+ integer_types = (int, long)
+ class_types = (type, types.ClassType)
+ text_type = unicode
+ binary_type = str
+ long = long
+
+if PY3: # pragma: no cover
+ from urllib.parse import unquote_to_bytes
+ def unquote_bytes_to_wsgi(bytestring):
+ return unquote_to_bytes(bytestring).decode('latin-1')
+else:
+ from urlparse import unquote as unquote_to_bytes
+ def unquote_bytes_to_wsgi(bytestring):
+ return unquote_to_bytes(bytestring)
+
+def text_(s, encoding='latin-1', errors='strict'):
+ """ If ``s`` is an instance of ``binary_type``, return
+ ``s.decode(encoding, errors)``, otherwise return ``s``"""
+ if isinstance(s, binary_type):
+ return s.decode(encoding, errors)
+ return s # pragma: no cover
+
+if PY3: # pragma: no cover
+ def tostr(s):
+ if isinstance(s, text_type):
+ s = s.encode('latin-1')
+ return str(s, 'latin-1', 'strict')
+ def tobytes(s):
+ return bytes(s, 'latin-1')
+else:
+ tostr = str
+ def tobytes(s):
+ return s
+
+try:
+ from Queue import (
+ Queue,
+ Empty,
+ )
+except ImportError: # pragma: no cover
+ from queue import (
+ Queue,
+ Empty,
+ )
+
+try:
+ import thread
+except ImportError: # pragma: no cover
+ import _thread as thread
+
+
+if PY3: # pragma: no cover
+ import builtins
+ exec_ = getattr(builtins, "exec")
+
+ def reraise(tp, value, tb=None):
+ if value is None:
+ value = tp
+ if value.__traceback__ is not tb:
+ raise value.with_traceback(tb)
+ raise value
+
+
+ del builtins
+
+else: # pragma: no cover
+ 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
+""")
+
+
+try:
+ from StringIO import StringIO as NativeIO
+except ImportError: # pragma: no cover
+ from io import StringIO as NativeIO
+
+try:
+ import httplib
+except ImportError: # pragma: no cover
+ from http import client as httplib
+