classification
Title: IDLE crashes when KeyError is raised during calltip generation
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: bup, miss-islington, taleinat, terry.reedy
Priority: normal Keywords: patch

Created on 2019-11-04 23:45 by bup, last changed 2020-04-04 20:37 by terry.reedy. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17152 merged taleinat, 2019-11-14 10:52
PR 19353 merged miss-islington, 2020-04-04 03:06
PR 19354 merged miss-islington, 2020-04-04 03:06
Messages (8)
msg355983 - (view) Author: Dan Snider (bup) * Date: 2019-11-04 23:45
When the following program has been input (into 32 bit 3.8.0 Python running on windows 10), all IDLE processes and windows will immediately and irrevocably hang the instant the open parentheses at the end of the statement "Object(" is rendered.

However that's just my 90% sure guess of the cause, based on 
 how when the regular dict from this example is swapped with one that raises AttributeError instead of KeyError, a crash no longer occurs. Quite perplexing, seeing as neither exception is handled in get_argspec.

>>> if 1:
    from idlelib.calltip import get_argspec
    class Type(type):
        __class__ = property((__class__:={}).__getitem__,__class__.__setitem__)
    class Object(metaclass=Type):
        __slots__ = '__class__'
    get_argspec(Object)

    
Traceback (most recent call last):
  File "<pyshell#41>", line 7, in <module>
    get_argspec(Object)
  File "C:\Python38\lib\idlelib\calltip.py", line 141, in get_argspec
    argspec = str(inspect.signature(fob))
  File "C:\Python38\lib\inspect.py", line 3093, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  File "C:\Python38\lib\inspect.py", line 2842, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
  File "C:\Python38\lib\inspect.py", line 2218, in _signature_from_callable
    if isinstance(obj, types.MethodType):
KeyError: <class '__main__.Object'>
msg356008 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-11-05 07:23
Since isinstance(Object.__call__, types.MethodType) is False, the 'fob' in 'inspect.signature(fob)' is Object.  (See the initial get_argspec code.)  Indeed, 'inspect.signature(Object)' results in the traceback following inspect.signature(fob) and 'isinstance(Object, types.MethodType)' results in the last line thereof.  Not returning 'False' strikes me as maybe a bug in 'isinstance'.  Have you opened a report for this?  

Directly importing and executing  idlelib code is not supported (its is 'private'), but entering 'Object(' to cause IDLE to make the same call is.  And the result is worse than the exception.  The IDLE gui process hangs, waiting for the response from the socket connection to the user code process that never comes.  This is clearly a bug, regardless of whether the user code is buggy.

The relevant section of get_argspec is

    try:
        argspec = str(inspect.signature(fob))
    except ValueError as err:
        msg = str(err)
        if msg.startswith(_invalid_method):
            return _invalid_method

Signature() is documented as returning either ValueError or TypeError, and with the 'bug' in isinstance, others are possible.  So any error should be caught.  (The default of falling through and checking for a docstring signature is correct.)
msg356010 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-11-05 07:55
Further experiments suggest a fix for the hang, which is not specific to this example.  See new issue #38695.
msg356592 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-11-14 11:00
See PR GH-17152 with a fix for the uncaught exception in getargspec().
msg365737 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-04-04 03:06
New changeset 52013e5b6d5ca32eef5a3d65ecdf7db89cefc2fd by Tal Einat in branch 'master':
bpo-38689: avoid IDLE hanging when calltip fails getting a signature (GH-17152)
https://github.com/python/cpython/commit/52013e5b6d5ca32eef5a3d65ecdf7db89cefc2fd
msg365739 - (view) Author: miss-islington (miss-islington) Date: 2020-04-04 03:24
New changeset 681044a0ab6c93554ff8d003c7f9fe5fdb0c83ba by Miss Islington (bot) in branch '3.7':
bpo-38689: avoid IDLE hanging when calltip fails getting a signature (GH-17152)
https://github.com/python/cpython/commit/681044a0ab6c93554ff8d003c7f9fe5fdb0c83ba
msg365740 - (view) Author: miss-islington (miss-islington) Date: 2020-04-04 03:25
New changeset 15337726e5b92976c2815d05c514804e9aa49a8c by Miss Islington (bot) in branch '3.8':
bpo-38689: avoid IDLE hanging when calltip fails getting a signature (GH-17152)
https://github.com/python/cpython/commit/15337726e5b92976c2815d05c514804e9aa49a8c
msg365779 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-04-04 20:37
get_argspec accesses the user object 3 times.  The first, ob.__call__ was already wrapped in try-except.  The second, signature(ob or ob.__call) is wrapped by this issue.  It also adds a new test based on Dan's example. The third is (ob or ob.__call__).__doc__.  I did not wrap this because I could not create an example for which this fails.  There seems to be some special casing of this special attribute so that its default is None.

I opened #40180 for the isinstance bug and #40181 for further get_argspec changes, in particular, removing the positional-only '/' note.
History
Date User Action Args
2020-04-04 20:37:09terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg365779

stage: patch review -> resolved
2020-04-04 03:25:12miss-islingtonsetmessages: + msg365740
2020-04-04 03:24:46miss-islingtonsetmessages: + msg365739
2020-04-04 03:06:27miss-islingtonsetpull_requests: + pull_request18716
2020-04-04 03:06:19miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request18715
2020-04-04 03:06:05terry.reedysetmessages: + msg365737
2019-11-14 11:00:49taleinatsetnosy: + taleinat
messages: + msg356592
2019-11-14 10:52:49taleinatsetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request16661
2019-11-05 07:55:42terry.reedysettype: behavior
stage: test needed
messages: + msg356010
versions: + Python 3.7, Python 3.9
2019-11-05 07:23:24terry.reedysetmessages: + msg356008
2019-11-04 23:45:56bupcreate