summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/util.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-12-27 15:02:31 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2019-12-30 14:07:18 -0500
commit04fbb9e63c098dd2de40b545eed210dfd93893ce (patch)
treef509e09f71c9a382b2d7934cf81262ad019df377 /lib/sqlalchemy/orm/util.py
parent9d4a58d35c53484a1de66396139fc34cd65f5be8 (diff)
downloadsqlalchemy-04fbb9e63c098dd2de40b545eed210dfd93893ce.tar.gz
Test for short term reference cycles and resolve as many as possible
Added test support and repaired a wide variety of unnecessary reference cycles created for short-lived objects, mostly in the area of ORM queries. Fixes: #5056 Change-Id: Ifd93856eba550483f95f9ae63d49f36ab068b85a
Diffstat (limited to 'lib/sqlalchemy/orm/util.py')
-rw-r--r--lib/sqlalchemy/orm/util.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py
index 2f78fa535..047eb9c16 100644
--- a/lib/sqlalchemy/orm/util.py
+++ b/lib/sqlalchemy/orm/util.py
@@ -8,6 +8,7 @@
import re
import types
+import weakref
from . import attributes # noqa
from .base import _class_to_mapper # noqa
@@ -583,14 +584,14 @@ class AliasedInsp(sql_base.HasCacheKey, InspectionAttr):
adapt_on_names,
represents_outer_join,
):
- self.entity = entity
+ self._weak_entity = weakref.ref(entity)
self.mapper = mapper
self.selectable = (
self.persist_selectable
) = self.local_table = selectable
self.name = name
self.polymorphic_on = polymorphic_on
- self._base_alias = _base_alias or self
+ self._base_alias = weakref.ref(_base_alias or self)
self._use_mapper_path = _use_mapper_path
self.represents_outer_join = represents_outer_join
@@ -625,6 +626,10 @@ class AliasedInsp(sql_base.HasCacheKey, InspectionAttr):
self._adapt_on_names = adapt_on_names
self._target = mapper.class_
+ @property
+ def entity(self):
+ return self._weak_entity()
+
is_aliased_class = True
"always returns True"
@@ -643,7 +648,7 @@ class AliasedInsp(sql_base.HasCacheKey, InspectionAttr):
:class:`.AliasedInsp`."""
return self.mapper.class_
- @util.memoized_property
+ @property
def _path_registry(self):
if self._use_mapper_path:
return self.mapper._path_registry
@@ -659,7 +664,7 @@ class AliasedInsp(sql_base.HasCacheKey, InspectionAttr):
"adapt_on_names": self._adapt_on_names,
"with_polymorphic_mappers": self.with_polymorphic_mappers,
"with_polymorphic_discriminator": self.polymorphic_on,
- "base_alias": self._base_alias,
+ "base_alias": self._base_alias(),
"use_mapper_path": self._use_mapper_path,
"represents_outer_join": self.represents_outer_join,
}
@@ -1241,7 +1246,7 @@ def _entity_corresponds_to(given, entity):
"""
if entity.is_aliased_class:
if given.is_aliased_class:
- if entity._base_alias is given._base_alias:
+ if entity._base_alias() is given._base_alias():
return True
return False
elif given.is_aliased_class: