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.getsource(), P302 loader and '<..>' filenames #64404

Open
stefanmueller mannequin opened this issue Jan 9, 2014 · 3 comments
Open

inspect.getsource(), P302 loader and '<..>' filenames #64404

stefanmueller mannequin opened this issue Jan 9, 2014 · 3 comments
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@stefanmueller
Copy link
Mannequin

stefanmueller mannequin commented Jan 9, 2014

BPO 20205
Nosy @bitdancer, @ericsnowcurrently, @serhiy-storchaka

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 2014-01-09.13:18:49.708>
labels = ['type-bug', 'library']
title = "inspect.getsource(), P302 loader and '<..>' filenames"
updated_at = <Date 2020-03-18.18:34:51.898>
user = 'https://bugs.python.org/stefanmueller'

bugs.python.org fields:

activity = <Date 2020-03-18.18:34:51.898>
actor = 'brett.cannon'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2014-01-09.13:18:49.708>
creator = 'stefan.mueller'
dependencies = []
files = []
hgrepos = []
issue_num = 20205
keywords = []
message_count = 3.0
messages = ['207736', '207766', '207772']
nosy_count = 4.0
nosy_names = ['r.david.murray', 'eric.snow', 'serhiy.storchaka', 'stefan.mueller']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue20205'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

@stefanmueller
Copy link
Mannequin Author

stefanmueller mannequin commented Jan 9, 2014

Following situation

  • python 2.7.6

  • module loaded via a PEP-302 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.

@stefanmueller stefanmueller mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 9, 2014
@serhiy-storchaka
Copy link
Member

There would be behavior change if file '<mymodule>' exists.

@bitdancer
Copy link
Member

Maybe the logic needs to be reordered: look for a loader first, before looking for a file on disk. It seems to me the current lookup order might itself be a bug.

Note that the code is the same in python3, so the issue exists there as well.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants