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 ignores sys.excepthook #56852
Comments
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. |
What do you suggest? |
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. |
Ok, I was not sure that I understood correctly. This change should be an option to not break existing code. |
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. |
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/. |
Another possibility yet is to add an "excepthook" argument to the InteractiveInterpreter / InteractiveConsole constructors. |
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. |
New patch to address comments provide by Nick Coghlan in person at PyCon-AU 2012. |
New changeset a812de69b493 by Nick Coghlan in branch 'default': |
Is it finished or not? |
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+ |
I'm ok with current status. |
3.2 is now dead and it's probably not worth to backport tests to 2.7 in 2016 :) Closing this as 'fixed'. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: