diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-04-06 14:27:03 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-04-06 14:27:03 +0000 |
commit | 941d9caef865a0c70d14ba56d04041853ff4440d (patch) | |
tree | c551098e5c2cdc5198aaf7eefaaf2ab52d847878 /lib/sqlalchemy/attributes.py | |
parent | 680c27607328a8f89e446601f7bc7ed56394dc27 (diff) | |
download | sqlalchemy-941d9caef865a0c70d14ba56d04041853ff4440d.tar.gz |
the ultimate "hands off" approach to the object's dictionary of managed attributes
Diffstat (limited to 'lib/sqlalchemy/attributes.py')
-rw-r--r-- | lib/sqlalchemy/attributes.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/lib/sqlalchemy/attributes.py b/lib/sqlalchemy/attributes.py index 6424c9d79..1a399159e 100644 --- a/lib/sqlalchemy/attributes.py +++ b/lib/sqlalchemy/attributes.py @@ -78,9 +78,10 @@ class ManagedAttribute(object): which occurs through the SmartProperty property object ultimately calls upon ManagedAttribute objects associated with the instance via this dictionary.""" def __init__(self, obj, key): - self.__obj = weakref.ref(obj) + #self.__obj = weakref.ref(obj) + self.obj = obj self.key = key - obj = property(lambda s:s.__obj()) + #obj = property(lambda s:s.__obj()) def history(self, **kwargs): return self def plain_init(self, *args, **kwargs): @@ -493,6 +494,27 @@ class AttributeManager(object): the new object instance as an argument to create the new ManagedAttribute. Extra keyword arguments can be sent which will be passed along to newly created ManagedAttribute.""" - class_._attribute_manager = self + if not hasattr(class_, '_attribute_manager'): + class_._attribute_manager = self + class_._managed_attributes = ObjectAttributeGateway() setattr(class_, key, self.create_prop(class_, key, uselist, callable_, **kwargs)) +managed_attributes = weakref.WeakKeyDictionary() + +class ObjectAttributeGateway(object): + """handles the dictionary of ManagedAttributes for instances. this level of indirection + is to prevent circular references upon objects, as well as keeping them Pickle-compatible.""" + def __set__(self, obj, value): + managed_attributes[obj] = value + def __delete__(self, obj): + try: + del managed_attributes[obj] + except KeyError: + raise AttributeError() + def __get__(self, obj, owner): + if obj is None: + return self + try: + return managed_attributes[obj] + except KeyError: + raise AttributeError()
\ No newline at end of file |