[Python-checkins] cpython: inspect.getfile: Don't crash on classes without '__module__' attribute #20372
yury.selivanov
python-checkins at python.org
Mon Jan 27 19:35:41 CET 2014
http://hg.python.org/cpython/rev/50706164c38f
changeset: 88776:50706164c38f
user: Yury Selivanov <yselivanov at sprymix.com>
date: Mon Jan 27 13:24:56 2014 -0500
summary:
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.
files:
Lib/inspect.py | 7 ++++---
Lib/test/test_inspect.py | 10 ++++++++++
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/Lib/inspect.py b/Lib/inspect.py
--- 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__
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -319,6 +319,16 @@
def test_getfile(self):
self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
+ def test_getfile_class_without_module(self):
+ class CM(type):
+ @property
+ def __module__(cls):
+ raise AttributeError
+ class C(metaclass=CM):
+ pass
+ with self.assertRaises(TypeError):
+ inspect.getfile(C)
+
def test_getmodule_recursion(self):
from types import ModuleType
name = '__inspect_dummy'
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list