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 undocumented error for code objects with empty filename
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Nils.Bruin, T8y8, ezio.melotti, python-dev, r.david.murray
Priority: normal Keywords: easy, patch

Created on 2013-03-23 02:03 by Nils.Bruin, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
17526_getsource.patch T8y8, 2013-03-27 07:42 Patch and unittest
Messages (7)
msg185025 - (view) Author: Nils Bruin (Nils.Bruin) Date: 2013-03-23 02:03
It would seem reasonable that an empty filename would be a legitimate way of indicating that a code object does not have a corresponding source file. However, if one does that then inspect.findsource raises an unexpected IndexError rather than the documented IOError.

This seems due to the fix introduced on issue9284

Python 3.2.3 (default, Jun  8 2012, 05:40:07) 
[GCC 4.6.3 20120306 (Red Hat 4.6.3-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> C=compile("print(1)","<string>","single")
>>> D=compile("print(1)","","single")
>>> inspect.findsource(C)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.2/inspect.py", line 547, in findsource
    raise IOError('could not get source code')
IOError: could not get source code
>>> inspect.findsource(D)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.2/inspect.py", line 537, in findsource
    if not sourcefile and file[0] + file[-1] != '<>':
IndexError: string index out of range
msg185153 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-24 18:42
It is very likely the code is the same in 3.3 and 3.4, so I'm adding those versions without testing :)
msg185190 - (view) Author: Tyler Doyle (T8y8) * Date: 2013-03-25 07:26
It looks like file is getting set to '' and then indexed on line 553 below, hitting the IndexError before we ever get to IOError.

--550-----------------------
    file = getfile(object)  <-- file = ''
    sourcefile = getsourcefile(object)
    if not sourcefile and file[0] + file[-1] != '<>': <-- ''[0]
        raise IOError('source code not available')
    file = sourcefile if sourcefile else file
--556-----------------------

Confirmed in 3.3
msg185219 - (view) Author: Nils Bruin (Nils.Bruin) Date: 2013-03-25 18:05
The most straightforward change I can think of is to change the line

    if not sourcefile and file[0] + file[-1] != '<>':

to

    if not sourcefile and (not file or file[0] + file[-1] != '<>'):

That solves the problem in the cases I have observed.
msg185257 - (view) Author: Tyler Doyle (T8y8) * Date: 2013-03-26 04:34
Patch and test to accompany.
msg185537 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-03-30 03:19
New changeset bc73223e4c10 by Ezio Melotti in branch '2.7':
#17526: fix an IndexError raised while passing code without filename to inspect.findsource().  Initial patch by Tyler Doyle.
http://hg.python.org/cpython/rev/bc73223e4c10

New changeset 39e103c1577e by Ezio Melotti in branch '3.3':
#17526: fix an IndexError raised while passing code without filename to inspect.findsource().  Initial patch by Tyler Doyle.
http://hg.python.org/cpython/rev/39e103c1577e

New changeset 8362fbb0ef42 by Ezio Melotti in branch 'default':
#17526: merge with 3.3.
http://hg.python.org/cpython/rev/8362fbb0ef42
msg185538 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-30 03:21
Fixed, thanks for the report and the patch!
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61728
2014-01-31 03:01:00berker.peksaglinkissue19658 superseder
2013-03-30 03:21:15ezio.melottisetstatus: open -> closed

assignee: ezio.melotti
versions: - Python 3.2
nosy: + ezio.melotti

messages: + msg185538
resolution: fixed
stage: needs patch -> resolved
2013-03-30 03:19:37python-devsetnosy: + python-dev
messages: + msg185537
2013-03-27 07:42:17T8y8setfiles: + 17526_getsource.patch
2013-03-27 07:41:33T8y8setfiles: - test_inspect.patch
2013-03-27 07:41:27T8y8setfiles: - inspect.patch
2013-03-26 04:34:55T8y8setfiles: + test_inspect.patch

messages: + msg185257
2013-03-26 04:34:28T8y8setfiles: + inspect.patch
keywords: + patch
2013-03-25 18:05:28Nils.Bruinsetmessages: + msg185219
2013-03-25 07:26:20T8y8setnosy: + T8y8
messages: + msg185190
2013-03-24 18:42:49r.david.murraysetversions: + Python 3.3, Python 3.4
nosy: + r.david.murray

messages: + msg185153

keywords: + easy
stage: needs patch
2013-03-23 02:03:59Nils.Bruincreate