Re:Local Variables as Accidental Breadcrumbs for Faster Debu...
by Sohcahtoa82 on Thursday October 10, 2024 @07:01PM
(#41804444)
> What’s the easiest possible way to cause stacktraces to also dump local variable information? I feel like this is a feature that should be built into the language…
That is built into Python!
import sys
def div(x, y):
return x / y
try:
print(div(1, 0))
except ZeroDivisionError as e:
print("Div by zero!")
print("Locals:")
_, _, tb = sys.exc_info()
print(tb.tb_next.tb_frame.f_locals)
That is built into Python!
This will output: https://stackoverflow.com/a/5328139 has a more thorough implementation that will print you entire stack of local variables.