summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/compat.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-05-29 18:49:27 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-05-29 18:49:27 -0400
commit3bf77b1b0b18b6091c96e37823bcca87237d5482 (patch)
tree7ce0a8733694d2ebf6e696dcca68d22fad8bd62b /lib/sqlalchemy/util/compat.py
parent9c746c44a314d357b855108b6e99b773ce52f760 (diff)
parentbe2c145a84df5db0233f84995765d3f614776f75 (diff)
downloadsqlalchemy-3bf77b1b0b18b6091c96e37823bcca87237d5482.tar.gz
Merge branch 'rel_0_9'
Conflicts: lib/sqlalchemy/dialects/postgresql/hstore.py lib/sqlalchemy/util/__init__.py lib/sqlalchemy/util/compat.py
Diffstat (limited to 'lib/sqlalchemy/util/compat.py')
-rw-r--r--lib/sqlalchemy/util/compat.py188
1 files changed, 117 insertions, 71 deletions
diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py
index 6905d6347..3bbce5dde 100644
--- a/lib/sqlalchemy/util/compat.py
+++ b/lib/sqlalchemy/util/compat.py
@@ -14,7 +14,6 @@ except ImportError:
import dummy_threading as threading
py32 = sys.version_info >= (3, 2)
-py3k_warning = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0)
py3k = sys.version_info >= (3, 0)
py2k = sys.version_info < (3, 0)
jython = sys.platform.startswith('java')
@@ -22,32 +21,10 @@ pypy = hasattr(sys, 'pypy_version_info')
win32 = sys.platform.startswith('win')
cpython = not pypy and not jython # TODO: something better for this ?
-if py3k_warning:
- set_types = set
-elif sys.version_info < (2, 6):
- import sets
- set_types = set, sets.Set
-else:
- # 2.6 deprecates sets.Set, but we still need to be able to detect them
- # in user code and as return values from DB-APIs
- ignore = ('ignore', None, DeprecationWarning, None, 0)
- import warnings
- try:
- warnings.filters.insert(0, ignore)
- except Exception:
- import sets
- else:
- import sets
- warnings.filters.remove(ignore)
- set_types = set, sets.Set
+next = next
-if sys.version_info < (2, 6):
- def next(iter):
- return iter.next()
-else:
- next = next
-if py3k_warning:
+if py3k:
import pickle
else:
try:
@@ -55,44 +32,114 @@ else:
except ImportError:
import pickle
-if sys.version_info < (2, 6):
- # emits a nasty deprecation warning
- # in newer pythons
- from cgi import parse_qsl
-else:
- from urlparse import parse_qsl
+if py3k:
+ import builtins
+
+ from inspect import getfullargspec as inspect_getfullargspec
+ from urllib.parse import quote_plus, unquote_plus, parse_qsl
+ import configparser
+ from io import StringIO
-# Py3K
-#from inspect import getfullargspec as inspect_getfullargspec
-# Py2K
-from inspect import getargspec as inspect_getfullargspec
-# end Py2K
+ from io import BytesIO as byte_buffer
-if py3k_warning:
- # they're bringing it back in 3.2. brilliant !
- def callable(fn):
- return hasattr(fn, '__call__')
+
+ string_types = str,
+ binary_type = bytes
+ text_type = str
+ int_types = int,
+ iterbytes = iter
+
+ def u(s):
+ return s
+
+ def ue(s):
+ return s
+
+ def b(s):
+ return s.encode("latin-1")
+
+ if py32:
+ callable = callable
+ else:
+ def callable(fn):
+ return hasattr(fn, '__call__')
def cmp(a, b):
return (a > b) - (a < b)
from functools import reduce
+
+ print_ = getattr(builtins, "print")
+
+ import_ = getattr(builtins, '__import__')
+
+ import itertools
+ itertools_filterfalse = itertools.filterfalse
+ itertools_filter = filter
+ itertools_imap = map
+
+ import base64
+ def b64encode(x):
+ return base64.b64encode(x).decode('ascii')
+ def b64decode(x):
+ return base64.b64decode(x.encode('ascii'))
+
else:
+ from inspect import getargspec as inspect_getfullargspec
+ from urllib import quote_plus, unquote_plus
+ from urlparse import parse_qsl
+ import ConfigParser as configparser
+ from StringIO import StringIO
+ from cStringIO import StringIO as byte_buffer
+
+ string_types = basestring,
+ binary_type = str
+ text_type = unicode
+ int_types = int, long
+ def iterbytes(buf):
+ return (ord(byte) for byte in buf)
+
+ def u(s):
+ # this differs from what six does, which doesn't support non-ASCII
+ # strings - we only use u() with
+ # literal source strings, and all our source files with non-ascii
+ # in them (all are tests) are utf-8 encoded.
+ return unicode(s, "utf-8")
+
+ def ue(s):
+ return unicode(s, "unicode_escape")
+
+ def b(s):
+ return s
+
+ def import_(*args):
+ if len(args) == 4:
+ args = args[0:3] + ([str(arg) for arg in args[3]],)
+ return __import__(*args)
+
callable = callable
cmp = cmp
reduce = reduce
-try:
- from collections import namedtuple
-except ImportError:
- def namedtuple(typename, fieldnames):
- def __new__(cls, *values):
- tup = tuple.__new__(cls, values)
- for i, fname in enumerate(fieldnames):
- setattr(tup, fname, tup[i])
- return tup
- tuptype = type(typename, (tuple, ), {'__new__': __new__})
- return tuptype
+ import base64
+ b64encode = base64.b64encode
+ b64decode = base64.b64decode
+
+ def print_(*args, **kwargs):
+ fp = kwargs.pop("file", sys.stdout)
+ if fp is None:
+ return
+ for arg in enumerate(args):
+ if not isinstance(arg, basestring):
+ arg = str(arg)
+ fp.write(arg)
+
+ import itertools
+ itertools_filterfalse = itertools.ifilterfalse
+ itertools_filter = itertools.ifilter
+ itertools_imap = itertools.imap
+
+
try:
from weakref import WeakSet
@@ -122,24 +169,8 @@ if win32 or jython:
else:
time_func = time.time
-
-if sys.version_info >= (2, 6):
- from operator import attrgetter as dottedgetter
-else:
- def dottedgetter(attr):
- def g(obj):
- for name in attr.split("."):
- obj = getattr(obj, name)
- return obj
- return g
-
-# Adapted from six.py
-if py3k:
- def b(s):
- return s.encode("latin-1")
-else:
- def b(s):
- return s
+from collections import namedtuple
+from operator import attrgetter as dottedgetter
if py3k:
@@ -150,19 +181,34 @@ if py3k:
raise value.with_traceback(tb)
raise value
- def raise_from_cause(exception, exc_info):
+ def raise_from_cause(exception, exc_info=None):
+ if exc_info is None:
+ exc_info = sys.exc_info()
exc_type, exc_value, exc_tb = exc_info
reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
else:
exec("def reraise(tp, value, tb=None, cause=None):\n"
" raise tp, value, tb\n")
- def raise_from_cause(exception, exc_info):
+ def raise_from_cause(exception, exc_info=None):
# not as nice as that of Py3K, but at least preserves
# the code line where the issue occurred
+ if exc_info is None:
+ exc_info = sys.exc_info()
exc_type, exc_value, exc_tb = exc_info
reraise(type(exception), exception, tb=exc_tb)
+if py3k:
+ exec_ = getattr(builtins, 'exec')
+else:
+ def exec_(func_text, globals_, lcl=None):
+ if lcl is None:
+ exec('exec func_text in globals_')
+ else:
+ exec('exec func_text in globals_, lcl')
+def with_metaclass(meta, *bases):
+ """Create a base class with a metaclass."""
+ return meta("MetaBase", bases, {})