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 exception with empty __init__.py #71765

Closed
atodorov mannequin opened this issue Jul 20, 2016 · 8 comments
Closed

inspect.findsource raises exception with empty __init__.py #71765

atodorov mannequin opened this issue Jul 20, 2016 · 8 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@atodorov
Copy link
Mannequin

atodorov mannequin commented Jul 20, 2016

BPO 27578
Nosy @terryjreedy, @bitdancer, @ztane, @kernc, @atodorov
PRs
  • gh-71765: Fix inspect.getsource() on empty file #20809
  • 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 = None
    created_at = <Date 2016-07-20.12:05:58.309>
    labels = ['type-bug', 'library', '3.10']
    title = 'inspect.findsource raises exception with empty __init__.py'
    updated_at = <Date 2020-11-14.03:03:41.490>
    user = 'https://github.com/atodorov'

    bugs.python.org fields:

    activity = <Date 2020-11-14.03:03:41.490>
    actor = 'kernc'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2016-07-20.12:05:58.309>
    creator = 'Alexander Todorov'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 27578
    keywords = ['patch']
    message_count = 7.0
    messages = ['270867', '270871', '270911', '270912', '270925', '373075', '380954']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'r.david.murray', 'ztane', 'kernc', 'Alexander Todorov']
    pr_nums = ['20809']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27578'
    versions = ['Python 3.10']

    @atodorov
    Copy link
    Mannequin Author

    atodorov mannequin commented Jul 20, 2016

    $ python2
    Python 2.7.5 (default, Oct 11 2015, 17:47:16) 
    [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import inspect
    >>> from pykickstart import handlers
    >>> inspect.getsource(handlers)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib64/python2.7/inspect.py", line 701, in getsource
        lines, lnum = getsourcelines(object)
      File "/usr/lib64/python2.7/inspect.py", line 690, in getsourcelines
        lines, lnum = findsource(object)
      File "/usr/lib64/python2.7/inspect.py", line 538, in findsource
        raise IOError('could not get source code')
    IOError: could not get source code
    >>> 

    There is a pykickstart/handlers/__init__.py file which is empty and the above import works fine as you can see. However inspect.findsource raises an exception. The same problem exists in Python 3.5 as well. The error comes from here:

    532     module = getmodule(object, file)
    533     if module:
    534         lines = linecache.getlines(file, module.__dict__)
    535     else:
    536         lines = linecache.getlines(file)
    537     if not lines:
    538         raise IOError('could not get source code')
    

    At this point lines is an empty list and we raise the exception. I'm hitting this problem when using a mutation testing tool that relies on getsource (which calls findsource).

    One possible workaround is to add a comment in the __init__.py file and everything seems to be working then. Another one is to patch the tool I'm using to take into account empty __init__.py files.

    @atodorov atodorov mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 20, 2016
    @bitdancer
    Copy link
    Member

    I agree that this is inconsistent. It might be quite tricky to fix, though, unless we special case file.endswith('__init__.py'), which feels like a hack. But maybe it is appropriate.

    @ztane
    Copy link
    Mannequin

    ztane mannequin commented Jul 21, 2016

    Or perhaps getlines should return [''] for empty regular files?

    @ztane
    Copy link
    Mannequin

    ztane mannequin commented Jul 21, 2016

    It must be noted that getlines itself is not documented, and thus there is no backwards-compatibility to preserve really. getline returns '' for any erroneous line, so it wouldn't affect it.

    @bitdancer
    Copy link
    Member

    I think someone should propose a patch with tests and we'll evaluate it. We prefer to retain backward compatibility even on undocumented interfaces, if possible, but yes they are more open to change (though only in a feature release, in general).

    @kernc kernc mannequin added 3.9 only security fixes 3.10 only security fixes labels Jun 11, 2020
    @terryjreedy
    Copy link
    Member

    I agree that getsource raising is a bug. I would more at the behavior and doc for getlines before I decided about that.

    @terryjreedy terryjreedy removed the 3.9 only security fixes label Jul 6, 2020
    @kernc
    Copy link
    Mannequin

    kernc mannequin commented Nov 14, 2020

    The proposed patch doesn't break any interfaces. inspect.getsource/.getsourcelines() still raise OSError if the source is unretrievable. linecache.getlines() is undocumented, but the change retains perfect compatibility with docstrings of both linecache.getlines() which is affected and linecache.updatecache() which is touched.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    encukou pushed a commit that referenced this issue Mar 18, 2024
    * bpo-27578: Fix inspect.getsource() on empty file
    
    For modules from empty files, `inspect.getsource()` now
    returns an empty string, and `inspect.getsourcelines()` returns
    a list of one empty string, fixing the expected invariant.
    
    As indicated by `exec('')`, empty strings are valid Python
    source code.
    
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    @encukou
    Copy link
    Member

    encukou commented Mar 18, 2024

    Sorry for the delay, and thank you for the fix!

    @encukou encukou closed this as completed Mar 18, 2024
    vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 20, 2024
    * bpo-27578: Fix inspect.getsource() on empty file
    
    For modules from empty files, `inspect.getsource()` now
    returns an empty string, and `inspect.getsourcelines()` returns
    a list of one empty string, fixing the expected invariant.
    
    As indicated by `exec('')`, empty strings are valid Python
    source code.
    
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
    * bpo-27578: Fix inspect.getsource() on empty file
    
    For modules from empty files, `inspect.getsource()` now
    returns an empty string, and `inspect.getsourcelines()` returns
    a list of one empty string, fixing the expected invariant.
    
    As indicated by `exec('')`, empty strings are valid Python
    source code.
    
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
    * bpo-27578: Fix inspect.getsource() on empty file
    
    For modules from empty files, `inspect.getsource()` now
    returns an empty string, and `inspect.getsourcelines()` returns
    a list of one empty string, fixing the expected invariant.
    
    As indicated by `exec('')`, empty strings are valid Python
    source code.
    
    Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    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

    3 participants