classification
Title: Discrepancy between traceback.print_exception and sys.__excepthook__
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Timothy McCurrach, iritkatriel
Priority: normal Keywords: patch

Created on 2018-08-22 19:01 by Timothy McCurrach, last changed 2020-11-20 18:57 by iritkatriel.

Pull Requests
URL Status Linked Edit
PR 23427 open iritkatriel, 2020-11-20 18:57
Messages (3)
msg323902 - (view) Author: Timothy McCurrach (Timothy McCurrach) Date: 2018-08-22 19:01
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 `File "<string>", line None`. 

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:
if self.filename or self.lineno:
    etc.

Also, if self.lineno is None, then str(self.lineno) evaluates to "None" and so the ? is never used.
msg380349 - (view) Author: Irit Katriel (iritkatriel) * (Python triager) Date: 2020-11-04 18:46
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
msg381488 - (view) Author: Irit Katriel (iritkatriel) * (Python triager) Date: 2020-11-20 16:32
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:
https://github.com/python/cpython/blob/fb5db7ec58624cab0797b4050735be865d380823/Python/pythonrun.c#L481

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:
---------------------------------------
Running Release|x64 interpreter...
========== lineno is None ==========
traceback.print_exception:
  File "myfile.py", line None
SyntaxError: wrong!
---------------------
sys.excepthook:                   <-- file-lineno missing
SyntaxError: wrong! (myfile.py)
---------------------
========== lineno is 55 ==========
traceback.print_exception:
  File "myfile.py", line 55
SyntaxError: wrong!
---------------------
sys.excepthook:
  File "myfile.py", line 55       <-- file-lineno is there
SyntaxError: wrong!
---------------------
---------------------------------------
History
Date User Action Args
2020-11-20 18:57:15iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22318
2020-11-20 16:32:35iritkatrielsetmessages: + msg381488
2020-11-04 18:46:08iritkatrielsetversions: + Python 3.10
nosy: + iritkatriel

messages: + msg380349

components: + Library (Lib), - Demos and Tools
2018-08-22 19:01:06Timothy McCurrachcreate