MCPcopy Index your code
hub / github.com/RustPython/RustPython / getsourcefile

Function getsourcefile

Lib/inspect.py:858–884  ·  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

856 return None
857
858def getsourcefile(object):
859 """Return the filename that can be used to locate an object's source.
860 Return None if no way can be identified to get the source.
861 """
862 filename = getfile(object)
863 all_bytecode_suffixes = importlib.machinery.BYTECODE_SUFFIXES[:]
864 if any(filename.endswith(s) for s in all_bytecode_suffixes):
865 filename = (os.path.splitext(filename)[0] +
866 importlib.machinery.SOURCE_SUFFIXES[0])
867 elif any(filename.endswith(s) for s in
868 importlib.machinery.EXTENSION_SUFFIXES):
869 return None
870 elif filename.endswith(".fwork"):
871 # Apple mobile framework markers are another type of non-source file
872 return None
873
874 # return a filename found in the linecache even if it doesn't exist on disk
875 if filename in linecache.cache:
876 return filename
877 if os.path.exists(filename):
878 return filename
879 # only return a non-existent filename if the module has a PEP 302 loader
880 module = getmodule(object, filename)
881 if getattr(module, '__loader__', None) is not None:
882 return filename
883 elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
884 return filename
885
886def getabsfile(object, _filename=None):
887 """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 7

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

Tested by

no test coverage detected