New submission from Alexander Todorov:

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

----------
components: Library (Lib)
messages: 270867
nosy: Alexander Todorov
priority: normal
severity: normal
status: open
title: inspect.findsource raises exception with empty __init__.py
type: behavior
versions: Python 2.7, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to