This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Discrepancy between traceback.print_exception and sys.__excepthook__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Timothy McCurrach, iritkatriel, lukasz.langa, miss-islington, taleinat
Priority: normal Keywords: patch

Created on 2018-08-22 19:01 by Timothy McCurrach, last changed 2022-04-11 14:59 by admin. This issue is now closed.

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 merged iritkatriel, 2020-12-22 22:48
Messages (6)
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 committer) 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 committer) 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
msg391979 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-04-26 19:42
New changeset 727bed675f1a47d12492efba0ca118707502d988 by Irit Katriel in branch '3.8':
[3.8] bpo-34463: Make python tracebacks identical to C tracebacks for (#23899)
https://github.com/python/cpython/commit/727bed675f1a47d12492efba0ca118707502d988
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78644
2021-04-27 06:57:02iritkatrielsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-04-26 19:42:48lukasz.langasetnosy: + lukasz.langa
messages: + msg391979
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