An open API service indexing awesome lists of open source software.

https://github.com/albertz/py_better_exchook

nice Python exception hook replacement
https://github.com/albertz/py_better_exchook

debugging python stacktrace variables

Last synced: 9 months ago
JSON representation

nice Python exception hook replacement

Awesome Lists containing this project

README

          

==============
better_exchook
==============

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 `_ for further details.

Features
--------
* Shows locals/globals per frame, but only those used in the current statement.
It does this by a simple Python code parser.
* Multi-line Python statements in the stack trace output,
in case the statement goes over multiple lines.
* Shows full function qualified name (not just ``co_name``).
* Colored/formatted output of each frame.
* Syntax highlighting for the Python source code.
* Support for `DomTerm `__ text folding
(`see more `__),
where it folds all the details of each stack frame away by default,
and thus provides a much more comprehensive overview,
while still providing all the details when needed.

.. image:: https://github.com/albertz/py_better_exchook/workflows/CI/badge.svg
:target: https://github.com/albertz/py_better_exchook/actions

Installation
------------

You can just copy over the single file ``better_exchook.py`` to your project.

Or alternatively, it is also available `on PyPI `_
and can be installed via:

.. code::

pip install better_exchook

Usage
-----

.. 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()

* **setup_all**
- ``install`` + ``replace_traceback_format_tb`` + ``replace_traceback_print_tb``
* **install**:
- ``sys.excepthook = better_exchook``
* **replace_traceback_format_tb**:
- ``traceback.format_tb = format_tb``
- ``traceback.StackSummary.format = format_tb``
- ``traceback.StackSummary.extract = _StackSummary_extract``
* **replace_traceback_print_tb**:
- ``traceback.print_tb = print_tb``
- ``traceback.print_exception = print_exception``
- ``traceback.print_exc = print_exc``

Examples
--------

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 =
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 =
sys.stdin = ', mode 'r' at 0x107d9f0c0>
sys.stdin.__class__ =
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 = 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 =
NameError: name 'finalfail' is not defined

Screenshot:

.. image:: https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/7ec2bb7079dbd56119d498f20905404cb2d812c0/screenshot1.png

.. _domterm:

Screencast with `DomTerm `__ using text folding (`see more `__):

.. image:: https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/7ec2bb7079dbd56119d498f20905404cb2d812c0/screencast-domterm.gif

Similar projects
----------------

* `Nose does something similar for assertion failures `_.
* IPython has something similar (`ultratb `__).
Do this: ``from IPython.core import ultratb; sys.excepthook = ultratb.VerboseTB()``.
Shows more source code context (but not necessarily all relevant parts).
* Ka-Ping Yee's "cgitb.py", which is part of Python,
`see here `__,
`code here `__.
* `Rich Python library `__.
Syntax highlighting but without locals.
* `andy-landy / traceback_with_variables `__.
Python Traceback (Error Message) Printing Variables.
Very similar, but less advanced.
Only shows locals, not globals, and also just all locals, not only those used in current statement.
Also does not expand statement if it goes over multiple lines.
* `cknd / stackprinter `__.
Similar as IPython ultratb.
* `patrys / great-justice `_
* See `this `__
related StackOverflow question.

-- Albert Zeyer,