diff options
Diffstat (limited to 'lib/sqlalchemy/orm')
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 63035f585..9ac6d07da 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -2713,7 +2713,14 @@ class Query( return None def __iter__(self) -> Iterable[_T]: - return self._iter().__iter__() # type: ignore + result = self._iter() + try: + yield from result + except GeneratorExit: + # issue #8710 - direct iteration is not re-usable after + # an iterable block is broken, so close the result + result._soft_close() + raise def _iter(self) -> Union[ScalarResult[_T], Result[_T]]: # new style execution. |