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, miss-islington, taleinat
Priority: normal Keywords: patch

Created on 2018-08-22 19:01 by Timothy McCurrach, last changed 2020-12-22 22:48 by iritkatriel.

Pull Requests
URL Status Linked Edit
PR 23427 merged iritkatriel, 2020-11-20 18:57
PR 23895 merged miss-islington, 2020-12-22 19:53
PR 23899 open iritkatriel, 2020-12-22 22:48
Messages (5)
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!
---------------------
---------------------------------------
msg383610 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-12-22 19:53
New changeset 069560b1171eb6385121ff3b6331e8814a4e7454 by Irit Katriel in branch 'master':
bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)
https://github.com/python/cpython/commit/069560b1171eb6385121ff3b6331e8814a4e7454
msg383620 - (view) Author: miss-islington (miss-islington) Date: 2020-12-22 22:12
New changeset 8e5c61a075f3a60272a7dcdc48db200090d76309 by Miss Islington (bot) in branch '3.9':
bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)
https://github.com/python/cpython/commit/8e5c61a075f3a60272a7dcdc48db200090d76309
History
Date User Action Args
2020-12-22 22:48:55iritkatrielsetpull_requests: + pull_request22754
2020-12-22 22:12:38miss-islingtonsetmessages: + msg383620
2020-12-22 19:53:26miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22750
2020-12-22 19:53:19taleinatsetnosy: + taleinat
messages: + msg383610
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