From 6dad6332cd0b777e4d876f51fada4fdf31299c53 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 14 Jul 2012 14:24:51 -0400 Subject: [PATCH] - [feature] The registry of classes in declarative_base() is now a WeakValueDictionary. So subclasses of "Base" that are dereferenced will be garbage collected, *if they are not referred to by any other mappers/superclass mappers*. [ticket:2526] --- CHANGES | 8 ++++++++ lib/sqlalchemy/ext/declarative.py | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 72787307fd..e94c55601e 100644 --- a/CHANGES +++ b/CHANGES @@ -151,6 +151,14 @@ underneath "0.7.xx". need autoflush w pre-attached object. [ticket:2464] + - [feature] The registry of classes + in declarative_base() is now a + WeakValueDictionary. So subclasses of + "Base" that are dereferenced will be + garbage collected, *if they are not + referred to by any other mappers/superclass + mappers*. [ticket:2526] + - [removed] Deprecated identifiers removed: * allow_null_pks mapper() argument diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 23346730fd..651a94970f 100755 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -1036,7 +1036,7 @@ from sqlalchemy import util, exc from sqlalchemy.sql import util as sql_util, expression from sqlalchemy import event from sqlalchemy.orm.util import polymorphic_union, _mapper_or_none - +import weakref __all__ = 'declarative_base', 'synonym_for', \ 'comparable_using', 'instrument_declarative' @@ -1718,7 +1718,7 @@ def declarative_base(bind=None, metadata=None, mapper=None, cls=object, lcl_metadata.bind = bind if class_registry is None: - class_registry = {} + class_registry = weakref.WeakValueDictionary() bases = not isinstance(cls, tuple) and (cls,) or cls class_dict = dict(_decl_class_registry=class_registry, -- 2.47.3