Created
September 13, 2019 17:01
-
-
Save mmerickel/3652e5c45d751b27cab66df1e22f70cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .tm_context import tm_context | |
with tm_context(request.tm): | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager, suppress | |
from transaction.interfaces import NoTransaction | |
class DoomedAbort(Exception): | |
pass | |
@contextmanager | |
def tm_context(tm): | |
tm.begin() | |
try: | |
yield tm | |
with suppress(NoTransaction): | |
if tm.isDoomed(): | |
raise DoomedAbort | |
tm.commit() | |
except DoomedAbort: | |
with suppress(NoTransaction): | |
tm.abort() | |
except BaseException as ex: | |
with suppress(NoTransaction): | |
tm.abort() | |
raise ex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment