summaryrefslogtreecommitdiff
path: root/Lib/inspect.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index ea30466433..d6ac4e02ca 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -437,7 +437,9 @@ def getmodulename(path):
if info: return info[0]
def getsourcefile(object):
- """Return the Python source file an object was defined in, if it exists."""
+ """Return the filename that can be used to locate an object's source.
+ Return None if no way can be identified to get the source.
+ """
filename = getfile(object)
if filename[-4:].lower() in ('.pyc', '.pyo'):
filename = filename[:-4] + '.py'
@@ -450,6 +452,9 @@ def getsourcefile(object):
# only return a non-existent filename if the module has a PEP 302 loader
if hasattr(getmodule(object, filename), '__loader__'):
return filename
+ # or it is in the linecache
+ if filename in linecache.cache:
+ return filename
def getabsfile(object, _filename=None):
"""Return an absolute path to the source or compiled file for an object.