Work out which source or compiled file an object was defined in.
(object)
| 816 | |
| 817 | |
| 818 | def getfile(object): |
| 819 | """Work out which source or compiled file an object was defined in.""" |
| 820 | if ismodule(object): |
| 821 | if getattr(object, '__file__', None): |
| 822 | return object.__file__ |
| 823 | raise TypeError('{!r} is a built-in module'.format(object)) |
| 824 | if isclass(object): |
| 825 | if hasattr(object, '__module__'): |
| 826 | module = sys.modules.get(object.__module__) |
| 827 | if getattr(module, '__file__', None): |
| 828 | return module.__file__ |
| 829 | if object.__module__ == '__main__': |
| 830 | raise OSError('source code not available') |
| 831 | raise TypeError('{!r} is a built-in class'.format(object)) |
| 832 | if ismethod(object): |
| 833 | object = object.__func__ |
| 834 | if isfunction(object): |
| 835 | object = object.__code__ |
| 836 | if istraceback(object): |
| 837 | object = object.tb_frame |
| 838 | if isframe(object): |
| 839 | object = object.f_code |
| 840 | if iscode(object): |
| 841 | return object.co_filename |
| 842 | raise TypeError('module, class, method, function, traceback, frame, or ' |
| 843 | 'code object was expected, got {}'.format( |
| 844 | type(object).__name__)) |
| 845 | |
| 846 | def getmodulename(path): |
| 847 | """Return the module name for a given file, or None.""" |
no test coverage detected