Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[errors-] options.debug_stacktrace_full for more callstack #2586

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion visidata/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traceback
import re

from visidata import vd, VisiData

Expand All @@ -11,8 +12,23 @@ class ExpectedException(Exception):


def stacktrace(e=None):
'''Return a list of strings. Includes extra callstack levels above the
level where the exception was technically caught, to aid debugging.'''

if not e:
return traceback.format_exc().strip().splitlines()
stack = ''.join(traceback.format_stack()).strip().splitlines()
trim_levels = 3 # calling function -> stacktrace() -> format_stack()
trace_above = stack[:-2*trim_levels]
trace_above[0] = ' ' + trace_above[0] #fix indent level of first line
try:
# remove several levels of uninformative stacktrace in typical interactive vd
idx = trace_above.index(' ret = vd.mainloop(scr)')
trace_above = trace_above[idx+1:]
except ValueError:
pass
# remove lines that mark error columns with carets and sometimes tildes
trace_below = [ line for line in traceback.format_exc().strip().splitlines() if not re.match('^ *~*\\^+$', line) ]
return [trace_below[0]] + trace_above + trace_below[1:]
return traceback.format_exception_only(type(e), e)


Expand Down