You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking for ideas on how to do this one. Stop-gap measure: include a "source line number" comment on every generated line originating in the source file? We are decorating each produced function, so in theory we can actually grab the exception itself and manipulate it. Comments are stripped from bytecode in optimized execution modes, so we'd need to store line number mapping somewhere else…
Not counting, like, SyntaxErrors that say the problem is on the line after the actual problematic line
You can't catch those (from within the same source file) anyway.
Bah, e.__traceback__.tb_lineno is readonly
Maybe instead of changing the exception object, you could write your own traceback display logic. I wonder if the traceback module would be useful there.
Then you only need to do try: whole_program_goes_here() \n except Exception as e: print_custom_traceback(e)
implementing print_custom_traceback left as an exercise to the reader
@amcgregor I don't think you'd have to write your own from the ground up, if that's what you're worried about. I'm pretty sure there's some way to get a list of (filename, lineno, name, line) tuples corresponding to the real traceback, which you can then modify, and then pass to some kind of formatting method to get authentic-looking output. traceback.extract_stack and traceback.format_list look promising. I haven't actually tried using them though.
Looking for ideas on how to do this one. Stop-gap measure: include a "source line number" comment on every generated line originating in the source file? We are decorating each produced function, so in theory we can actually grab the exception itself and manipulate it. Comments are stripped from bytecode in optimized execution modes, so we'd need to store line number mapping somewhere else…
External References
The text was updated successfully, but these errors were encountered: