Skip to content

code.InteractiveConsole ignores sys.excepthook #56852

Closed

Description

@aliles
mannequin
BPO 12643
Nosy @ncoghlan, @pitrou, @vstinner, @asvetlov, @florentx, @berkerpeksag
Files
  • console.py: Demonstrate sys.excepthook behaviour from code.InteractiveConsole
  • startup.py
  • issue12643.diff: Add unit tests. Call sys.excepthook if overridden
  • issue12643_2.diff: Updated patch to address feedback
  • 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 = <Date 2016-09-12.01:49:27.894>
    created_at = <Date 2011-07-27.12:10:06.983>
    labels = ['type-bug', 'library']
    title = 'code.InteractiveConsole ignores sys.excepthook'
    updated_at = <Date 2016-09-12.01:49:27.677>
    user = 'https://bugs.python.org/aliles'

    bugs.python.org fields:

    activity = <Date 2016-09-12.01:49:27.677>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-09-12.01:49:27.894>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2011-07-27.12:10:06.983>
    creator = 'aliles'
    dependencies = []
    files = ['22771', '22773', '26920', '26928']
    hgrepos = []
    issue_num = 12643
    keywords = ['patch']
    message_count = 14.0
    messages = ['141222', '141224', '141225', '141226', '141227', '141367', '141369', '168635', '168652', '168656', '168662', '168665', '168666', '275909']
    nosy_count = 8.0
    nosy_names = ['ncoghlan', 'pitrou', 'vstinner', 'asvetlov', 'flox', 'python-dev', 'aliles', 'berker.peksag']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue12643'
    versions = ['Python 3.3']

    Activity

    1. aliles commented on Jul 27, 2011

      alilesmannequin
      MannequinAuthor

      code.InteractiveConsole doesn't match the CPython interactive interpreter with respect to allowing sys.excepthook to handle exceptions. Unlike the interactive interpreter, replacing sys.excepthook with an alternate function will not change exception handling behaviour from inside a code.InteractiveConole

      This affects downstream interpreters such as PyPy.
      https://bugs.pypy.org/issue811

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jul 27, 2011
    3. vstinner commented on Jul 27, 2011

      @vstinner
      Member

      What do you suggest?

    4. aliles commented on Jul 27, 2011

      alilesmannequin
      MannequinAuthor

      OK. Not something I was expecting to be asked. But cool :D

      If it's not possible for InteractiveConsole to allow exceptions to propagate through sys.excepthook. (I assume they are being caught in a try: catch: block) check sys.excepthook for a custom exception handler and call present.

      As InteractiveConsole is supposed to "Closely emulate the behaviour of the interactive Python interpreter", any existing code that replies on it's current could be viewed as relying on a bug.

      I'd be happy to attempt a patch if it's worthwhile preparing one.

    5. vstinner commented on Jul 27, 2011

      @vstinner
      Member

      If it's not possible for InteractiveConsole to allow exceptions
      to propagate through sys.excepthook.

      Ok, I was not sure that I understood correctly. This change should be an option to not break existing code.

    6. aliles commented on Jul 27, 2011

      alilesmannequin
      MannequinAuthor

      Yes. I have code that behaves differently under CPython and PyPy because of this issue.

      Admittedly that code is somewhat evil. It's attached for reference, in particular the __HelpSyntax class.

    7. pitrou commented on Jul 29, 2011

      @pitrou
      Member

      If someone is setting sys.excepthook, they are not expecting it to be ignored, so I think there's no reason to preserve existing behaviour. On the other hand, it is easy to customize exception printout by subclassing InteractiveConsole and overriding the showtraceback() method.

      aliles, if you want to work on a patch, we have a developer's guide at http://docs.python.org/devguide/.

    8. pitrou commented on Jul 29, 2011

      @pitrou
      Member

      Another possibility yet is to add an "excepthook" argument to the InteractiveInterpreter / InteractiveConsole constructors.

    9. aliles commented on Aug 20, 2012

      alilesmannequin
      MannequinAuthor

      Submitted patch. Patch creates a new unit test suite with basic unit tests for InteractiveConsole. Enhances InteractiveConsole to call sys.excepthook instead of it's own handler if the user has overridden the excepthook.

      The unit tests use the new unittest.mock module and ExitStack class for contextlib.

    10. aliles commented on Aug 20, 2012

      alilesmannequin
      MannequinAuthor

      New patch to address comments provide by Nick Coghlan in person at PyCon-AU 2012.

    11. python-dev commented on Aug 20, 2012

      python-devmannequin
      Mannequin

      New changeset a812de69b493 by Nick Coghlan in branch 'default':
      Issue bpo-12643: Respect sys.excepthook in code.InteractiveConsole
      http://hg.python.org/cpython/rev/a812de69b493

    12. asvetlov commented on Aug 20, 2012

      @asvetlov
      Contributor

      Is it finished or not?
      Also looks like patch applied to 3.3 while issue marked for 2.7 and 3.2 also.

    13. ncoghlan commented on Aug 20, 2012

      @ncoghlan
      Contributor

      The patch also adds a test suite for the code module, in order to demonstrate that the issue is actually fixed.

      However, due to the interactive nature of the component, the new tests rely on unittest.mock. Backporting the test suite would mean eliminating that dependency (which is doable, but somewhat tedious).

      So we can either leave the issue open, indicating that a backport of the tests and change to 2.7 (and perhaps 3.2) is still acceptable, or else declare that we aren't going to worry about that and just leave this as something that is only supported properly in 3.3+

    14. asvetlov commented on Aug 20, 2012

      @asvetlov
      Contributor

      I'm ok with current status.
      Thanks for clarification.

    15. berkerpeksag commented on Sep 12, 2016

      @berkerpeksag
      Member

      3.2 is now dead and it's probably not worth to backport tests to 2.7 in 2016 :) Closing this as 'fixed'.

    16. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions