From: Mike Bayer Date: Sun, 28 Oct 2012 23:07:56 +0000 (-0400) Subject: - do a straight __subclasses__ traversal here, so that we aren't X-Git-Tag: rel_0_8_0b1~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fb10b94e710a531203b73bd440aa149e18e3d06d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - do a straight __subclasses__ traversal here, so that we aren't iterating through all mappers in memory when a new event is tacked onto an unmapped superclass, also removes the strong ref that was breaking some GC teardown tests --- diff --git a/lib/sqlalchemy/orm/events.py b/lib/sqlalchemy/orm/events.py index 858c6e5a30..8f9680911e 100644 --- a/lib/sqlalchemy/orm/events.py +++ b/lib/sqlalchemy/orm/events.py @@ -336,32 +336,36 @@ class _EventsHold(object): else: collection = target.all_holds[target.class_] = [] - collection.append((target.class_, identifier, fn, raw, propagate)) + collection.append((identifier, fn, raw, propagate)) if propagate: - for subject_dispatch, (subclass, subject) in \ - target.established.items(): - if issubclass(subclass, target.class_): - subject_dispatch._listen(subject, identifier, fn, + stack = list(target.class_.__subclasses__()) + while stack: + subclass = stack.pop(0) + stack.extend(subclass.__subclasses__()) + subject = target.resolve(subclass) + if subject is not None: + subject.dispatch._listen(subject, identifier, fn, raw=raw, propagate=propagate) @classmethod def populate(cls, class_, subject): - cls.established[subject.dispatch] = (class_, subject) for subclass in class_.__mro__: if subclass in cls.all_holds: if subclass is class_: collection = cls.all_holds.pop(subclass) else: collection = cls.all_holds[subclass] - for target, ident, fn, raw, propagate in collection: + for ident, fn, raw, propagate in collection: if propagate or subclass is class_: subject.dispatch._listen(subject, ident, fn, raw, propagate) class _InstanceEventsHold(_EventsHold): all_holds = weakref.WeakKeyDictionary() - established = weakref.WeakKeyDictionary() + + def resolve(self, class_): + return orm.instrumentation.manager_of_class(class_) class HoldInstanceEvents(_EventsHold.HoldEvents, InstanceEvents): pass @@ -1029,7 +1033,9 @@ class MapperEvents(event.Events): class _MapperEventsHold(_EventsHold): all_holds = weakref.WeakKeyDictionary() - established = weakref.WeakKeyDictionary() + + def resolve(self, class_): + return orm.util._mapper_or_none(class_) class HoldMapperEvents(_EventsHold.HoldEvents, MapperEvents): pass diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index dfa3ef8523..43632ff134 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -932,7 +932,6 @@ def _attr_as_key(attr): else: return expression._column_as_key(attr) - _state_mapper = util.dottedgetter('manager.mapper') @inspection._inspects(object) @@ -950,6 +949,8 @@ def _inspect_mapped_object(instance): def _inspect_mapped_class(class_, configure=False): try: class_manager = attributes.manager_of_class(class_) + if not class_manager.is_mapped: + return None mapper = class_manager.mapper if configure and mapperlib.module._new_mappers: mapperlib.configure_mappers()