bpo-37173: Show passed class in inspect.getfile error (GH-13861)

Currently, inspect.getfile(str) will report nonsense:

```pytb
>>> inspect.getfile(str)
TypeError: <module 'builtins' (built-in)> is a built-in class
```

This fixes that


https://bugs.python.org/issue37173
This commit is contained in:
Philipp A 2019-06-08 14:05:46 +02:00 committed by Miss Islington (bot)
parent 65e5860fcc
commit d407d2a726
3 changed files with 22 additions and 3 deletions

View file

@ -659,9 +659,9 @@ def getfile(object):
raise TypeError('{!r} is a built-in module'.format(object))
if isclass(object):
if hasattr(object, '__module__'):
object = sys.modules.get(object.__module__)
if getattr(object, '__file__', None):
return object.__file__
module = sys.modules.get(object.__module__)
if getattr(module, '__file__', None):
return module.__file__
raise TypeError('{!r} is a built-in class'.format(object))
if ismethod(object):
object = object.__func__