Work out which source or compiled file an object was defined in.
(object)
| 890 | return '\n'.join(lines) |
| 891 | |
| 892 | def getfile(object): |
| 893 | """Work out which source or compiled file an object was defined in.""" |
| 894 | if ismodule(object): |
| 895 | if getattr(object, '__file__', None): |
| 896 | return object.__file__ |
| 897 | raise TypeError('{!r} is a built-in module'.format(object)) |
| 898 | if isclass(object): |
| 899 | if hasattr(object, '__module__'): |
| 900 | module = sys.modules.get(object.__module__) |
| 901 | if getattr(module, '__file__', None): |
| 902 | return module.__file__ |
| 903 | if object.__module__ == '__main__': |
| 904 | raise OSError('source code not available') |
| 905 | raise TypeError('{!r} is a built-in class'.format(object)) |
| 906 | if ismethod(object): |
| 907 | object = object.__func__ |
| 908 | if isfunction(object): |
| 909 | object = object.__code__ |
| 910 | if istraceback(object): |
| 911 | object = object.tb_frame |
| 912 | if isframe(object): |
| 913 | object = object.f_code |
| 914 | if iscode(object): |
| 915 | return object.co_filename |
| 916 | raise TypeError('module, class, method, function, traceback, frame, or ' |
| 917 | 'code object was expected, got {}'.format( |
| 918 | type(object).__name__)) |
| 919 | |
| 920 | def getmodulename(path): |
| 921 | """Return the module name for a given file, or None.""" |
no test coverage detected