Skip to main content

nice Python exception hook replacement

Project description

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.

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

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:

pip install better_exchook

Usage

import better_exchook
better_exchook.install()  # will just do: sys.excepthook = better_exchook

Or:

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:

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:

EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 478, in <module>
    line: f()
    locals:
      f = <local> <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 = <global> {'a': 'b', 1: 2}
      sys = <global> <module 'sys' (built-in)>
      sys.stdin = <global> <open file '<stdin>', mode 'r' at 0x107d9f0c0>
      sys.stdin.__class__ = <global> <type 'file'>
      sys.exc_info = <global> <built-in function exc_info>
      y = <local> 'foo'
      z = <not found>
NameError: global name 'z' is not defined

Python example code:

try:
    f = lambda x: None
    f(x, y)
except Exception:
    better_exchook.better_exchook(*sys.exc_info())

Output:

EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 484, in <module>
    line: f(x, y)
    locals:
      f = <local> <function <lambda> at 0x107f1df50>
      x = <local> {'a': 'b', 1: 2}
      y = <not found>
NameError: name 'y' is not defined

Python example code:

try:
    (lambda x: None)(__name__,
                     42)  # multiline
except Exception:
    better_exchook.better_exchook(*sys.exc_info())

Output:

EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 490, in <module>
    line: (lambda x: None)(__name__,
                           42)  # multiline
    locals:
      x = <local> {'a': 'b', 1: 2}
      __name__ = <local> '__main__', len = 8
TypeError: <lambda>() takes exactly 1 argument (2 given)

Python example code:

# use this to overwrite the global exception handler
sys.excepthook = better_exchook.better_exchook
# and fail
finalfail(sys)

Output:

EXCEPTION
Traceback (most recent call last):
  File "better_exchook.py", line 497, in <module>
    line: finalfail(sys)
    locals:
      finalfail = <not found>
      sys = <local> <module 'sys' (built-in)>
NameError: name 'finalfail' is not defined

Screenshot:

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

Screencast with DomTerm using text folding (see more):

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

Similar projects

– Albert Zeyer, <http://www.az2000.de>

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

better_exchook-1.20250617.190044.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

better_exchook-1.20250617.190044-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file better_exchook-1.20250617.190044.tar.gz.

File metadata

File hashes

Hashes for better_exchook-1.20250617.190044.tar.gz
Algorithm Hash digest
SHA256 d47132c575709607706389f2f87ceeeaccd62795767d8e3f22269ecd7b3a81a1
MD5 63e757cacad98e6362fa36d1aa6c97a1
BLAKE2b-256 2bc714bd96cb98728c4831ce48dc7eb7af4771379bfb20d5429e86b02aaa786c

See more details on using hashes here.

File details

Details for the file better_exchook-1.20250617.190044-py3-none-any.whl.

File metadata

File hashes

Hashes for better_exchook-1.20250617.190044-py3-none-any.whl
Algorithm Hash digest
SHA256 eb58cfe43d7bb43d2d94133a99ea6ec8e5d4a07252f93a8aa6d89e9a076c1e04
MD5 8a93993c3d579a7bdffe26df48504b05
BLAKE2b-256 3ed7d20cc22755e97ee4686014e13ec29749a5b10dd19f46a067a78a0300db31

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page