-
Notifications
You must be signed in to change notification settings - Fork 18
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
TracebackException subclass #4
Comments
|
Yes. I think really what I want is an API compatible version of the standard library My use-case is I have existing code that uses the Is anything in the existing API focused on formatting and printing output? All the examples in the README show manual formatting, which is lower level than what I want. I don't see anything that can print a traceback, for instance, but I may have missed it. |
No, there's nothing to format for you because there's no obviously correct way to format a traceback, especially when there's so much data available. For example IPython has one style, stackprinter has another, and traceback has another. Many libraries format tracebacks in HTML. The traceback module has it easy because there's a single standard format for tracebacks in Python so it just has to provide exactly that. And it displays so little information that no one needs to vary it anyway.
What would the output look like? I honestly think you should take the code in ipython/ipython#11827 (comment), tweak it to your preferences, and use that. |
The traceback module gives you the API necessary if you want to modify how the tracebacks look. So for instance if you want to show more than one line of context, you can do that. The API is not the greatest for that once you really want to get picky on how things look. Most people don't want to rewrite the entire traceback formatting from scratch like you've done at ipython/ipython#11827 (comment). If something has reasonable defaults they will want to use that. You can have different styles, like standard Python style, IPython style, HTML, and so on. You can make it extensible so that people can change how it looks, but defaults are important.
I would suggest for the default output to look as similar to the default tracebacks as possible. For instance, say you had something like def test():
return 1/0
print(test()) which normally gives Traceback (most recent call last):
File "test.py", line 4, in <module>
print(test())
File "test.py", line 2, in test
return 1/0
ZeroDivisionError: division by zero A reasonable extension of that would be Traceback (most recent call last):
File "test.py", line 4, in <module>
print(test())
^^^^^^
File "test.py", line 2, in test
return 1/0
^^^
ZeroDivisionError: division by zero For cases where the columns are not on the printed line it could either only show the parts that are on the given line, or show multiple lines (either would be a reasonable default IMO). Ultimately, if you want people to use this, you need higher level tools that they can use as drop-in replacements for existing tooling. |
OK, I guess you're right. I've started an implementation in the import json
import stack_data
formatter = stack_data.Formatter(
options=stack_data.Options(before=0, after=0),
show_linenos=False,
current_line_indicator=False,
)
try:
json.loads(" s s")
except:
formatter.print_exception() |
Nice, it looks good. I'll need to try playing with the API a to see if I can integrate it into my existing uses. One suggestion so far, is to use |
https://www.python.org/dev/peps/pep-0657/ (targeted for Python 3.11) might contain some interesting information. The latest beta (3.10.0b1) does have some extended highlighting for SyntaxErrors. The exception argument can have two additional elements ( |
It would be useful to have a TracebackException subclass that added column information, perhaps also with a default
format
override. That way existing code that uses thetraceback
module could easily swap it out for something that shows column information. It might also be useful to have versions of the various functions in thetraceback
module that used this.The text was updated successfully, but these errors were encountered: