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

Adjust exception representation to match original source file. #4

Open
amcgregor opened this issue Oct 8, 2015 · 1 comment
Open

Comments

@amcgregor
Copy link
Member

amcgregor commented Oct 8, 2015

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 amcgregor modified the milestone: Eiger (Feature Release, 1.1) Oct 8, 2015
amcgregor added a commit that referenced this issue Dec 6, 2015
amcgregor added a commit that referenced this issue Dec 6, 2015
Capture and preserve source line numbers.

Now ready for application in #4 and #12.
@amcgregor
Copy link
Member Author

amcgregor commented Feb 19, 2019

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant