[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Another idea: do what test_exceptions() does:

try:
f()
except NameError as exc:
with support.captured_stderr() as err:
sys.__excepthook__(*sys.exc_info())

self.assertNotIn("a1", err.getvalue())

Then instead of the assert, do something like

last_line = err.getvalue().rsplit("\n", 2)[-2]
_, did_you_mean, suggestion = last_line.rpartition("Did you mean: ")
if did_you_mean:
   print(did_you_mean + suggestion)

This can probably be done without test.support.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

Those are intesting options, I will think about this. I am still afraid of 
adding more APIs in this area as having arbitrary Python getting call in the 
middle of handling the exceptions it can be quite tricky: think that we are 
handling an exception already so other exceptions can mess up stuff in very 
subtle ways or we need to ignore exceptions and that can also be dangerous.

Another thing we could do is reproduce the feature in idle itself with a custom 
display hook, which would be easier as we can just use difflib. It would be 
slightly different than the built-in one but honestly I don't think that's a 
problem at all. I understand that other people may think differently so I am 
open minded as well. But I wanted to clarify what are my worries :)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

I realized after posting that looking for close matching is a performance issue 
and does not need to be done unless the exception is going to be displayed.  
But it is a shame to keep such great work away from many users.

What would you think of either

1. add sys.tracebackhook, to be called by the traceback display functions if 
not None.  Then idlelib.run.print_exception would not have to duplicate the 
sometimes changing logic for chaining or otherwise joining multiple tracebacks 
into one mega-traceback.  Instead, from what Dennis said above, it could set 
the hook and call the built-in exceptionhook.

2. add a new optional parameter to exceptionhook and the traceback display 
function.  This would have the same advantage as above.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

I don't feel comfortable adding a public API to get the message either in the 
traceback or the exception, at least for the time being.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

> Pablo, I checked an AttributeError instance and the missing phrase is not 
> present

This cannot be included in the AttributeError because of performance reasons. 
These errors will be thrown naturally all over the place without this meaning 
that the interpreter will finish so we can only compute these safely on the 
except hook. 

We also discarded mutating the attribute error because it can make some stuff 
crash if the exception happens on a thread and that doesn't imply full program 
termination.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

(Shreyan, the fake filenames are not relevant.)

By default, sys.excepthook is sys.__excepthook is .  So by default IDLE runcode calls print_exception, which call 
cleanup_traceback for each traceback printed.  This function makes  two 
important changes to the traceback.

First, it removes traceback lines that are not present when running in the 
C-coded REPL, because IDLE does some functions with Python code.  The result is 
that IDLE tracebacks nearly always equal REPL tracebacks.

Second, for Shell code, IDLE add cached code lines.  Compare

>>> a = b
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'b' is not defined

to

>>> a = b
Traceback (most recent call last):
  File "", line 1, in 
a = b
NameError: name 'b' is not defined
>>> 

We are not going to stop using print_exception.  Note that it is normal for 
IDEs to modify tracebacks.


The 'is' is false when user change excepthook.  The false branch was recently 
added to run user excepthooks.

Pablo, I checked an AttributeError instance and the missing phrase is not 
present.  Why is it hidden from Python code.  Why not add the rest of the 
message to the message?  Or at least add it to the tuple or an attribute.  Or 
add a .get_suggestion method to exception objects, like .with_traceback.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Shreyan Avigyan


Shreyan Avigyan  added the comment:

Does the test suite run succesfully?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Indeed, this change enables the feature for IDLE, though I'm not sure what it 
breaks.

diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py
index 07e9a2bf9c..319b16f311 100644
--- a/Lib/idlelib/run.py
+++ b/Lib/idlelib/run.py
@@ -569,7 +569,7 @@ def runcode(self, code):
 self.user_exc_info = sys.exc_info()  # For testing, hook, viewer.
 if quitting:
 exit()
-if sys.excepthook is sys.__excepthook__:
+if False:
 print_exception()
 else:
 try:

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

It looks like Lib/idlelib/run.py : print_exception() re-implements the 
traceback, rather than relying on sys.excepthook

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

PyErr_Display() grabs the current sys.stderr and writes to that, but it looks 
like IDLE never gets to call PyErr_Display().

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

I'm not sure if this helps, but this is the relevant tree of callers:

suggestions.c: _Py_Offer_Suggestions() (the expected behavior) is only 
referenced by
pythonrun.c: print_exception(), which is only referenced by
pythonrun.c: print_exception_recursive(), which is only referenced by itself and
pythonrun.c: _PyErr_Display(), which is referenced by:
- threadmodule.c: thread_excepthook_file()
- pythonrun.c: PyErr_Display()
pythonrun.c: PyErr_Display() is referenced by:
- pythonrun.c: _PyErr_PrintEx()
- pythonrun.c: _Py_FatalErr_PrintExc()
- _testcapimodule.c: exception_print()
- sysmodule.c: sys_excepthook_impl() (Python's sys.excepthook)
pythonrun.c: _PyErr_PrintEx() is referenced by:
- pythonrun.c: PyErr_PrintEx() is referenced by
- pythonrun.c: PyErr_Print()
- pylifecycle.c: new_interpreter()
- pythonrun.c _PyErr_Print() is referenced by
- ceval.c:_Py_FinishPendingCalls()
- pylifecycle.c: init_importlib_external()
- pylifecycle.c: init_interp_main()

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Shreyan Avigyan


Shreyan Avigyan  added the comment:

Python shell uses  to evaluate while IDLE uses . Is that the 
problem?

--
nosy: +shreyanavigyan

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-04 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

I am surprised for 2 reasons.  First, I have seen other improved messages in 
IDLE, though maybe only for syntax errors.  Second, code entered through IDLE 
is executed by Python, 'same' in this respect as code entered through the REPL.

In more detail, IDLE calls
  exec(compile(code, '', 'single', 0x200, True))
Still, 'int.reel' does not give the message while 'int.reel' in the exec above 
does.  I have no idea right now why the difference.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-03 Thread Dennis Sweeney


Change by Dennis Sweeney :


--
nosy: +pablogsal

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44026] IDLE doesn't offer "Did you mean?" for AttributeError and NameError

2021-05-03 Thread Dennis Sweeney


New submission from Dennis Sweeney :

After bpo-38530, I get this in the python shell:


Python 3.10.0b1 (tags/v3.10.0b1:ba42175, May  3 2021, 20:22:30) [MSC v.1928 64 
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... foobar = 1
...
>>> A.foocar
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: type object 'A' has no attribute 'foocar'. Did you mean: 
'foobar'?
>>>


But I get this in IDLE:

Python 3.10.0b1 (tags/v3.10.0b1:ba42175, May  3 2021, 20:22:30) [MSC v.1928 64 
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
class A:
foobar = 1


A.foocar
Traceback (most recent call last):
  File "", line 1, in 
A.foocar
AttributeError: type object 'A' has no attribute 'foocar'



Can we extend this functionality to IDLE, and fix the discrepancy?

--
assignee: terry.reedy
components: IDLE
messages: 392850
nosy: Dennis Sweeney, terry.reedy
priority: normal
severity: normal
status: open
title: IDLE doesn't offer "Did you mean?" for AttributeError and NameError
type: enhancement
versions: Python 3.10, Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com