MCPcopy Index your code
hub / github.com/python/cpython / getsourcefile

Function getsourcefile

Lib/inspect.py:882–908  ·  view source on GitHub ↗

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.

(object)

Source from the content-addressed store, hash-verified

880 return None
881
882def getsourcefile(object):
883 """Return the filename that can be used to locate an object's source.
884 Return None if no way can be identified to get the source.
885 """
886 filename = getfile(object)
887 all_bytecode_suffixes = importlib.machinery.BYTECODE_SUFFIXES[:]
888 if any(filename.endswith(s) for s in all_bytecode_suffixes):
889 filename = (os.path.splitext(filename)[0] +
890 importlib.machinery.SOURCE_SUFFIXES[0])
891 elif any(filename.endswith(s) for s in
892 importlib.machinery.EXTENSION_SUFFIXES):
893 return None
894 elif filename.endswith(".fwork"):
895 # Apple mobile framework markers are another type of non-source file
896 return None
897
898 # return a filename found in the linecache even if it doesn't exist on disk
899 if filename in linecache.cache:
900 return filename
901 if os.path.exists(filename):
902 return filename
903 # only return a non-existent filename if the module has a PEP 302 loader
904 module = getmodule(object, filename)
905 if getattr(module, '__loader__', None) is not None:
906 return filename
907 elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
908 return filename
909
910def getabsfile(object, _filename=None):
911 """Return an absolute path to the source or compiled file for an object.

Callers 4

getabsfileFunction · 0.85
findsourceFunction · 0.85
getframeinfoFunction · 0.85
_mainFunction · 0.85

Calls 6

getfileFunction · 0.85
splitextMethod · 0.80
anyFunction · 0.70
getmoduleFunction · 0.70
endswithMethod · 0.45
existsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…