From c66c6d1aeff92f838740b7745a9c2a47852949d6 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 22 Dec 2021 15:33:11 -0500 Subject: add recursion check for with_loader_criteria() option Fixed recursion overflow which could occur within ORM statement compilation when using either the :func:`_orm.with_loader_criteria` feature or the the :meth:`_orm.PropComparator.and_` method within a loader strategy in conjunction with a subquery which referred to the same entity being altered by the criteria option, or loaded by the loader strategy. A check for coming across the same loader criteria option in a recursive fashion has been added to accommodate for this scenario. Fixes: #7491 Change-Id: I8701332717c45a21948ea4788a3058c0fbbf03a7 --- lib/sqlalchemy/sql/annotation.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/annotation.py b/lib/sqlalchemy/sql/annotation.py index 519a3103b..1706da44e 100644 --- a/lib/sqlalchemy/sql/annotation.py +++ b/lib/sqlalchemy/sql/annotation.py @@ -243,7 +243,9 @@ class Annotated: annotated_classes = {} -def _deep_annotate(element, annotations, exclude=None): +def _deep_annotate( + element, annotations, exclude=None, detect_subquery_cols=False +): """Deep copy the given ClauseElement, annotating each element with the given annotations dictionary. @@ -257,6 +259,7 @@ def _deep_annotate(element, annotations, exclude=None): cloned_ids = {} def clone(elem, **kw): + kw["detect_subquery_cols"] = detect_subquery_cols id_ = id(elem) if id_ in cloned_ids: @@ -267,9 +270,12 @@ def _deep_annotate(element, annotations, exclude=None): and hasattr(elem, "proxy_set") and elem.proxy_set.intersection(exclude) ): - newelem = elem._clone(**kw) + newelem = elem._clone(clone=clone, **kw) elif annotations != elem._annotations: - newelem = elem._annotate(annotations) + if detect_subquery_cols and elem._is_immutable: + newelem = elem._clone(clone=clone, **kw)._annotate(annotations) + else: + newelem = elem._annotate(annotations) else: newelem = elem newelem._copy_internals(clone=clone) -- cgit v1.2.1