better-exchook


Namebetter-exchook JSON
Version 1.20240112.4845 PyPI version JSON
download
home_pagehttps://github.com/albertz/py_better_exchook
Summarynice Python exception hook replacement
upload_time2024-01-11 23:49:31
maintainer
docs_urlNone
authorAlbert Zeyer
requires_python
license2-clause BSD license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ==============
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 <https://github.com/albertz/py_better_exchook/blob/master/better_exchook.py>`_ 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 <https://github.com/PerBothner/DomTerm>`__,
  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 <https://pypi.python.org/pypi/better_exchook>`_
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


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

.. 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 <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:

.. 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 <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:

.. 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 <module>
      line: finalfail(sys)
      locals:
        finalfail = <not found>
        sys = <local> <module 'sys' (built-in)>
  NameError: name 'finalfail' is not defined

Screenshot:

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

Screencast with `DomTerm <http://domterm.org>`__:

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


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

* `Nose does something similar for assertion failures <http://nose.readthedocs.io/en/latest/plugins/failuredetail.html>`_.
* IPython has something similar (`ultratb <https://github.com/ipython/ipython/blob/master/IPython/core/ultratb.py>`__).
  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 <https://docs.python.org/3/library/cgitb.html>`__,
  `code here <https://github.com/python/cpython/blob/3.7/Lib/cgitb.py>`__.
* `Rich Python library <https://github.com/willmcgugan/rich#tracebacks>`__.
  Syntax highlighting but without locals.
* `andy-landy / traceback_with_variables <https://github.com/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 <https://github.com/cknd/stackprinter>`__.
  Similar as IPython ultratb.
* `patrys / great-justice <https://github.com/patrys/great-justice>`_
* See `this <http://stackoverflow.com/questions/1308607/python-assert-improved-introspection-of-failure>`__
  related StackOverflow question.


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

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/albertz/py_better_exchook",
    "name": "better-exchook",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Albert Zeyer",
    "author_email": "albzey@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/98/5f/5b24e04efc98523bb77c6185467b1dfdcb531a2206204614375b6f0475d4/better_exchook-1.20240112.004845.tar.gz",
    "platform": null,
    "description": "==============\nbetter_exchook\n==============\n\nA nicer drop-in-replacement for Python ``sys.excepthook``,\ni.e. it prints stack traces with extended information.\nIt will add some useful information for each frame,\nlike printing the relevant variables (relevant = referenced in the code line).\nAlso see `Python source and comments <https://github.com/albertz/py_better_exchook/blob/master/better_exchook.py>`_ for further details.\n\nFeatures\n--------\n* Shows locals/globals per frame, but only those used in the current statement.\n  It does this by a simple Python code parser.\n* Multi-line Python statements in the stack trace output,\n  in case the statement goes over multiple lines.\n* Shows full function qualified name (not just ``co_name``).\n* Colored/formatted output of each frame.\n* Syntax highlighting for the Python source code.\n* Support for `DomTerm <https://github.com/PerBothner/DomTerm>`__,\n  where it folds all the details of each stack frame away by default,\n  and thus provides a much more comprehensive overview,\n  while still providing all the details when needed.\n\n.. image:: https://github.com/albertz/py_better_exchook/workflows/CI/badge.svg\n    :target: https://github.com/albertz/py_better_exchook/actions\n\n\nInstallation\n------------\n\nYou can just copy over the single file ``better_exchook.py`` to your project.\n\nOr alternatively, it is also available `on PyPI <https://pypi.python.org/pypi/better_exchook>`_\nand can be installed via:\n\n.. code::\n\n  pip install better_exchook\n\n\nUsage\n-----\n\n.. code:: python\n\n  import better_exchook\n  better_exchook.install()  # will just do: sys.excepthook = better_exchook\n\n\nExamples\n--------\n\nPython example code:\n\n.. code:: python\n\n    try:\n        x = {1:2, \"a\":\"b\"}\n        def f():\n            y = \"foo\"\n            x, 42, sys.stdin.__class__, sys.exc_info, y, z\n        f()\n    except Exception:\n        better_exchook.better_exchook(*sys.exc_info())\n\nOutput:\n\n.. code::\n\n  EXCEPTION\n  Traceback (most recent call last):\n    File \"better_exchook.py\", line 478, in <module>\n      line: f()\n      locals:\n        f = <local> <function f at 0x107f1de60>\n    File \"better_exchook.py\", line 477, in f\n      line: x, 42, sys.stdin.__class__, sys.exc_info, y, z\n      locals:\n        x = <global> {'a': 'b', 1: 2}\n        sys = <global> <module 'sys' (built-in)>\n        sys.stdin = <global> <open file '<stdin>', mode 'r' at 0x107d9f0c0>\n        sys.stdin.__class__ = <global> <type 'file'>\n        sys.exc_info = <global> <built-in function exc_info>\n        y = <local> 'foo'\n        z = <not found>\n  NameError: global name 'z' is not defined\n\nPython example code:\n\n.. code:: python\n\n    try:\n        f = lambda x: None\n        f(x, y)\n    except Exception:\n        better_exchook.better_exchook(*sys.exc_info())\n\nOutput:\n\n.. code::\n\n  EXCEPTION\n  Traceback (most recent call last):\n    File \"better_exchook.py\", line 484, in <module>\n      line: f(x, y)\n      locals:\n        f = <local> <function <lambda> at 0x107f1df50>\n        x = <local> {'a': 'b', 1: 2}\n        y = <not found>\n  NameError: name 'y' is not defined\n\nPython example code:\n\n.. code:: python\n\n    try:\n        (lambda x: None)(__name__,\n                         42)  # multiline\n    except Exception:\n        better_exchook.better_exchook(*sys.exc_info())\n\nOutput:\n\n.. code::\n\n  EXCEPTION\n  Traceback (most recent call last):\n    File \"better_exchook.py\", line 490, in <module>\n      line: (lambda x: None)(__name__,\n                             42)  # multiline\n      locals:\n        x = <local> {'a': 'b', 1: 2}\n        __name__ = <local> '__main__', len = 8\n  TypeError: <lambda>() takes exactly 1 argument (2 given)\n\nPython example code:\n\n.. code:: python\n\n    # use this to overwrite the global exception handler\n    sys.excepthook = better_exchook.better_exchook\n    # and fail\n    finalfail(sys)\n\nOutput:\n\n.. code::\n\n  EXCEPTION\n  Traceback (most recent call last):\n    File \"better_exchook.py\", line 497, in <module>\n      line: finalfail(sys)\n      locals:\n        finalfail = <not found>\n        sys = <local> <module 'sys' (built-in)>\n  NameError: name 'finalfail' is not defined\n\nScreenshot:\n\n.. image:: https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/7ec2bb7079dbd56119d498f20905404cb2d812c0/screenshot1.png\n\nScreencast with `DomTerm <http://domterm.org>`__:\n\n.. image:: https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/7ec2bb7079dbd56119d498f20905404cb2d812c0/screencast-domterm.gif\n\n\nSimilar projects\n----------------\n\n* `Nose does something similar for assertion failures <http://nose.readthedocs.io/en/latest/plugins/failuredetail.html>`_.\n* IPython has something similar (`ultratb <https://github.com/ipython/ipython/blob/master/IPython/core/ultratb.py>`__).\n  Do this: ``from IPython.core import ultratb; sys.excepthook = ultratb.VerboseTB()``.\n  Shows more source code context (but not necessarily all relevant parts).\n* Ka-Ping Yee's \"cgitb.py\", which is part of Python,\n  `see here <https://docs.python.org/3/library/cgitb.html>`__,\n  `code here <https://github.com/python/cpython/blob/3.7/Lib/cgitb.py>`__.\n* `Rich Python library <https://github.com/willmcgugan/rich#tracebacks>`__.\n  Syntax highlighting but without locals.\n* `andy-landy / traceback_with_variables <https://github.com/andy-landy/traceback_with_variables>`__.\n  Python Traceback (Error Message) Printing Variables.\n  Very similar, but less advanced.\n  Only shows locals, not globals, and also just all locals, not only those used in current statement.\n  Also does not expand statement if it goes over multiple lines.\n* `cknd / stackprinter <https://github.com/cknd/stackprinter>`__.\n  Similar as IPython ultratb.\n* `patrys / great-justice <https://github.com/patrys/great-justice>`_\n* See `this <http://stackoverflow.com/questions/1308607/python-assert-improved-introspection-of-failure>`__\n  related StackOverflow question.\n\n\n-- Albert Zeyer, <http://www.az2000.de>",
    "bugtrack_url": null,
    "license": "2-clause BSD license",
    "summary": "nice Python exception hook replacement",
    "version": "1.20240112.4845",
    "project_urls": {
        "Homepage": "https://github.com/albertz/py_better_exchook"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "985f5b24e04efc98523bb77c6185467b1dfdcb531a2206204614375b6f0475d4",
                "md5": "46ca24d3c1d70c91a77ec363ef2886e0",
                "sha256": "97c89a3792157ca205c2a931cf791c30cb94e9bd4fefae73ce4f40c337b58942"
            },
            "downloads": -1,
            "filename": "better_exchook-1.20240112.004845.tar.gz",
            "has_sig": false,
            "md5_digest": "46ca24d3c1d70c91a77ec363ef2886e0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 22370,
            "upload_time": "2024-01-11T23:49:31",
            "upload_time_iso_8601": "2024-01-11T23:49:31.392267Z",
            "url": "https://files.pythonhosted.org/packages/98/5f/5b24e04efc98523bb77c6185467b1dfdcb531a2206204614375b6f0475d4/better_exchook-1.20240112.004845.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-11 23:49:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "albertz",
    "github_project": "py_better_exchook",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "better-exchook"
}
        
Elapsed time: 0.16304s