Skip to main content

Prettier tracebacks & call stacks, with variable values and colorful semantic highlighting

Project description

Python stack formatter

Print prettier tracebacks & call stacks, with more source code context and current variable values, to stay fearless when the only debugging tool is a log file.

Before

After

Installation

pip install stackprinter

Logging exceptions

To just replace the default python crash message, call set_excepthook() somewhere once:

import stackprinter
stackprinter.set_excepthook(style='color')

For manual mode, call show or format inside an except block to trace the current exception. show prints to stderr, format returns a string (for logging). You can also pass exception objects explicitly.

try:
    something()
except:
    stackprinter.show()  # grab the current exception and print a traceback to stderr

    # ...or only return a string, e.g. for logging.
    message = stackprinter.format()
    logging.log(message)

By default, this will generate plain text. Pass style='color' to any of these functions to get funky terminal colors (a type of semantic highlighting, not syntax highlighting). For more configs, see the docs of format().

Printing the call stack of another thread

Pass a thread object to show or format.

thread = threading.Thread(target=something)
thread.start()
while True:
    stackprinter.show(thread) # or format(thread)
    time.sleep(0.1)

Printing the call stack of the current thread

Call show or format outside of exception handling.

stackprinter.show() # or format()

There's also show_current_stack(), which does the same thing everywhere, even inside except blocks.

Tracing a piece of code as it is executed

More for curiosity than anything else, you can watch a piece of code execute step-by-step, printing a trace of all calls & returns 'live' as they are happening. Slows everything down though, of course.

tp = stackprinter.TracePrinter(style='color', suppressed_paths=[r"lib/python.*/site-packages/numpy"])
tp.enable()
a = np.ones(111)
dosomething(a)
tp.disable()

How it works

Basically, this is a frame formatter. For each frame on the call stack, it grabs the source code to find out which source lines reference which variables. Then it displays code and variables in the neighbourhood of the last executed line.

Since this already requires a map of where each variable occurs in the code, it was difficult to not also implement the whole semantic highlighting color thing seen in the screenshots. The colors are ANSI escape codes now, but it should be fairly straightforward™ to render the underlying data without any 1980ies terminal technology. Say, a foldable and clickable HTML page with downloadable pickled variables. For now you'll have to pipe the ANSI strings through ansi2html or something.

Caveats

This displays variable values as they are at the time of formatting. In multi-threaded programs, variables can change while we're busy walking the stack & printing them. So, if nothing seems to make sense, consider that your exception and the traceback messages are from slightly different times. Sadly, there is no responsible way to freeze all other threads as soon as we want to inspect some thread's call stack (...or is there?)

Docs

*coughs*

For now, just look at all the doc strings, e.g. of format()

Project details


Download files

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

Source Distribution

stackprinter-0.1.1.post1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

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

stackprinter-0.1.1.post1-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file stackprinter-0.1.1.post1.tar.gz.

File metadata

  • Download URL: stackprinter-0.1.1.post1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for stackprinter-0.1.1.post1.tar.gz
Algorithm Hash digest
SHA256 a439e9dfdf65849fea55d84a2158bd329d8725e0e5a8ca89f170f078aeaf1b24
MD5 ae1c77230cac7203fabb61a9c2f9e50a
BLAKE2b-256 df7b88a6ad646af7ad9a22ca08bb568d92a594cc7da63b2945458ca0ec7ee7cd

See more details on using hashes here.

File details

Details for the file stackprinter-0.1.1.post1-py3-none-any.whl.

File metadata

  • Download URL: stackprinter-0.1.1.post1-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for stackprinter-0.1.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 3fd305ce99b48d86f883a050be3c4b71a5459ba37a842bafe20e480627c05216
MD5 ffa6b6ea8e496fad5d08aeb3e3a031d5
BLAKE2b-256 349ae1cde52afa38b90a29fa738f80f06cbbb2bb8303e5f754d427aae8b1aea5

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