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
Discrepancy between traceback.print_exception and sys.__excepthook__ #78644
Comments
If you have set sys.excepthook to some function which calls traceback.print_exception, then I would expect to get identical traceback/exception messages. If you run raise SyntaxError("some message"), then print_exception has the extra line This comes from lines 558-561 of traceback.py: # It was a syntax error; show exactly where the problem was found.
filename = self.filename or "<string>"
lineno = str(self.lineno) or '?'
yield ' File "{}", line {}\n'.format(filename, lineno) Is it expected behaviour that these two functions behave differently, or should there be something like: Also, if self.lineno is None, then str(self.lineno) evaluates to "None" and so the ? is never used. |
Reproduced on 3.10: Running Release|Win32 interpreter...
Python 3.10.0a1+ (heads/exceptionGroup-stage1-dirty:928c211ad8, Oct 28 2020, 14:36:37) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> raise SyntaxError("some message")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SyntaxError: some message
>>> import traceback
>>> import sys
>>> sys.excepthook = traceback.print_exception
>>> raise SyntaxError("some message")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line None
SyntaxError: some message |
After debugging the default excepthook code (C version), I see that the difference is because the SyntaxError's lineno is None. It doesn't mind that the filename is None (it defaults it to <string>) but when it can't get the lineno it gives up here: Line 481 in fb5db7e
If you run this script: --------------------------------------- import traceback
import sys
se = SyntaxError("wrong!")
se.filename = "myfile.py"
print("========== lineno is None ==========")
print('traceback.print_exception:')
traceback.print_exception(type(se), se, None)
print('---------------------')
print('sys.excepthook:')
sys.excepthook(type(se), se, None)
print('---------------------')
se.lineno = 55
print("========== lineno is 55 ==========")
print('traceback.print_exception:')
traceback.print_exception(type(se), se, None)
print('---------------------')
print('sys.excepthook:')
sys.excepthook(type(se), se, None)
print('---------------------') The output is: |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: