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

Function getfile

Lib/inspect.py:842–868  ·  view source on GitHub ↗

Work out which source or compiled file an object was defined in.

(object)

Source from the content-addressed store, hash-verified

840
841
842def getfile(object):
843 """Work out which source or compiled file an object was defined in."""
844 if ismodule(object):
845 if getattr(object, '__file__', None):
846 return object.__file__
847 raise TypeError('{!r} is a built-in module'.format(object))
848 if isclass(object):
849 if hasattr(object, '__module__'):
850 module = sys.modules.get(object.__module__)
851 if getattr(module, '__file__', None):
852 return module.__file__
853 if object.__module__ == '__main__':
854 raise OSError('source code not available')
855 raise TypeError('{!r} is a built-in class'.format(object))
856 if ismethod(object):
857 object = object.__func__
858 if isfunction(object):
859 object = object.__code__
860 if istraceback(object):
861 object = object.tb_frame
862 if isframe(object):
863 object = object.f_code
864 if iscode(object):
865 return object.co_filename
866 raise TypeError('module, class, method, function, traceback, frame, or '
867 'code object was expected, got {}'.format(
868 type(object).__name__))
869
870def getmodulename(path):
871 """Return the module name for a given file, or None."""

Callers 4

getsourcefileFunction · 0.85
getabsfileFunction · 0.85
findsourceFunction · 0.85
getframeinfoFunction · 0.85

Calls 9

ismoduleFunction · 0.85
isclassFunction · 0.85
ismethodFunction · 0.85
isfunctionFunction · 0.85
istracebackFunction · 0.85
isframeFunction · 0.85
iscodeFunction · 0.85
formatMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…