cannot override sys.excepthook

前端 未结 4 857
星月不相逢
星月不相逢 2020-12-02 00:02

I try to customize behavior of sys.excepthook as described by the recipe.

in ipython:

:import pdb, sys, traceback
:def info(type, value         


        
4条回答
  •  温柔的废话
    2020-12-02 00:45

    expanding on Chris answer, you can use another function like a decorator to add your own functionality to jupyters showbacktrace:

    from IPython.core.interactiveshell import InteractiveShell
    from functools import wraps
    import traceback
    import sys
    
    def change_function(func):
        @wraps(func)
        def showtraceback(*args, **kwargs):
            # extract exception type, value and traceback
            etype, evalue, tb = sys.exc_info()
            if issubclass(etype, Exception):
                print('caught an exception')
            else:
                # otherwise run the original hook
                value = func(*args, **kwargs)
                return value
        return showtraceback
    
    InteractiveShell.showtraceback = change_function(InteractiveShell.showtraceback)
    
    raise IOError
    

提交回复
热议问题