From: Mike Bayer Date: Mon, 8 May 2017 22:36:57 +0000 (-0400) Subject: Protect against cls weakref becoming None X-Git-Tag: rel_1_2_0b1~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22570c3181ef4e98c548c2f6254a0c7585568f06;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Protect against cls weakref becoming None Protected against testing "None" as a class in the case where declarative classes are being garbage collected and new automap prepare() operations are taking place concurrently, very infrequently hitting a weakref that has not been fully acted upon after gc. Change-Id: I32e1dfc5ac46bac4127fe808cfd18368e2fad9dd --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 3f0df74c9e..2a44a2f100 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,17 @@ .. changelog:: :version: 1.1.10 + .. change:: 3980 + :tags: bug, ext + :versions: 1.2.0b1 + :tickets: 3980 + + Protected against testing "None" as a class in the case where + declarative classes are being garbage collected and new + automap prepare() operations are taking place concurrently, very + infrequently hitting a weakref that has not been fully acted upon + after gc. + .. change:: 3966 :tags: bug, mysql :versions: 1.2.0b1 diff --git a/lib/sqlalchemy/ext/automap.py b/lib/sqlalchemy/ext/automap.py index 14599fe5e5..bd33ebde9f 100644 --- a/lib/sqlalchemy/ext/automap.py +++ b/lib/sqlalchemy/ext/automap.py @@ -877,9 +877,9 @@ def _relationships_for_fks(automap_base, map_config, table_to_map_config, name_for_collection_relationship, generate_relationship): local_table = map_config.local_table - local_cls = map_config.cls + local_cls = map_config.cls # derived from a weakref, may be None - if local_table is None: + if local_table is None or local_cls is None: return for constraint in local_table.constraints: if isinstance(constraint, ForeignKeyConstraint): diff --git a/lib/sqlalchemy/ext/declarative/base.py b/lib/sqlalchemy/ext/declarative/base.py index 3beee31910..95f02ea968 100644 --- a/lib/sqlalchemy/ext/declarative/base.py +++ b/lib/sqlalchemy/ext/declarative/base.py @@ -568,8 +568,12 @@ class _DeferredMapperConfig(_MapperConfig): @classmethod def classes_for_base(cls, base_cls, sort=True): - classes_for_base = [m for m in cls._configs.values() - if issubclass(m.cls, base_cls)] + classes_for_base = [ + m for m, cls_ in + [(m, m.cls) for m in cls._configs.values()] + if cls_ is not None and issubclass(cls_, base_cls) + ] + if not sort: return classes_for_base