- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
Issue27578
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.
Created on 2016-07-20 12:05 by Alexander Todorov, last changed 2022-04-11 14:58 by admin.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 20809 | open | kernc, 2020-06-11 14:55 | |
| Messages (7) | |||
|---|---|---|---|
| msg270867 - (view) | Author: Alexander Todorov (Alexander Todorov) | Date: 2016-07-20 12:05 | |
$ 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.
|
|||
| msg270871 - (view) | Author: R. David Murray (r.david.murray) * ![]() |
Date: 2016-07-20 13:54 | |
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.
|
|||
| msg270911 - (view) | Author: Antti Haapala (ztane) * | Date: 2016-07-21 11:28 | |
Or perhaps getlines should return [''] for empty regular files? |
|||
| msg270912 - (view) | Author: Antti Haapala (ztane) * | Date: 2016-07-21 11:30 | |
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. |
|||
| msg270925 - (view) | Author: R. David Murray (r.david.murray) * ![]() |
Date: 2016-07-21 13:44 | |
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). |
|||
| msg373075 - (view) | Author: Terry J. Reedy (terry.reedy) * ![]() |
Date: 2020-07-06 08:07 | |
I agree that getsource raising is a bug. I would more at the behavior and doc for getlines before I decided about that. |
|||
| msg380954 - (view) | Author: kernc (kernc) * | Date: 2020-11-14 03:03 | |
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. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:34 | admin | set | github: 71765 |
| 2020-11-14 03:03:41 | kernc | set | messages: + msg380954 |
| 2020-07-06 08:07:33 | terry.reedy | set | nosy:
+ terry.reedy messages: + msg373075 versions: - Python 2.7, Python 3.5, Python 3.6, Python 3.9 |
| 2020-06-11 14:55:16 | kernc | set | keywords:
+ patch stage: patch review pull_requests: + pull_request20006 |
| 2020-06-11 14:51:56 | kernc | set | nosy:
+ kernc versions: + Python 3.9, Python 3.10 |
| 2016-07-21 13:44:17 | r.david.murray | set | messages: + msg270925 |
| 2016-07-21 11:30:03 | ztane | set | messages: + msg270912 |
| 2016-07-21 11:28:17 | ztane | set | nosy:
+ ztane messages: + msg270911 |
| 2016-07-20 13:54:45 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg270871 versions: + Python 3.6 |
| 2016-07-20 12:05:58 | Alexander Todorov | create | |


