Skip to content

Instantly share code, notes, and snippets.

@methane
Last active April 10, 2024 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/929d19af4aaaab3486f7bc3085172e4d to your computer and use it in GitHub Desktop.
Save methane/929d19af4aaaab3486f7bc3085172e4d to your computer and use it in GitHub Desktop.
short-traceback

stdlib traceback

Traceback (most recent call last):
  File "/Users/inada-n/tmp/f.py", line 64, in main
    f1()
    ~~^^
  File "/Users/inada-n/tmp/f.py", line 20, in f1
    return f2(10)
           ~~^^^^
  File "/Users/inada-n/tmp/f.py", line 25, in f2
    f2(n - 1)
    ~~^^^^^^^
  File "/Users/inada-n/tmp/f.py", line 25, in f2
    f2(n - 1)
    ~~^^^^^^^
  File "/Users/inada-n/tmp/f.py", line 25, in f2
    f2(n - 1)
    ~~^^^^^^^
  [Previous line repeated 7 more times]
  File "/Users/inada-n/tmp/f.py", line 27, in f2
    return sub.c(f3)
           ~~~~~^^^^
  File "/Users/inada-n/tmp/sub/__init__.py", line 2, in c
    f()
    ~^^
  File "/Users/inada-n/tmp/f.py", line 31, in f3
    return pathlib.Path("./foo").relative_to(pathlib.Path("./bar"))
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/inada-n/work/python/cpython/Lib/pathlib/__init__.py", line 392, in relative_to
    raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
ValueError: 'foo' is not in the subpath of 'bar'

in json

"Traceback (most recent call last):\n  File \"/Users/inada-n/tmp/f.py\", line 64, in main\n    f1()\n    ~~^^\n  File \"/Users/inada-n/tmp/f.py\", line 20, in f1\n    return f2(10)\n           ~~^^^^\n  File \"/Users/inada-n/tmp/f.py\", line 25, in f2\n    f2(n - 1)\n    ~~^^^^^^^\n  File \"/Users/inada-n/tmp/f.py\", line 25, in f2\n    f2(n - 1)\n    ~~^^^^^^^\n  File \"/Users/inada-n/tmp/f.py\", line 25, in f2\n    f2(n - 1)\n    ~~^^^^^^^\n  [Previous line repeated 7 more times]\n  File \"/Users/inada-n/tmp/f.py\", line 27, in f2\n    return sub.c(f3)\n           ~~~~~^^^^\n  File \"/Users/inada-n/tmp/sub/__init__.py\", line 2, in c\n    f()\n    ~^^\n  File \"/Users/inada-n/tmp/f.py\", line 31, in f3\n    return pathlib.Path(\"./foo\").relative_to(pathlib.Path(\"./bar\"))\n           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/Users/inada-n/work/python/cpython/Lib/pathlib/__init__.py\", line 392, in relative_to\n    raise ValueError(f\"{str(self)!r} is not in the subpath of {str(other)!r}\")\nValueError: 'foo' is not in the subpath of 'bar'\n"

py.code traceback

f.py:64: in main
    f1()
f.py:20: in f1
    return f2(10)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:25: in f2
    f2(n - 1)
f.py:27: in f2
    return sub.c(f3)
sub/__init__.py:2: in c
    f()
f.py:31: in f3
    return pathlib.Path("./foo").relative_to(pathlib.Path("./bar"))
../.rye/py/cpython@3.12.2/lib/python3.12/pathlib.py:682: in relative_to
    raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
E   ValueError: 'foo' is not in the subpath of 'bar'

short traceback

ValueError: 'foo' is not in the subpath of 'bar'
Traceback (most recent call first):
  12.2/lib/python3.12/pathlib.py:682 relative_to
  f.py:31 f3
  sub/__init__.py:2 c
  f.py:27 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:25 f2
  f.py:20 f1
  f.py:71 main

in json

"ValueError: 'foo' is not in the subpath of 'bar'\nTraceback (most recent call first):\n  12.2/lib/python3.12/pathlib.py:682 relative_to\n  f.py:31 f3\n  sub/__init__.py:2 c\n  f.py:27 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:25 f2\n  f.py:20 f1\n  f.py:71 main\n"
import sys
import os
import io
import traceback
import json
import pathlib
import sub
try:
import py.code
except ImportError:
py = None
def f1():
try:
return f2(10)
except:
return f2(10)
def f2(n):
if n:
f2(n - 1)
else:
return sub.c(f3)
def f3():
return pathlib.Path("./foo").relative_to(pathlib.Path("./bar"))
_DEFAULT_MAXFILENAMELEN = 30
def format_tb_short(tb, *, maxfilenamelen=None, limit=None) -> list[str]:
cwd = os.getcwd()
if maxfilenamelen is None:
maxfilenamelen = _DEFAULT_MAXFILENAMELEN
lines = ["Traceback (most recent call first):\n"]
tbs = [*traceback.walk_tb(tb)]
tbs.reverse()
if limit is not None:
del tbs[limit:]
for f, lineno in tbs:
c = f.f_code
filename = c.co_filename
try:
filename = str(pathlib.Path(filename).relative_to(cwd, walk_up=False))
except ValueError:
pass
filename = filename[-maxfilenamelen:]
lines.append(f" {filename}:{lineno} {c.co_name}\n")
return lines
def format_exception_short(exc: BaseException, *, maxfilenamelen=None, limit=None) -> str:
lines = traceback.format_exception_only(exc)
lines += format_tb_short(
exc.__traceback__, maxfilenamelen=maxfilenamelen, limit=limit
)
return "".join(lines)
def main():
try:
f1()
except Exception as e:
tb = e.__traceback__
print("# stdlib traceback")
s = "".join(traceback.format_exception(e, limit=30, chain=False))
print(s)
print("## in json")
print(json.dumps(s))
if py:
print("# py.code traceback")
exc_info = py.code.ExceptionInfo()
print(f"traceback:\n{exc_info.getrepr(showlocals=False, style='short')}")
print()
print("# short traceback")
s = format_exception_short(e)
print(s)
print("## in json")
print(json.dumps(s))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment