Fix context managers behavior on exceptions.

This commit is contained in:
Florian Bruhin 2015-06-15 06:18:16 +02:00
parent 8dc9f0562a
commit 70699988ed
3 changed files with 21 additions and 13 deletions

View File

@ -230,7 +230,9 @@ def log_time(logger, action='operation'):
action: A description of what's being done. action: A description of what's being done.
""" """
started = datetime.datetime.now() started = datetime.datetime.now()
try:
yield yield
finally:
finished = datetime.datetime.now() finished = datetime.datetime.now()
delta = (finished - started).total_seconds() delta = (finished - started).total_seconds()
logger.debug("{} took {} seconds.".format(action.capitalize(), delta)) logger.debug("{} took {} seconds.".format(action.capitalize(), delta))

View File

@ -159,7 +159,9 @@ def init_log(args):
def disable_qt_msghandler(): def disable_qt_msghandler():
"""Contextmanager which temporarily disables the Qt message handler.""" """Contextmanager which temporarily disables the Qt message handler."""
old_handler = QtCore.qInstallMessageHandler(None) old_handler = QtCore.qInstallMessageHandler(None)
try:
yield yield
finally:
QtCore.qInstallMessageHandler(old_handler) QtCore.qInstallMessageHandler(old_handler)
@ -319,7 +321,9 @@ def hide_qt_warning(pattern, logger='qt'):
log_filter = QtWarningFilter(pattern) log_filter = QtWarningFilter(pattern)
logger_obj = logging.getLogger(logger) logger_obj = logging.getLogger(logger)
logger_obj.addFilter(log_filter) logger_obj.addFilter(log_filter)
try:
yield yield
finally:
logger_obj.removeFilter(log_filter) logger_obj.removeFilter(log_filter)

View File

@ -438,7 +438,9 @@ def disabled_excepthook():
"""Run code with the exception hook temporarily disabled.""" """Run code with the exception hook temporarily disabled."""
old_excepthook = sys.excepthook old_excepthook = sys.excepthook
sys.excepthook = sys.__excepthook__ sys.excepthook = sys.__excepthook__
try:
yield yield
finally:
# If the code we did run did change sys.excepthook, we leave it # If the code we did run did change sys.excepthook, we leave it
# unchanged. Otherwise, we reset it. # unchanged. Otherwise, we reset it.
if sys.excepthook is sys.__excepthook__: if sys.excepthook is sys.__excepthook__: