[issue20205] inspect.getsource(), P302 loader and '<..>' filenames

Stefan Müller report at bugs.python.org
Thu Jan 9 14:18:49 CET 2014


New submission from Stefan Müller:

Following situation

* python 2.7.6

* module loaded via a PEP302 loader.

* the loader has get_source(fullname)

* assigns a dummy string as a file: module.__file__ == "<mymodule>"

Then

inspect.getsource(module)

throws

IOError: could not get source code

I tired to track this down, and it seems to be caused by  linecache.updatecache(..) with has

    if not filename or (filename.startswith('<') and filename.endswith('>')):
        return []

at the beginning.

This seems too restrictive me. Without the 'if' it would try to read the file from disk, and if that fails check if there is a loader, without a loader it returns [], so there would not be any behaviour change for non-loader modules if the 'if' was removed, only an additional disk access.

I suggest to remove the 'if'.

Workaround: Don't use '<>' for the dummy file name, but I've read somewhere that those '<>' are a convention for such use-cases.

----------
components: Library (Lib)
messages: 207736
nosy: stefan.mueller
priority: normal
severity: normal
status: open
title: inspect.getsource(), P302 loader and '<..>' filenames
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20205>
_______________________________________


More information about the Python-bugs-list mailing list