Adding function to sys.excepthook

前端 未结 5 652
不知归路
不知归路 2021-01-13 12:02

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

import sys

def register_handler():
    orig_excepthook =         


        
5条回答
  •  逝去的感伤
    2021-01-13 13:09

    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
    

提交回复
热议问题