Complete user-defined exception handling in IDLE

AndrĂ© Roberge <[email protected]> Sat, 19 Jun 2021 18:36:56 -0300
Newsgroups gmane.comp.python.idle
Message-ID <CAGMu_=paGM82x7pT7mQ-VettaD+o0nKH+9F34c3giDgSMG_sWg@mail.gmail.com>
--===============2807206802029900937==
Content-Type: multipart/alternative; boundary="0000000000006b10a705c5253fec"

--0000000000006b10a705c5253fec
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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! ;-) ]

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

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

  File "<SyntaxError>", line 1
    tau =3D 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 =3D 2pi
                ^

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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=C3=A9 Roberge

--0000000000006b10a705c5253fec
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:arial,he=
lvetica,sans-serif;font-size:small">Hi everyone,</div><div class=3D"gmail_d=
efault" style=3D"font-family:arial,helvetica,sans-serif;font-size:small"><b=
r></div><div class=3D"gmail_default" style=3D"font-family:arial,helvetica,s=
ans-serif;font-size:small">With the latest Python beta version (3.10.0), ID=
LE&#39;s shell now supports custom exception hooks.=C2=A0</div><div class=
=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif;font-siz=
e:small">(<a href=3D"https://docs.python.org/3.10/whatsnew/3.10.html#idle-a=
nd-idlelib">https://docs.python.org/3.10/whatsnew/3.10.html#idle-and-idleli=
b</a>)=C2=A0</div><div class=3D"gmail_default" style=3D"font-family:arial,h=
elvetica,sans-serif;font-size:small">However, this support is incomplete si=
nce when SyntaxErrors are raised for code entered in IDLE&#39;s shell, the =
information is not passed to custom exception hooks.</div><div class=3D"gma=
il_default" style=3D"font-family:arial,helvetica,sans-serif;font-size:small=
"><br></div><div class=3D"gmail_default" style=3D"font-family:arial,helveti=
ca,sans-serif;font-size:small">A while ago, I filed an issue=C2=A0<a href=
=3D"https://bugs.python.org/issue43476">https://bugs.python.org/issue43476<=
/a> suggesting that it would be &quot;nice&quot; to make this possible.=C2=
=A0 I tried on my own, but have not been able to figure out how to do it.=
=C2=A0 However, as noted in the issue, I found a workaround of sorts: addin=
g a single line of code to save the offending code into a specially named f=
ile.=C2=A0 This file can then be accessed like any others allowing someone =
to recreate the SyntaxError and handle it as desired.</div><div class=3D"gm=
ail_default" style=3D"font-family:arial,helvetica,sans-serif;font-size:smal=
l"><br></div><div class=3D"gmail_default" style=3D"font-family:arial,helvet=
ica,sans-serif;font-size:small">For those that are curious to see what it m=
ay look like, I suggest you install &quot;friendly&quot; from PyPI, and add=
 the one line to your Python 3.10 <a href=3D"http://idlelib.pyshell.py">idl=
elib.pyshell.py</a> file as mentioned in the issue linked above.=C2=A0 This=
 is a sample session illustrating what it looks like [it&#39;s more impress=
ive with colours, of course! ;-) ]</div><div class=3D"gmail_default" style=
=3D"font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div =
class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif;fon=
t-size:small">=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</div><di=
v class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif;f=
ont-size:small"><br></div><div class=3D"gmail_default" style=3D"font-family=
:arial,helvetica,sans-serif;font-size:small">&gt;&gt;&gt; from friendly.idl=
e import *<br>&gt;&gt;&gt; install()<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 WARNING<br>Friendly cannot handle SyntaxErrors for code entered in the =
shell.<br>&gt;&gt;&gt; from math import pi<br>&gt;&gt;&gt; tau =3D 2pi<br>S=
yntaxError: invalid decimal literal<br>&gt;&gt;&gt; explain(&quot;syntax&qu=
ot;) =C2=A0# Special friendly command needed to recreate the exception<br><=
br>=C2=A0 File &quot;&lt;SyntaxError&gt;&quot;, line 1<br>=C2=A0 =C2=A0 tau=
 =3D 2pi<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^<br>SyntaxError: invalid de=
cimal literal<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 Perhaps you forgot a multi=
plication operator, 2 * pi.<br>&gt;&gt;&gt; why()<br><br>=C2=A0 =C2=A0 Vali=
d names cannot begin with a number.<br>=C2=A0 =C2=A0 Perhaps you forgot a m=
ultiplication operator, 2 * pi.<br>&gt;&gt;&gt; what()<br><br>=C2=A0 =C2=A0=
 A SyntaxError occurs when Python cannot understand your code.<br>&gt;&gt;&=
gt; where()<br><br>=C2=A0 =C2=A0 Python could not understand the code in th=
e file<br>=C2=A0 =C2=A0 &#39;&lt;SyntaxError&gt;&#39;<br>=C2=A0 =C2=A0 beyo=
nd the location indicated by ^.<br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 --&gt;1:=
 tau =3D 2pi<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^<b=
r></div><div class=3D"gmail_default" style=3D"font-family:arial,helvetica,s=
ans-serif;font-size:small"><br></div><div class=3D"gmail_default" style=3D"=
font-family:arial,helvetica,sans-serif;font-size:small">=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</div><div class=3D"gmail_defa=
ult" style=3D"font-family:arial,helvetica,sans-serif;font-size:small">You c=
an also simply type &quot;Friendly&quot; at a prompt, and a special __repr_=
_ will provide some quick help.=C2=A0 (This is not yet documented.)=C2=A0 A=
nd, of course, Friendly makes uses of the new support for custom exception =
hook for runtime errors.</div><div class=3D"gmail_default" style=3D"font-fa=
mily:arial,helvetica,sans-serif;font-size:small"><br></div><div class=3D"gm=
ail_default" style=3D"font-family:arial,helvetica,sans-serif;font-size:smal=
l">It is possible that I will change explain(&quot;syntax&quot;) to explain=
(&quot;SyntaxError&quot;) in a future version - depending on feedback and a=
ssuming there is some support for a change in IDLE&#39;s code as described.=
</div><div class=3D"gmail_default" style=3D"font-family:arial,helvetica,san=
s-serif;font-size:small"><br></div><div class=3D"gmail_default" style=3D"fo=
nt-family:arial,helvetica,sans-serif;font-size:small">If any of you try it =
out, I&#39;d be really curious to see what you think of this.</div><div cla=
ss=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif;font-s=
ize:small"><br></div><div class=3D"gmail_default" style=3D"font-family:aria=
l,helvetica,sans-serif;font-size:small">More information about friendly and=
 IDLE can be found at=C2=A0<a href=3D"https://aroberge.github.io/friendly-t=
raceback-docs/docs/html/idle_repl.html">https://aroberge.github.io/friendly=
-traceback-docs/docs/html/idle_repl.html</a></div><div class=3D"gmail_defau=
lt" style=3D"font-family:arial,helvetica,sans-serif;font-size:small"><br></=
div><div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-=
serif;font-size:small">Cheers,</div><div class=3D"gmail_default" style=3D"f=
ont-family:arial,helvetica,sans-serif;font-size:small">Andr=C3=A9 Roberge</=
div></div>

--0000000000006b10a705c5253fec--

--===============2807206802029900937==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
IDLE-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/idle-dev

--===============2807206802029900937==--