Skip to content

bpo-33587: inspect.getsource: reorder stat on file in linecache #6805

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

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,9 @@ def getsourcefile(object):
elif any(filename.endswith(s) for s in
importlib.machinery.EXTENSION_SUFFIXES):
return None
# return a filename found in the linecache even if it doesn't exist on disk
if filename in linecache.cache:
return filename
if os.path.exists(filename):
return filename
# only return a non-existent filename if the module has a PEP 302 loader
Expand All @@ -954,9 +957,6 @@ def getsourcefile(object):
return filename
elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
return filename
# or it is in the linecache
elif filename in linecache.cache:
return filename

def getabsfile(object, _filename=None):
"""Return an absolute path to the source or compiled file for an object.
Expand Down