Skip to content

Instantly share code, notes, and snippets.

@cknd
Last active May 14, 2024 21:44
Show Gist options
  • Save cknd/9f769b09483e42eb00701a22765be0dc to your computer and use it in GitHub Desktop.
Save cknd/9f769b09483e42eb00701a22765be0dc to your computer and use it in GitHub Desktop.
super verbose python tracebacks
import sys
import traceback
import linecache
def printlocals(frame, truncate=500, truncate__=True):
sep = '.' * 50
msg = ' %s\n' % sep
for name, value in sorted(frame.f_locals.items()):
if hasattr(value, '__repr__'):
try:
val_str = value.__repr__()
except:
val_str = "<error when calling __repr__>"
else:
try:
val_str = str(value)
except:
val_str = "<error calling str()>"
if truncate and len(val_str) > truncate:
val_str = "%s..." % val_str[:truncate]
if truncate__ and name.startswith('__') and len(val_str) > 50:
val_str = "%s..." % val_str[:50]
val_str = val_str.replace('\n', '\n %s' % (' ' * (len(name) + 2)))
msg += " %s = %s\n" % (name, val_str)
msg += ' %s' % sep
return msg
def tb2string(tb, context=5):
frame_strings = []
while tb:
frame = tb.tb_frame
filename, name = (frame.f_code.co_filename, frame.f_code.co_name)
lineno = tb.tb_lineno - 1
msg = "%s in %s\n" % (filename, frame.f_code.co_name)
lines = linecache.getlines(filename)
start = max(lineno - context, 0)
stop = lineno + 1
for line_idx in range(start, stop+1):
if line_idx == lineno:
line_str = "--> %s " % line_idx
else:
line_str = " %s " % line_idx
try:
line_str += lines[line_idx]
except IndexError:
line_str += '<error reading line>'
msg += line_str
msg += printlocals(frame)
frame_strings.append(msg)
tb = tb.tb_next
return '\n\n\n'.join(frame_strings)
def format_traceback(tb, etype=None, evalue=None):
msg = tb2string(tb)
if etype is not None and evalue is not None:
exc_str = ' '.join(traceback.format_exception_only(etype, evalue))
msg += '\n\n' + exc_str
return msg
if __name__ == '__main__':
import numpy as np
np.set_printoptions(linewidth=70)
rng = np.random.RandomState()
def a_broken_function(foo):
list_of_things = [foo, foo]
somestring = "yet another string"
somearray = rng.randint(0,9,size=(100,100))
for k in range(10):
if k == 9:
raise Exception('hello!')
def some_function(boing):
foo = boing + "world"
a_broken_function(foo)
try:
some_function("hello")
except:
etype, exc, tb = sys.exc_info()
print(format_traceback(tb, etype, exc))

Extra verbose python traceback formatting with current variable values (because sometimes there's no debugger, only a log file)

Before:

Traceback (most recent call last):
  File "tracebacks.py", line 85, in <module>
    some_function("hello")
  File "tracebacks.py", line 82, in some_function
    a_broken_function(foo)
  File "tracebacks.py", line 78, in a_broken_function
    raise Exception('hello!')
Exception: hello!

After:

(...)

/home/.../projects/tracebacks/tracebacks.py in some_function
    76             if k == 9:
    77                 raise Exception('hello!')
    78 
    79     def some_function(boing):
    80         foo = boing + "world"
--> 81         a_broken_function(foo)
    82 
    ..................................................
       boing = 'hello'
       foo = 'helloworld'
    ..................................................


/home/.../projects/tracebacks/tracebacks.py in a_broken_function
    72         list_of_things = [foo, foo]
    73         somestring = "yet another string"
    74         somearray = rng.randint(0,9,size=(100,100))
    75         for k in range(10):
    76             if k == 9:
--> 77                 raise Exception('hello!')
    78 
    ..................................................
       foo = 'helloworld'
       k = 9
       list_of_things = ['helloworld', 'helloworld']
       somearray = array([[6, 4, 1, ..., 0, 4, 6],
                          [5, 7, 0, ..., 4, 3, 2],
                          [8, 6, 8, ..., 3, 8, 1],
                          ...,
                          [4, 1, 0, ..., 0, 4, 7],
                          [6, 5, 1, ..., 5, 6, 4],
                          [0, 4, 5, ..., 6, 6, 1]])
       somestring = 'yet another string'
    ..................................................

Exception: hello!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment