{"id":16513067,"url":"https://github.com/albertz/py_better_exchook","last_synced_at":"2025-04-09T11:10:04.560Z","repository":{"id":1476025,"uuid":"1718790","full_name":"albertz/py_better_exchook","owner":"albertz","description":"nice Python exception hook replacement","archived":false,"fork":false,"pushed_at":"2025-03-18T20:42:08.000Z","size":434,"stargazers_count":42,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T10:12:04.581Z","etag":null,"topics":["debugging","python","stacktrace","variables"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/albertz.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"License.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-05-08T14:22:00.000Z","updated_at":"2025-03-22T14:07:45.000Z","dependencies_parsed_at":"2024-12-16T11:34:41.412Z","dependency_job_id":"51dea19c-957d-4197-b386-a9c13213e434","html_url":"https://github.com/albertz/py_better_exchook","commit_stats":{"total_commits":168,"total_committers":4,"mean_commits":42.0,"dds":"0.45238095238095233","last_synced_commit":"ea490a286c39a068c2ec34cd6f9af192bc741d2a"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/albertz%2Fpy_better_exchook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/albertz%2Fpy_better_exchook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/albertz%2Fpy_better_exchook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/albertz%2Fpy_better_exchook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/albertz","download_url":"https://codeload.github.com/albertz/py_better_exchook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248027408,"owners_count":21035594,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["debugging","python","stacktrace","variables"],"created_at":"2024-10-11T16:07:12.468Z","updated_at":"2025-04-09T11:10:04.537Z","avatar_url":"https://github.com/albertz.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"==============\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 \u003chttps://github.com/albertz/py_better_exchook/blob/master/better_exchook.py\u003e`_ 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 \u003chttps://github.com/PerBothner/DomTerm\u003e`__ text folding\n  (`see more \u003chttps://stackoverflow.com/a/54019993/133374\u003e`__),\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 \u003chttps://pypi.python.org/pypi/better_exchook\u003e`_\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\nOr:\n\n.. code:: python\n\n  import better_exchook\n  better_exchook.setup_all()\n\n* **setup_all**\n    - ``install`` + ``replace_traceback_format_tb`` + ``replace_traceback_print_tb``\n* **install**:\n    - ``sys.excepthook = better_exchook``\n* **replace_traceback_format_tb**:\n    - ``traceback.format_tb = format_tb``\n    - ``traceback.StackSummary.format = format_tb``\n    - ``traceback.StackSummary.extract = _StackSummary_extract``\n* **replace_traceback_print_tb**:\n    - ``traceback.print_tb = print_tb``\n    - ``traceback.print_exception = print_exception``\n    - ``traceback.print_exc = print_exc``\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 \u003cmodule\u003e\n      line: f()\n      locals:\n        f = \u003clocal\u003e \u003cfunction f at 0x107f1de60\u003e\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 = \u003cglobal\u003e {'a': 'b', 1: 2}\n        sys = \u003cglobal\u003e \u003cmodule 'sys' (built-in)\u003e\n        sys.stdin = \u003cglobal\u003e \u003copen file '\u003cstdin\u003e', mode 'r' at 0x107d9f0c0\u003e\n        sys.stdin.__class__ = \u003cglobal\u003e \u003ctype 'file'\u003e\n        sys.exc_info = \u003cglobal\u003e \u003cbuilt-in function exc_info\u003e\n        y = \u003clocal\u003e 'foo'\n        z = \u003cnot found\u003e\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 \u003cmodule\u003e\n      line: f(x, y)\n      locals:\n        f = \u003clocal\u003e \u003cfunction \u003clambda\u003e at 0x107f1df50\u003e\n        x = \u003clocal\u003e {'a': 'b', 1: 2}\n        y = \u003cnot found\u003e\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 \u003cmodule\u003e\n      line: (lambda x: None)(__name__,\n                             42)  # multiline\n      locals:\n        x = \u003clocal\u003e {'a': 'b', 1: 2}\n        __name__ = \u003clocal\u003e '__main__', len = 8\n  TypeError: \u003clambda\u003e() 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 \u003cmodule\u003e\n      line: finalfail(sys)\n      locals:\n        finalfail = \u003cnot found\u003e\n        sys = \u003clocal\u003e \u003cmodule 'sys' (built-in)\u003e\n  NameError: name 'finalfail' is not defined\n\nScreenshot:\n\n.. image:: https://gist.githubusercontent.com/albertz/a4ce78e5ccd037041638777f10b10327/raw/7ec2bb7079dbd56119d498f20905404cb2d812c0/screenshot1.png\n\n.. _domterm:\n\nScreencast with `DomTerm \u003chttp://domterm.org\u003e`__ using text folding (`see more \u003chttps://stackoverflow.com/a/54019993/133374\u003e`__):\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 \u003chttp://nose.readthedocs.io/en/latest/plugins/failuredetail.html\u003e`_.\n* IPython has something similar (`ultratb \u003chttps://github.com/ipython/ipython/blob/master/IPython/core/ultratb.py\u003e`__).\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 \u003chttps://docs.python.org/3/library/cgitb.html\u003e`__,\n  `code here \u003chttps://github.com/python/cpython/blob/3.7/Lib/cgitb.py\u003e`__.\n* `Rich Python library \u003chttps://github.com/willmcgugan/rich#tracebacks\u003e`__.\n  Syntax highlighting but without locals.\n* `andy-landy / traceback_with_variables \u003chttps://github.com/andy-landy/traceback_with_variables\u003e`__.\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 \u003chttps://github.com/cknd/stackprinter\u003e`__.\n  Similar as IPython ultratb.\n* `patrys / great-justice \u003chttps://github.com/patrys/great-justice\u003e`_\n* See `this \u003chttp://stackoverflow.com/questions/1308607/python-assert-improved-introspection-of-failure\u003e`__\n  related StackOverflow question.\n\n\n-- Albert Zeyer, \u003chttp://www.az2000.de\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falbertz%2Fpy_better_exchook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falbertz%2Fpy_better_exchook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falbertz%2Fpy_better_exchook/lists"}