nice Python exception hook replacement
BSD-2-CLAUSE License
A nicer drop-in-replacement for Python sys.excepthook
,
i.e. it prints stack traces with extended information.
It will add some useful information for each frame,
like printing the relevant variables (relevant = referenced in the code line).
Also see Python source and comments <https://github.com/albertz/py_better_exchook/blob/master/better_exchook.py>
_ for further details.
co_name
).DomTerm <https://github.com/PerBothner/DomTerm>
__,.. image:: https://github.com/albertz/py_better_exchook/workflows/CI/badge.svg :target: https://github.com/albertz/py_better_exchook/actions
You can just copy over the single file better_exchook.py
to your project.
Or alternatively, it is also available on PyPI <https://pypi.python.org/pypi/better_exchook>
_
and can be installed via:
.. code::
pip install better_exchook
.. code:: python
import better_exchook better_exchook.install() # will just do: sys.excepthook = better_exchook
Or:
.. code:: python
import better_exchook better_exchook.setup_all()
install
+ replace_traceback_format_tb
+ replace_traceback_print_tb
sys.excepthook = better_exchook
traceback.format_tb = format_tb
traceback.StackSummary.format = format_tb
traceback.StackSummary.extract = _StackSummary_extract
traceback.print_tb = print_tb
traceback.print_exception = print_exception
traceback.print_exc = print_exc
Python example code:
.. code:: python
try:
x = {1:2, "a":"b"}
def f():
y = "foo"
x, 42, sys.stdin.__class__, sys.exc_info, y, z
f()
except Exception:
better_exchook.better_exchook(*sys.exc_info())
Output:
.. code::
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 478, in line: f() locals: f = <function f at 0x107f1de60> File "better_exchook.py", line 477, in f line: x, 42, sys.stdin.class, sys.exc_info, y, z locals: x = {'a': 'b', 1: 2} sys = <module 'sys' (built-in)> sys.stdin = <open file '', mode 'r' at 0x107d9f0c0> sys.stdin.class = <type 'file'> sys.exc_info = y = 'foo' z = NameError: global name 'z' is not defined
Python example code:
.. code:: python
try:
f = lambda x: None
f(x, y)
except Exception:
better_exchook.better_exchook(*sys.exc_info())
Output:
.. code::
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 484, in line: f(x, y) locals: f = <function at 0x107f1df50> x = {'a': 'b', 1: 2} y = NameError: name 'y' is not defined
Python example code:
.. code:: python
try:
(lambda x: None)(__name__,
42) # multiline
except Exception:
better_exchook.better_exchook(*sys.exc_info())
Output:
.. code::
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 490, in line: (lambda x: None)(name, 42) # multiline locals: x = {'a': 'b', 1: 2} name = 'main', len = 8 TypeError: () takes exactly 1 argument (2 given)
Python example code:
.. code:: python
# use this to overwrite the global exception handler
sys.excepthook = better_exchook.better_exchook
# and fail
finalfail(sys)
Output:
.. code::
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 497, in line: finalfail(sys) locals: finalfail = sys = <module 'sys' (built-in)> NameError: name 'finalfail' is not defined
Screenshot:
Screencast with DomTerm <http://domterm.org>
__:
Nose does something similar for assertion failures <http://nose.readthedocs.io/en/latest/plugins/failuredetail.html>
_.ultratb <https://github.com/ipython/ipython/blob/master/IPython/core/ultratb.py>
__).from IPython.core import ultratb; sys.excepthook = ultratb.VerboseTB()
.see here <https://docs.python.org/3/library/cgitb.html>
,code here <https://github.com/python/cpython/blob/3.7/Lib/cgitb.py>
.Rich Python library <https://github.com/willmcgugan/rich#tracebacks>
__.andy-landy / traceback_with_variables <https://github.com/andy-landy/traceback_with_variables>
__.cknd / stackprinter <https://github.com/cknd/stackprinter>
__.patrys / great-justice <https://github.com/patrys/great-justice>
_this <http://stackoverflow.com/questions/1308607/python-assert-improved-introspection-of-failure>
__-- Albert Zeyer, http://www.az2000.de