summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r--lib/sqlalchemy/util.py53
1 files changed, 45 insertions, 8 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py
index 8eeeda455..f970f3737 100644
--- a/lib/sqlalchemy/util.py
+++ b/lib/sqlalchemy/util.py
@@ -4,8 +4,10 @@
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-import inspect, itertools, operator, sys, warnings, weakref
+import inspect, itertools, operator, sys, warnings, weakref, gc
+# Py2K
import __builtin__
+# end Py2K
types = __import__('types')
from sqlalchemy import exc
@@ -16,6 +18,7 @@ except ImportError:
import dummy_threading as threading
py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0)
+jython = sys.platform.startswith('java')
if py3k:
set_types = set
@@ -38,6 +41,8 @@ else:
EMPTY_SET = frozenset()
+NoneType = type(None)
+
if py3k:
import pickle
else:
@@ -46,11 +51,13 @@ else:
except ImportError:
import pickle
+# Py2K
# a controversial feature, required by MySQLdb currently
def buffer(x):
return x
buffer = getattr(__builtin__, 'buffer', buffer)
+# end Py2K
if sys.version_info >= (2, 5):
class PopulateDict(dict):
@@ -84,12 +91,13 @@ else:
if py3k:
def callable(fn):
return hasattr(fn, '__call__')
-else:
- callable = __builtin__.callable
+ def cmp(a, b):
+ return (a > b) - (a < b)
-if py3k:
from functools import reduce
else:
+ callable = __builtin__.callable
+ cmp = __builtin__.cmp
reduce = __builtin__.reduce
try:
@@ -262,6 +270,15 @@ else:
def decode_slice(slc):
return (slc.start, slc.stop, slc.step)
+def update_copy(d, _new=None, **kw):
+ """Copy the given dict and update with the given values."""
+
+ d = d.copy()
+ if _new:
+ d.update(_new)
+ d.update(**kw)
+ return d
+
def flatten_iterator(x):
"""Given an iterator of which further sub-elements may also be
iterators, flatten the sub-elements into a single iterator.
@@ -296,6 +313,7 @@ def get_cls_kwargs(cls):
class_ = stack.pop()
ctr = class_.__dict__.get('__init__', False)
if not ctr or not isinstance(ctr, types.FunctionType):
+ stack.update(class_.__bases__)
continue
names, _, has_kw, _ = inspect.getargspec(ctr)
args.update(names)
@@ -419,20 +437,32 @@ def class_hierarchy(cls):
will not be descended.
"""
+ # Py2K
if isinstance(cls, types.ClassType):
return list()
+ # end Py2K
hier = set([cls])
process = list(cls.__mro__)
while process:
c = process.pop()
+ # Py2K
if isinstance(c, types.ClassType):
continue
for b in (_ for _ in c.__bases__
if _ not in hier and not isinstance(_, types.ClassType)):
+ # end Py2K
+ # Py3K
+ #for b in (_ for _ in c.__bases__
+ # if _ not in hier):
process.append(b)
hier.add(b)
+ # Py3K
+ #if c.__module__ == 'builtins' or not hasattr(c, '__subclasses__'):
+ # continue
+ # Py2K
if c.__module__ == '__builtin__' or not hasattr(c, '__subclasses__'):
continue
+ # end Py2K
for s in [_ for _ in c.__subclasses__() if _ not in hier]:
process.append(s)
hier.add(s)
@@ -664,12 +694,11 @@ class OrderedProperties(object):
return self._data.keys()
def has_key(self, key):
- return self._data.has_key(key)
+ return key in self._data
def clear(self):
self._data.clear()
-
class OrderedDict(dict):
"""A dict that returns keys/values/items in the order they were added."""
@@ -735,7 +764,12 @@ class OrderedDict(dict):
def __setitem__(self, key, object):
if key not in self:
- self._list.append(key)
+ try:
+ self._list.append(key)
+ except AttributeError:
+ # work around Python pickle loads() with
+ # dict subclass (seems to ignore __setstate__?)
+ self._list = [key]
dict.__setitem__(self, key, object)
def __delitem__(self, key):
@@ -915,7 +949,7 @@ class IdentitySet(object):
if len(self) > len(other):
return False
- for m in itertools.ifilterfalse(other._members.has_key,
+ for m in itertools.ifilterfalse(other._members.__contains__,
self._members.iterkeys()):
return False
return True
@@ -1416,8 +1450,11 @@ class WeakIdentityMapping(weakref.WeakKeyDictionary):
return item
def clear(self):
+ # Py2K
+ # in 3k, MutableMapping calls popitem()
self._weakrefs.clear()
self.by_id.clear()
+ # end Py2K
weakref.WeakKeyDictionary.clear(self)
def update(self, *a, **kw):