This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.findsource raises IndexError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Kyle.Simpson, iritkatriel, jaraco, miss-islington, r.david.murray, taleinat
Priority: normal Keywords: patch

Created on 2013-04-15 12:52 by Kyle.Simpson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23633 merged iritkatriel, 2020-12-03 22:19
PR 23646 merged miss-islington, 2020-12-04 21:23
PR 23647 merged miss-islington, 2020-12-04 21:23
Messages (10)
msg186983 - (view) Author: Kyle Simpson (Kyle.Simpson) * Date: 2013-04-15 12:52
Here is one way to reproduce this bug:

1. Create a module file (bug.py in this example)

def func():
    pass

2. Run Python

>>> import bug
>>> help(bug)

3. Edit bug.py

def func():
    pass

def newfunc():
    pass

4. Use the same Python interpreter as in step 2

>>> reload(bug)
>>> help(bug)

5. Observe traceback

  [..snip..]
  File "C:\Python27\lib\inspect.py", line 578, in findsource
    if pat.match(lines[lnum]): break
IndexError: list index out of range


Note: A related but different issue is http://bugs.python.org/issue1218234.
msg186991 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-04-15 14:06
Can you explain what makes this one a different problem?  It looks like the same one to me.  Or is your intent in this issue just to avoid the exception?  In that case it seems to me it would better to fix issue 1218234 if we can.
msg187047 - (view) Author: Kyle Simpson (Kyle.Simpson) * Date: 2013-04-16 03:15
Right, this issue is about the IndexError.

The current patch for issue 1218234 doesn't solve this problem. You will still get IndexErrors, but you will get them when functions are removed rather than when functions are added.

I think we should simply throw an IOError instead of an IndexError. This matches the docstring and prevents help() from blowing up after reloading a module.
msg217950 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2014-05-05 20:15
This issue appears implicated in https://bitbucket.org/pypa/setuptools/issue/201
msg382444 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-03 19:33
I'm unable to reproduce it now. Has it been fixed?

>>> import x
>>> help(x)
Help on module x:

NAME
    x

FUNCTIONS
    func()

FILE
    c:\users\user\src\cpython\x.py


>>> from importlib import reload
>>> reload(x)
<module 'x' from 'C:\\Users\\User\\src\\cpython\\x.py'>
>>> help(x)
Help on module x:

NAME
    x

FUNCTIONS
    func()

    newfunc()

FILE
    c:\users\user\src\cpython\x.py
msg382445 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-03 19:35
Sorry, you're right - now the issue is when you remove functions from the module:

>>> reload(x)
<module 'x' from 'C:\\Users\\User\\src\\cpython\\x.py'>
>>> help(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\src\cpython\\lib\_sitebuiltins.py", line 103, in __call__
    return pydoc.help(*args, **kwds)
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 2000, in __call__
    self.help(request)
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 2059, in help
    else: doc(request, 'Help on %s:', output=self._output)
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 1779, in doc
    pager(render_doc(thing, title, forceload))
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 1772, in render_doc
    return title % desc + '\n\n' + renderer.document(object, name)
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 472, in document
    if inspect.ismodule(object): return self.docmodule(*args)
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 1273, in docmodule
    contents.append(self.document(value, key, name))
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 474, in document
    if inspect.isroutine(object): return self.docroutine(*args)
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 1510, in docroutine
    doc = getdoc(object) or ''
  File "C:\Users\User\src\cpython\lib\pydoc.py", line 187, in getdoc
    result = _getdoc(object) or inspect.getcomments(object)
  File "C:\Users\User\src\cpython\lib\inspect.py", line 882, in getcomments
    lines, lnum = findsource(object)
  File "C:\Users\User\src\cpython\lib\inspect.py", line 871, in findsource
    if pat.match(lines[lnum]): break
IndexError: list index out of range
msg382531 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-12-04 21:22
New changeset 2e0760bb2edb595050aff82f236cd32b44d3dfb3 by Irit Katriel in branch 'master':
bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
https://github.com/python/cpython/commit/2e0760bb2edb595050aff82f236cd32b44d3dfb3
msg382533 - (view) Author: miss-islington (miss-islington) Date: 2020-12-04 21:45
New changeset a4e7d5f750e06e31a80a83c2af02b1a40cecd0ff by Miss Islington (bot) in branch '3.8':
bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
https://github.com/python/cpython/commit/a4e7d5f750e06e31a80a83c2af02b1a40cecd0ff
msg382541 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-12-04 22:42
New changeset d1f07419c7560ed3ba52ba4f667f4eec9b5fe95d by Miss Islington (bot) in branch '3.9':
bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
https://github.com/python/cpython/commit/d1f07419c7560ed3ba52ba4f667f4eec9b5fe95d
msg382543 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-12-04 22:43
Thanks for reporting this, Kyle!

Thanks for the PR, Irit!
History
Date User Action Args
2022-04-11 14:57:44adminsetgithub: 61935
2021-04-26 12:09:20iritkatriellinkissue20290 superseder
2020-12-04 22:43:17taleinatsetstatus: open -> closed
resolution: fixed
messages: + msg382543

stage: patch review -> resolved
2020-12-04 22:42:06taleinatsetmessages: + msg382541
2020-12-04 21:45:15miss-islingtonsetmessages: + msg382533
2020-12-04 21:23:33miss-islingtonsetpull_requests: + pull_request22515
2020-12-04 21:23:25miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22514
2020-12-04 21:22:28taleinatsetnosy: + taleinat
messages: + msg382531
2020-12-04 18:02:26iritkatriellinkissue37166 superseder
2020-12-03 22:19:15iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22502
2020-12-03 19:35:33iritkatrielsetmessages: + msg382445
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.3
2020-12-03 19:33:08iritkatrielsetnosy: + iritkatriel
messages: + msg382444
2014-05-05 20:15:39jaracosetnosy: + jaraco
messages: + msg217950
2013-04-19 22:02:55eric.araujosettitle: inspect.findsource throws IndexError -> inspect.findsource raises IndexError
2013-04-16 03:15:45Kyle.Simpsonsetmessages: + msg187047
2013-04-15 14:06:53r.david.murraysetnosy: + r.david.murray
messages: + msg186991
2013-04-15 12:52:20Kyle.Simpsoncreate