Skip to content

Commit

Permalink
Merge branch 'master' to 'orm'
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Jan 13, 2017
2 parents 5c123dd + 1146ff1 commit 27c251d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from os.path import dirname

__version__ = '0.7.1'
__version__ = '0.7.2-dev'

def detect_mode():
try: import google.appengine
Expand Down
21 changes: 13 additions & 8 deletions pony/orm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ def transact_reraise(exc_class, exceptions):
reraise(exc_class, new_exc, tb)
finally: del exceptions, exc, tb, new_exc

def rollback_and_reraise(exc_info):
try:
rollback()
finally:
reraise(*exc_info)

@cut_traceback
def commit():
caches = _get_caches()
Expand All @@ -292,8 +298,7 @@ def commit():
for cache in caches:
cache.flush()
except:
rollback()
raise
rollback_and_reraise(sys.exc_info())

primary_cache = caches[0]
other_caches = caches[1:]
Expand Down Expand Up @@ -387,14 +392,15 @@ def __exit__(db_session, exc_type=None, exc=None, tb=None):
else:
assert exc is not None # exc can be None in Python 2.6 even if exc_type is not None
try: can_commit = db_session.allowed_exceptions(exc)
except:
rollback()
raise
except: rollback_and_reraise(sys.exc_info())
if can_commit:
commit()
for cache in _get_caches(): cache.release()
assert not local.db2cache
else: rollback()
else:
try: rollback()
except:
if exc_type is None: raise # if exc_type is not None it will be reraised outside of __exit__
finally:
del exc, tb
local.db_session = None
Expand Down Expand Up @@ -465,8 +471,7 @@ def wrapped_interact(iterator, input=None, exc_info=None):
if cache.modified or cache.in_transaction: throw(TransactionError,
'You need to manually commit() changes before yielding from the generator')
except:
rollback()
raise
rollback_and_reraise(sys.exc_info())
else:
return output
finally:
Expand Down
7 changes: 7 additions & 0 deletions pony/orm/tests/test_db_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ def before_insert(self):
db.commit()
# Should raise ZeroDivisionError and not CommitException

@raises_exception(ZeroDivisionError)
def test_db_session_exceptions_4(self):
with db_session:
connection = self.db.get_connection()
connection.close()
1/0

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

class Group(db.Entity):
Expand Down

0 comments on commit 27c251d

Please sign in to comment.