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

Terry Reedy tjreedy at udel.edu
Sat Jun 19 22:36:14 EDT 2021


On 6/19/2021 5:36 PM, André Roberge wrote:
> 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 
> <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 
> <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.

I gave more feedback on the issue.  IDLE already has a bit of code for a 
3rd party package, V-IDLE?, which I am not sure is still current.

> 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 <http://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 *

I hope you document the alternative for anyone wanting to isolate f.i 
functions: `import friendly.idle as fi` or
`from friendly import idle as fi`.

In any case, fi executes in the user process.  Do you use the rpc 
connection to query linecache in the IDLE process?

>  >>> 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
On the issue, I suggested de-serializing the original rather than 
recompiling.
>    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.

I think it is fine as is.

> 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 
> <https://aroberge.github.io/friendly-traceback-docs/docs/html/idle_repl.html>




More information about the IDLE-dev mailing list