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: code.InteractiveConsole can crash if sys.excepthook is broken
Type: behavior Stage: test needed
Components: Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Carl.Friedrich.Bolz, Julian, terry.reedy, vstinner
Priority: normal Keywords:

Created on 2021-02-07 16:26 by Carl.Friedrich.Bolz, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
crash.log Carl.Friedrich.Bolz, 2021-02-07 16:26
Messages (3)
msg386593 - (view) Author: Carl Friedrich Bolz-Tereick (Carl.Friedrich.Bolz) * Date: 2021-02-07 16:26
When using code.InteractiveConsole to implement a Python shell (like PyPy is doing), having a broken sys.excepthook set can crash the console (see attached terminal log). Instead, it should catch errors and report then ignore them (using sys.unraisablehook I would think, but right now it's not that simple to call unraisablehook from python code).

Related to https://bugs.python.org/issue43148
msg386892 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-02-13 00:00
unraiseable hook is not needed.  The REPL does:

>>> sys.excepthook = lambda: None
>>> 1/0
Error in sys.excepthook:
TypeError: <lambda>() takes 0 positional arguments but 3 were given

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> (no crash)
----------------------------------------------------

code.Console in the REPL currently does:

>>> import sys
>>> sys.excepthook = 1
>>> arsdfsd
Error in sys.excepthook:
TypeError: 'int' object is not callable

Original exception was:
Traceback (most recent call last):
  File "F:\dev\3x\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
NameError: name 'arsdfsd' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\dev\3x\lib\code.py", line 301, in interact
    console.interact(banner, exitmsg)
  File "F:\dev\3x\lib\code.py", line 232, in interact
    more = self.push(line)
  File "F:\dev\3x\lib\code.py", line 258, in push
    more = self.runsource(source, self.filename)
  File "F:\dev\3x\lib\code.py", line 74, in runsource
    self.runcode(code)
  File "F:\dev\3x\lib\code.py", line 94, in runcode
    self.showtraceback()
  File "F:\dev\3x\lib\code.py", line 148, in showtraceback
    sys.excepthook(ei[0], ei[1], last_tb)
TypeError: 'int' object is not callable
>>> (code console crashes, evidenced by 'sys' not recognized
--------------------------------------------------------------
IDLE in its normal 2-process mode, since 3 weeks ago, does:

>>> sys.excepthook = lambda: None
>>> 1/0
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    1/0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Programs\Python310\lib\idlelib\run.py", line 576, in runcode
    sys.excepthook(*self.user_exc_info)
TypeError: <lambda>() takes 0 positional arguments but 3 were given
>>> (no crash)

In IDLE's 1-process mode, selected with -n command line option, the hook exception has several IDLE specific lines added, as with the code exception.  A return or two is needed to get >>> back.
------------------------------------------------------------------------

In any case, both the immediate code exception and the defective hook exception are printed.  The recent patch to IDLE, PR-24302, was to replace 'print_exception()' (ignoring excepthook) with

            if sys.excepthook is sys.__excepthook__:
                print_exception()
            else:
                try:
                    sys.excepthook(*self.user_exc_info)
                except:
                    print_exception()

I suggest that the code line 148, call to sys.excepthook be wrapped similarly (adjusted to write the lines if sys.excepthook fails).

IDLE's exception order is determine by the default context annotation.  I don't know what the REPL does to change the output, but I prefer the default.  I will prepare a PR.

code tests are in test.test_code_module because test_code tests code objects.
msg390108 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-04-02 22:33
code.InteractiveInterpreter handles SyntaxErrors separately in showsyntaxerror rather than showtraceback.  The same problem arises with a bad excepthook in line 129.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87320
2021-04-02 22:33:56terry.reedysetmessages: + msg390108
2021-02-13 00:00:51terry.reedysetversions: + Python 3.10
nosy: + terry.reedy

messages: + msg386892

type: behavior
stage: test needed
2021-02-11 10:38:00vstinnersetnosy: + vstinner
2021-02-07 16:33:29Juliansetnosy: + Julian
2021-02-07 16:26:30Carl.Friedrich.Bolzcreate