diff --git a/qutebrowser/utils/debug.py b/qutebrowser/utils/debug.py index 97a21c0e5..16aa53adf 100644 --- a/qutebrowser/utils/debug.py +++ b/qutebrowser/utils/debug.py @@ -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(): diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index 9106eb7dd..bac158c3d 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -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): diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index b25997662..b5f9ac25c 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -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