Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect.findsource raises IndexError #61935

Closed
KyleSimpson mannequin opened this issue Apr 15, 2013 · 10 comments
Closed

inspect.findsource raises IndexError #61935

KyleSimpson mannequin opened this issue Apr 15, 2013 · 10 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@KyleSimpson
Copy link
Mannequin

KyleSimpson mannequin commented Apr 15, 2013

BPO 17735
Nosy @jaraco, @taleinat, @bitdancer, @miss-islington, @iritkatriel
PRs
  • bpo-17735: make inspect.findsource raise OsError instead of IndexError wh… #23633
  • [3.9] bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633) #23646
  • [3.8] bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633) #23647
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-12-04.22:43:17.342>
    created_at = <Date 2013-04-15.12:52:19.969>
    labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
    title = 'inspect.findsource raises IndexError'
    updated_at = <Date 2020-12-04.22:43:17.341>
    user = 'https://bugs.python.org/KyleSimpson'

    bugs.python.org fields:

    activity = <Date 2020-12-04.22:43:17.341>
    actor = 'taleinat'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-04.22:43:17.342>
    closer = 'taleinat'
    components = ['Library (Lib)']
    creation = <Date 2013-04-15.12:52:19.969>
    creator = 'Kyle.Simpson'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 17735
    keywords = ['patch']
    message_count = 10.0
    messages = ['186983', '186991', '187047', '217950', '382444', '382445', '382531', '382533', '382541', '382543']
    nosy_count = 6.0
    nosy_names = ['jaraco', 'taleinat', 'r.david.murray', 'Kyle.Simpson', 'miss-islington', 'iritkatriel']
    pr_nums = ['23633', '23646', '23647']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue17735'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @KyleSimpson
    Copy link
    Mannequin Author

    KyleSimpson mannequin commented Apr 15, 2013

    Here is one way to reproduce this bug:

    1. Create a module file (bug.py in this example)
    def func():
        pass
    1. Run Python

    >> import bug
    >> help(bug)

    1. Edit bug.py
    def func():
        pass
    
    def newfunc():
        pass
    1. Use the same Python interpreter as in step 2

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

    1. 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.

    @KyleSimpson KyleSimpson mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 15, 2013
    @bitdancer
    Copy link
    Member

    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 bpo-1218234 if we can.

    @KyleSimpson
    Copy link
    Mannequin Author

    KyleSimpson mannequin commented Apr 16, 2013

    Right, this issue is about the IndexError.

    The current patch for bpo-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.

    @merwok merwok changed the title inspect.findsource throws IndexError inspect.findsource raises IndexError Apr 19, 2013
    @jaraco
    Copy link
    Member

    jaraco commented May 5, 2014

    This issue appears implicated in https://bitbucket.org/pypa/setuptools/issue/201

    @iritkatriel
    Copy link
    Member

    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

    @iritkatriel
    Copy link
    Member

    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

    @iritkatriel iritkatriel added 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Dec 3, 2020
    @taleinat
    Copy link
    Contributor

    taleinat commented Dec 4, 2020

    New changeset 2e0760b by Irit Katriel in branch 'master':
    bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
    2e0760b

    @miss-islington
    Copy link
    Contributor

    New changeset a4e7d5f by Miss Islington (bot) in branch '3.8':
    bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
    a4e7d5f

    @taleinat
    Copy link
    Contributor

    taleinat commented Dec 4, 2020

    New changeset d1f0741 by Miss Islington (bot) in branch '3.9':
    bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
    d1f0741

    @taleinat
    Copy link
    Contributor

    taleinat commented Dec 4, 2020

    Thanks for reporting this, Kyle!

    Thanks for the PR, Irit!

    @taleinat taleinat closed this as completed Dec 4, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants