Skip to main content

If you have ever wrote a Python program that processes large input files you probably have been in a situation where you get an error on certain file and line. You may get a traceback telling you have e.g. a KeyError but still don’t know what line in that text file provoked it.

Then you may start adding print(line) calls every time a line is processed, but that’s quirky, clutters your terminal quickly and you’ll have to eventually remove it, only to later add it when the next bug hits.

This module is an alternative to improve this kind of debugging issue, making it clean and robust, whilst providing a easy to use API.

This module works by providing you with a context object where you can set state variables like file name and line number. When an unhandled exception is raised, this module catches it and prints the value of the state variables along with the traceback.

Example code:

from contextualized import contextualized_tracebacks

def main():
    dcontext.file = 'hello.txt'
    a()

def a():
    dcontext.line = 20
    b()

def b():
    # buggy code
    nonexistent += 1

with contextualized_tracebacks(['file', 'line', 'character']) as dcontext:
    main()

This program will show this once run:

Traceback (most recent call last):
  File "contextualized_example.py", line 16, in <module>
    main()
  File "contextualized_example.py", line 5, in main
    a()
  File "contextualized_example.py", line 9, in a
    b()
  File "contextualized_example.py", line 13, in b
    nonexistent += 1
UnboundLocalError: local variable 'nonexistent' referenced before assignment
The exception was caught on file 'hello.txt', line 20.

The context information is right after the traceback. Notice how only non None fields are shown. They also preserve the order from the contextualized_tracebacks() call.

Overriding the traceback print function

You can override the default traceback print with the print_tb argument. For example, this example will print the context before the traceback.

from contextualized import contextualized_tracebacks

def main():
    dcontext.file = 'hello.txt'
    a()

def a():
    dcontext.line = 20
    b()

def b():
    # buggy code
    nonexistent += 1

def my_print_tb(context, tb_info):
    import traceback
    import sys

    print('An exception was caught on %s.' % context._to_prose(),
          file=sys.stderr)
    traceback.print_exception(*tb_info)

with contextualized_tracebacks(['file', 'line', 'character'],
                               print_tb=my_print_tb) as dcontext:
    main()

This will be the new output:

An exception was caught on file 'hello.txt', line 20.
Traceback (most recent call last):
  File "contextualized_example.py", line 25, in <module>
    main()
  File "contextualized_example.py", line 5, in main
    a()
  File "contextualized_example.py", line 9, in a
    b()
  File "contextualized_example.py", line 13, in b
    nonexistent += 1
UnboundLocalError: local variable 'nonexistent' referenced before assignment

Changelog

  • 1.0: Initial release.

  • 1.1: SystemExit exceptions no longer are catched, thus triggering normal exit() by the interpreter.

  • 1.2: Modified setup.py to install successfully with LANG=C.

  • 1.3: Fixed a bug that caused contextualized not to work in Python 2.

License

This module is under the MIT License.

Copyright (c) 2015 Juan Luis Boya García

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for contextualized 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contextualized 1.3
File Size Uploaded Provenance
contextualized-1.3.tar.gz 4.2 kB Without provenance Details

Release files / contextualized-1.3.tar.gz

Download URL contextualized-1.3.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fe4095a9bc7a34aca0fbde4e24d1b16f6e5488fefbf92fb09bfd29f47a645a6a
BLAKE2b-256 checksum
How to use checksums
605c78c1a7ef91568614fa5e3f5f3d039457b0a10742a094bef926edca889742
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for contextualized 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page