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

View File

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

View File

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