-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
IDLE ignores sys.excepthook in normal, subprocess mode #87174
Comments
Similar to https://bugs.python.org/issue12643 - IDLE ignores sys.excepthook and always prints exceptions. |
I looked at all 4 hooks. sys.breakpointhook is invoked by breakpoint() in user code, so is not IDLE's concern at present. sys.unraiseablehook is invoked by the interpreter when user code messes up badly, so is not IDLE's condern. (And it is an expert feature that beginners should ignore.) sys.displayhook is set to rpc.displayhook in run.py or, if started with -n no subprocess, pyshell.py. IDLE executes user code with exec(). Shell entries are compiled in 'single' mode. Exec invokes displayhook for expression statements so compiled. In both normal and -n mode, displayhook exceptions are displayed and processed as user code exceptions, with the last traceback line giving the bad line. sys.excepthook is invoked by the interpreter for uncaught exceptions. Since, in the subprocess, IDLE catches all user exceptions for its custom handling, there are no uncaught user exceptions. I believe your patch calls the user hook in the proper place, but an uncaught exception in sys.excepthook would be nasty. Replace the call with something like
If possible, I want a unittest added in test_run.py. (I am not sure exactly how as I don't think we have yet directly faked receipt of compiled user code.) class Executive(unittest.TextCase):
def test_good_excepthook(self):
def test_bad_excepthook(self): A blurb is needed (and always is for anything non-trivial). In Doc/library/idle.rst, I want to insert " and reset display and exception handling" before the period ending the current Shell entry. Restart Shell |
Made requested changes, besides the tests. I didn't manage to figure out how to go about testing an excepthook in the first place, let alone a good or bad one. Hope that's not an issue. |
Andre: while I would like friendlier tracebacks available in IDLE without 3rd party installation, I doubt this will happen soon. In the meanwhile, I see that you are working to polish Friendly Traceback with a friendly contest. I presume FT uses sys.excepthook. If so, this issue is opportune for supporting your package. Feel free to test the minimal patch as is, or when fleshed out, and let us know how it works. |
Ken: I will probably work on tests, perhaps as a followup or parallel issue. I will likely do a bit or refactoring to make testing easier. I just looked at print_exception and discovered that the double traceback can likely by handled by properly setting exc.context for the 2nd exception. But getting that right will be aided by having something that already works. Possible manual test protocol in Shell
>>> sys.excepthook = egood
>>> 1/0
# "Exception traceback"
>>> def ebad(a,b,c): z
>>> sys.excepthook = ebad
>>> 1/0
# Double traceback for ZeroDivisionError and NameError |
Terry: I will put it on my todo list. Friendly-traceback can currently work (indirectly) with IDLE; see https://aroberge.github.io/friendly-traceback-docs/docs/html/editor.html I am not sure what benefit this patch will yield when it comes to using Friendly-traceback with IDLE compared with what is currently possible. |
Andre: with this patch, you could potentially add a function or submodule that would point sys.excepthook to a function that would print the exception and then interact before returning. If this worked, one could add, for instance, 'import friendly_traceback.idle' at the top of a file or at a Shell prompt and have friendly-traceback only activate when there is an exception. So it would work in Shell. I hope by 3.10 to have IDLE '>>>' prompts moved into a sidebar (instead of line numbers. F-T prompts would be distinctly different. |
Ken, I read on another issue that an exception in an except suite is automatically linked to the caught exception, though the printed result may not be exactly the same. Once your CLA is 'activated', I will experiment with the options. |
CLA registers as signed now. |
Ken, for next time -- there is a separate News category for IDLE. |
Oops, gotcha. Thanks for doing the tests. I'm a Python contributor! :D |
Terry: as per your earlier comment, I have tried to use Friendly-traceback with the latest version. While it does set the exception hook correctly, it is not useful. Friendly-traceback (FT) needs to have access to the code that was executed. FT's REPL caches the code (similarly to what IPython does, but not CPython) prior to execution so that it can retrieve it correctly, have access to the relevant execution frame, etc. This is not possible when using IDLE's REPL. In fact, for some types of exceptions, when FT tries to get access to the code, it ends up triggering secondary exceptions. However, using the method describe in FT's documentation (with relies on FT's own console) still works correctly. |
IDLE also caches shell input. Off topic here, I sent Andre an email about how FT could access it. |
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: