This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

nice Python exception hook replacement

Project Description

A nicer drop-in-replacement for Python sys.excepthook. 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.

You can just copy over the 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

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
Similar projects:

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

Release History

Release History

This version
History Node

1.20160612.150038

History Node

1.20160612.145523

History Node

1.20160612.135412

History Node

1.20160612.134947

History Node

1.20160612.134916

History Node

1.20160612.133806

History Node

1.20160612.132614

History Node

1.20160612.132345

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
better_exchook-1.20160612.150038.tar.gz (8.1 kB) Copy SHA256 Checksum SHA256 Source Jun 12, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting