diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-07-14 15:41:31 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-07-14 15:41:31 -0400 |
commit | 800efc75256283770d5c28ddd99f26f341733698 (patch) | |
tree | 195206da35d9f2d434e9bd35596038ecaab1e832 /lib/sqlalchemy/util/compat.py | |
parent | 6dad6332cd0b777e4d876f51fada4fdf31299c53 (diff) | |
download | sqlalchemy-800efc75256283770d5c28ddd99f26f341733698.tar.gz |
- [feature] *Very limited* support for
inheriting mappers to be GC'ed when the
class itself is deferenced. The mapper
must not have its own table (i.e.
single table inh only) without polymorphic
attributes in place.
This allows for the use case of
creating a temporary subclass of a declarative
mapped class, with no table or mapping
directives of its own, to be garbage collected
when dereferenced by a unit test.
[ticket:2526]
Diffstat (limited to 'lib/sqlalchemy/util/compat.py')
-rw-r--r-- | lib/sqlalchemy/util/compat.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index 5dc59b5c5..215a68e91 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -166,6 +166,27 @@ except ImportError: return 'defaultdict(%s, %s)' % (self.default_factory, dict.__repr__(self)) +try: + from weakref import WeakSet +except: + import weakref + + class WeakSet(object): + """Implement the small subset of set() which SQLAlchemy needs + here. """ + def __init__(self, values=None): + self._storage = weakref.WeakKeyDictionary() + if values is not None: + self._storage.update((value, None) for value in values) + + def __iter__(self): + return iter(self._storage) + + def union(self, other): + return WeakSet(set(self).union(other)) + + def add(self, other): + self._storage[other] = True # find or create a dict implementation that supports __missing__ class _probe(dict): |