Work out which source or compiled file an object was defined in.
(object)
| 840 | |
| 841 | |
| 842 | def 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 | |
| 870 | def getmodulename(path): |
| 871 | """Return the module name for a given file, or None.""" |
no test coverage detected
searching dependent graphs…