.. changelog::
:version: 1.1.5
+ .. change:: try_finally_for_noautoflush
+ :tags: bug, orm
+
+ The :attr:`.Session.no_autoflush` context manager now ensures that
+ the autoflush flag is reset within a "finally" block, so that if
+ an exception is raised within the block, the state still resets
+ appropriately. Pull request courtesy Emin Arakelian.
+
.. change:: 3878
:tags: bug, sql
:tickets: 3878
"""
autoflush = self.autoflush
self.autoflush = False
- yield self
- self.autoflush = autoflush
+ try:
+ yield self
+ finally:
+ self.autoflush = autoflush
def _autoflush(self):
if self.autoflush and not self._flushing:
from sqlalchemy.testing import eq_, assert_raises, \
- assert_raises_message, assertions
+ assert_raises_message, assertions, is_true
from sqlalchemy.testing.util import gc_collect
from sqlalchemy.testing import pickleable
from sqlalchemy.util import pickle
assert u in sess.query(User).all()
assert u not in sess.new
+ def test_with_no_autoflush_after_exception(self):
+ sess = Session(autoflush=True)
+
+ assert_raises(
+ ZeroDivisionError,
+ testing.run_as_contextmanager,
+ sess.no_autoflush,
+ lambda obj: 1 / 0
+ )
+
+ is_true(sess.autoflush)
def test_deleted_flag(self):
users, User = self.tables.users, self.classes.User