marrow / cinje

A Pythonic and ultra fast template engine DSL.
MIT License
32 stars 0 forks source link

Adjust exception representation to match original source file. #4

Open amcgregor opened 8 years ago

amcgregor commented 8 years ago

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

amcgregor commented 5 years ago

From SO chat:

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.

See also: