MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / getsourcefile

Function getsourcefile

tools/python-3.11.9-amd64/Lib/inspect.py:932–955  ·  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

930 return None
931
932def getsourcefile(object):
933 """Return the filename that can be used to locate an object's source.
934 Return None if no way can be identified to get the source.
935 """
936 filename = getfile(object)
937 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
938 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
939 if any(filename.endswith(s) for s in all_bytecode_suffixes):
940 filename = (os.path.splitext(filename)[0] +
941 importlib.machinery.SOURCE_SUFFIXES[0])
942 elif any(filename.endswith(s) for s in
943 importlib.machinery.EXTENSION_SUFFIXES):
944 return None
945 if os.path.exists(filename):
946 return filename
947 # only return a non-existent filename if the module has a PEP 302 loader
948 module = getmodule(object, filename)
949 if getattr(module, '__loader__', None) is not None:
950 return filename
951 elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
952 return filename
953 # or it is in the linecache
954 elif filename in linecache.cache:
955 return filename
956
957def getabsfile(object, _filename=None):
958 """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
getmoduleFunction · 0.85
endswithMethod · 0.80
splitextMethod · 0.80
anyFunction · 0.70
existsMethod · 0.45

Tested by

no test coverage detected