Skip to content
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

code.InteractiveConsole can crash if sys.excepthook is broken #87320

Closed
cfbolz mannequin opened this issue Feb 7, 2021 · 4 comments
Closed

code.InteractiveConsole can crash if sys.excepthook is broken #87320

cfbolz mannequin opened this issue Feb 7, 2021 · 4 comments
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@cfbolz
Copy link
Mannequin

cfbolz mannequin commented Feb 7, 2021

BPO 43154
Nosy @terryjreedy, @vstinner, @cfbolz, @Julian
Files
  • crash.log
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2021-02-07.16:26:30.537>
    labels = ['type-bug', '3.10']
    title = 'code.InteractiveConsole can crash if sys.excepthook is broken'
    updated_at = <Date 2021-04-02.22:33:56.780>
    user = 'https://github.com/cfbolz'

    bugs.python.org fields:

    activity = <Date 2021-04-02.22:33:56.780>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2021-02-07.16:26:30.537>
    creator = 'Carl.Friedrich.Bolz'
    dependencies = []
    files = ['49797']
    hgrepos = []
    issue_num = 43154
    keywords = []
    message_count = 3.0
    messages = ['386593', '386892', '390108']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'vstinner', 'Carl.Friedrich.Bolz', 'Julian']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'test needed'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue43154'
    versions = ['Python 3.10']

    Linked PRs

    @cfbolz
    Copy link
    Mannequin Author

    cfbolz mannequin commented Feb 7, 2021

    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

    @terryjreedy
    Copy link
    Member

    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.

    @terryjreedy terryjreedy added 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Feb 13, 2021
    @terryjreedy
    Copy link
    Member

    code.InteractiveInterpreter handles SyntaxErrors separately in showsyntaxerror rather than showtraceback. The same problem arises with a bad excepthook in line 129.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel added 3.11 only security fixes 3.12 bugs and security fixes and removed 3.10 only security fixes labels Apr 5, 2023
    @iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 24, 2023
    @cfbolz
    Copy link
    Contributor

    cfbolz commented Jul 29, 2024

    This issue is a little bit more pressing now with pyrepl, because that is based on the code module. Here's Python 3.12:

    Python 3.12.0 (main, Apr 28 2024, 22:42:26) [GCC 13.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys; sys.excepthook = object(); asdf
    Error in sys.excepthook:
    TypeError: 'object' object is not callable
    
    Original exception was:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'asdf' is not defined

    And Python 3.13 (I tried on current 3.13 branch, 10cf7d6):

    >>> import sys; sys.excepthook = object(); asdf
    Error in sys.excepthook:
    TypeError: 'object' object is not callable
    
    Original exception was:
    Traceback (most recent call last):
      File "/home/cfbolz/projects/cpython/Lib/code.py", line 91, in runcode
        exec(code, self.locals)
        ~~~~^^^^^^^^^^^^^^^^^^^
      File "<python-input-0>", line 1, in <module>
        import sys; sys.excepthook = object(); asdf
                                               ^^^^
    NameError: name 'asdf' is not defined
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<frozen runpy>", line 198, in _run_module_as_main
      File "<frozen runpy>", line 88, in _run_code
      File "/home/cfbolz/projects/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
        __pyrepl_interactive_console()
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
      File "/home/cfbolz/projects/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
        run_multiline_interactive_console(console)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
      File "/home/cfbolz/projects/cpython/Lib/_pyrepl/simple_interact.py", line 156, in run_multiline_interactive_console
        more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")  # type: ignore[call-arg]
      File "/home/cfbolz/projects/cpython/Lib/code.py", line 303, in push
        more = self.runsource(source, filename, symbol=_symbol)
      File "/home/cfbolz/projects/cpython/Lib/_pyrepl/console.py", line 200, in runsource
        self.runcode(code)
        ~~~~~~~~~~~~^^^^^^
      File "/home/cfbolz/projects/cpython/Lib/code.py", line 95, in runcode
        self.showtraceback()
        ~~~~~~~~~~~~~~~~~~^^
      File "/home/cfbolz/projects/cpython/Lib/_pyrepl/console.py", line 168, in showtraceback
        super().showtraceback(colorize=self.can_colorize)
        ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/cfbolz/projects/cpython/Lib/code.py", line 153, in showtraceback
        sys.excepthook(ei[0], ei[1], last_tb)
        ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: 'object' object is not callable

    @vstinner vstinner added the topic-repl Related to the interactive shell label Jul 29, 2024
    cfbolz added a commit to cfbolz/cpython that referenced this issue Jul 30, 2024
    serhiy-storchaka pushed a commit that referenced this issue Jul 31, 2024
    …ook (GH-122456)
    
    Before, the exception caused by calling non-default sys.excepthook
    in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 31, 2024
    …xcepthook (pythonGH-122456)
    
    Before, the exception caused by calling non-default sys.excepthook
    in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
    (cherry picked from commit bd3d31f)
    
    Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
    serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this issue Jul 31, 2024
    …n sys.excepthook (pythonGH-122456)
    
    Before, the exception caused by calling non-default sys.excepthook
    in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
    (cherry picked from commit bd3d31f)
    
    Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
    @serhiy-storchaka serhiy-storchaka added 3.13 bugs and security fixes 3.14 new features, bugs and security fixes and removed 3.11 only security fixes labels Jul 31, 2024
    serhiy-storchaka added a commit that referenced this issue Jul 31, 2024
    …excepthook (GH-122456) (GH-122515)
    
    Before, the exception caused by calling non-default sys.excepthook
    in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
    (cherry picked from commit bd3d31f)
    
    Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
    serhiy-storchaka pushed a commit that referenced this issue Aug 8, 2024
    …excepthook (GH-122456) (GH-122514)
    
    Before, the exception caused by calling non-default sys.excepthook
    in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
    (cherry picked from commit bd3d31f)
    
    Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
    blhsing pushed a commit to blhsing/cpython that referenced this issue Aug 22, 2024
    …xcepthook (pythonGH-122456)
    
    Before, the exception caused by calling non-default sys.excepthook
    in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants