inspect.getfile: Don't crash on classes without '__module__' attribute #20372
Some classes defined in C may not have the '__module__' attribute, so
we now handle this case to avoid having unexepected AttributeError.
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 781a532..3599e02 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -516,9 +516,10 @@
return object.__file__
raise TypeError('{!r} is a built-in module'.format(object))
if isclass(object):
- object = sys.modules.get(object.__module__)
- if hasattr(object, '__file__'):
- return object.__file__
+ if hasattr(object, '__module__'):
+ object = sys.modules.get(object.__module__)
+ if hasattr(object, '__file__'):
+ return object.__file__
raise TypeError('{!r} is a built-in class'.format(object))
if ismethod(object):
object = object.__func__