Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding function to sys.excepthook

Say I have something like this, which sends unhanded exceptions to logging.critical():

import sys

def register_handler():
    orig_excepthook = sys.excepthook

    def error_catcher(*exc_info):
        import logging
        log = logging.getLogger(__name__)
        log.critical("Unhandled exception", exc_info=exc_info)
        orig_excepthook(*exc_info)

    sys.excepthook = error_catcher

It works:

import logging
logging.basicConfig()

register_handler()

undefined() # logs, then runs original excepthook

However if register_handler() is called multiple times, multiple error_catcher's are called in a chain, and the logging message appears several times..

I can think of a few ways, but none of them are particularly good (like checking if sys.excepthook is the error_catcher function, or using a "have_registered" attribute on the module to avoid double-registering)

Is there a recommended way of doing this?

like image 764
dbr Avatar asked Dec 07 '11 12:12

dbr


4 Answers

You can just check if sys.excepthook is still built-in function before registering your handler:

>>> import sys, types
>>> isinstance(sys.excepthook, types.BuiltinFunctionType)
True
>>> sys.excepthook = lambda x: x
>>> isinstance(sys.excepthook, types.BuiltinFunctionType)
False
like image 94
Roman Bodnarchuk Avatar answered Oct 01 '22 07:10

Roman Bodnarchuk


If you put the code in your question into a module, you can import it many times, but it will be executed only the first time.

like image 38
Janne Karila Avatar answered Oct 01 '22 08:10

Janne Karila


Having a module-level "have the hook already been registered" variable seems like the simplest and most reliable way of doing this.

The other possible solutions would fall over in certain (rather obscure) circumstances - checking if the sys.excepthook is a builtin function will fail if an application registers a custom excepthook, storing the original excepthook at function-definition time will clobber subsequently registered excepthook functions.

import sys

_hook_registered = False

def register_handler(force = False):
    global _hook_registered

    if _hook_registered and not force:
        return

    orig_excepthook = sys.excepthook

    def error_catcher(*exc_info):
        import logging
        log = logging.getLogger(__name__)
        log.critical("Unhandled exception", exc_info=exc_info)
        orig_excepthook(*exc_info)

    sys.excepthook = error_catcher

    _hook_registered = True
like image 28
dbr Avatar answered Oct 01 '22 09:10

dbr


If you make orig_excepthook an argument with a default value, the default value is fixed once at definition-time. So repeated calls to register_handler will not change orig_excepthook.

import sys

def register_handler(orig_excepthook=sys.excepthook):
    def error_catcher(*exc_info):
        import logging
        log = logging.getLogger(__name__)
        log.critical("Unhandled exception", exc_info=exc_info)
        orig_excepthook(*exc_info)
    sys.excepthook = error_catcher

import logging
logging.basicConfig()

register_handler()
register_handler()
register_handler()

undefined() 

produces only one call to log.critical.

like image 26
unutbu Avatar answered Oct 01 '22 08:10

unutbu