Skip to content

Commit 37306ae

Browse files
committed
Fixes #159: exceptions happened during flush() should not be wrapped with CommitException
1 parent 524e7ad commit 37306ae

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pony/orm/core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,19 @@ def transact_reraise(exc_class, exceptions):
280280
def commit():
281281
caches = _get_caches()
282282
if not caches: return
283+
284+
try:
285+
for cache in caches:
286+
cache.flush()
287+
except:
288+
rollback()
289+
raise
290+
283291
primary_cache = caches[0]
284292
other_caches = caches[1:]
285293
exceptions = []
286-
try: primary_cache.commit()
294+
try:
295+
primary_cache.commit()
287296
except:
288297
exceptions.append(sys.exc_info())
289298
for cache in other_caches:

pony/orm/tests/test_db_session.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,34 @@ def test():
296296
pass
297297
test()
298298

299+
@raises_exception(ZeroDivisionError)
300+
def test_db_session_exceptions_1(self):
301+
def before_insert(self):
302+
1/0
303+
self.X.before_insert = before_insert
304+
with db_session:
305+
self.X(a=3, b=3)
306+
# Should raise ZeroDivisionError and not CommitException
307+
308+
@raises_exception(ZeroDivisionError)
309+
def test_db_session_exceptions_2(self):
310+
def before_insert(self):
311+
1 / 0
312+
self.X.before_insert = before_insert
313+
with db_session:
314+
self.X(a=3, b=3)
315+
commit()
316+
# Should raise ZeroDivisionError and not CommitException
317+
318+
@raises_exception(ZeroDivisionError)
319+
def test_db_session_exceptions_3(self):
320+
def before_insert(self):
321+
1 / 0
322+
self.X.before_insert = before_insert
323+
with db_session:
324+
self.X(a=3, b=3)
325+
db.commit()
326+
# Should raise ZeroDivisionError and not CommitException
299327

300328
db = Database('sqlite', ':memory:')
301329

0 commit comments

Comments
 (0)