[Idle-dev] Complete user-defined exception handling in IDLE

André Roberge andre.roberge at gmail.com
Sat Jun 19 17:36:56 EDT 2021


Hi everyone,

With the latest Python beta version (3.10.0), IDLE's shell now supports
custom exception hooks.
(https://docs.python.org/3.10/whatsnew/3.10.html#idle-and-idlelib)
However, this support is incomplete since when SyntaxErrors are raised for
code entered in IDLE's shell, the information is not passed to custom
exception hooks.

A while ago, I filed an issue https://bugs.python.org/issue43476 suggesting
that it would be "nice" to make this possible.  I tried on my own, but have
not been able to figure out how to do it.  However, as noted in the issue,
I found a workaround of sorts: adding a single line of code to save the
offending code into a specially named file.  This file can then be accessed
like any others allowing someone to recreate the SyntaxError and handle it
as desired.

For those that are curious to see what it may look like, I suggest you
install "friendly" from PyPI, and add the one line to your Python 3.10
idlelib.pyshell.py file as mentioned in the issue linked above.  This is a
sample session illustrating what it looks like [it's more impressive with
colours, of course! ;-) ]

==========================================

>>> from friendly.idle import *
>>> install()
                                WARNING
Friendly cannot handle SyntaxErrors for code entered in the shell.
>>> from math import pi
>>> tau = 2pi
SyntaxError: invalid decimal literal
>>> explain("syntax")  # Special friendly command needed to recreate the
exception

  File "<SyntaxError>", line 1
    tau = 2pi
          ^
SyntaxError: invalid decimal literal

        Perhaps you forgot a multiplication operator, 2 * pi.
>>> why()

    Valid names cannot begin with a number.
    Perhaps you forgot a multiplication operator, 2 * pi.
>>> what()

    A SyntaxError occurs when Python cannot understand your code.
>>> where()

    Python could not understand the code in the file
    '<SyntaxError>'
    beyond the location indicated by ^.

    -->1: tau = 2pi
                ^

=====================
You can also simply type "Friendly" at a prompt, and a special __repr__
will provide some quick help.  (This is not yet documented.)  And, of
course, Friendly makes uses of the new support for custom exception hook
for runtime errors.

It is possible that I will change explain("syntax") to
explain("SyntaxError") in a future version - depending on feedback and
assuming there is some support for a change in IDLE's code as described.

If any of you try it out, I'd be really curious to see what you think of
this.

More information about friendly and IDLE can be found at
https://aroberge.github.io/friendly-traceback-docs/docs/html/idle_repl.html

Cheers,
André Roberge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/idle-dev/attachments/20210619/04399e10/attachment.html>


More information about the IDLE-dev mailing list