cpython: 50706164c38f (original) (raw)
Mercurial > cpython
changeset 88776:50706164c38f
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. [#20372]
Yury Selivanov yselivanov@sprymix.com | |
---|---|
date | Mon, 27 Jan 2014 13:24:56 -0500 |
parents | dbad4564cd12 |
children | fc32459495fc |
files | Lib/inspect.py Lib/test/test_inspect.py |
diffstat | 2 files changed, 14 insertions(+), 3 deletions(-)[+] [-] Lib/inspect.py 7 Lib/test/test_inspect.py 10 |
line wrap: on
line diff
--- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -516,9 +516,10 @@ def getfile(object): return object.file raise TypeError('{!r} is a built-in module'.format(object)) if isclass(object):
object = sys.modules.get(object.__module__)[](#l1.7)
if hasattr(object, '__file__'):[](#l1.8)
return object.__file__[](#l1.9)
if hasattr(object, '__module__'):[](#l1.10)
object = sys.modules.get(object.__module__)[](#l1.11)
if hasattr(object, '__file__'):[](#l1.12)
if ismethod(object): object = object.funcreturn object.__file__[](#l1.13) raise TypeError('{!r} is a built-in class'.format(object))[](#l1.14)
--- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -319,6 +319,16 @@ class TestRetrievingSourceCode(GetSource def test_getfile(self): self.assertEqual(inspect.getfile(mod.StupidGit), mod.file)
- def test_getfile_class_without_module(self):
class CM(type):[](#l2.8)
@property[](#l2.9)
def __module__(cls):[](#l2.10)
raise AttributeError[](#l2.11)
class C(metaclass=CM):[](#l2.12)
pass[](#l2.13)
with self.assertRaises(TypeError):[](#l2.14)
inspect.getfile(C)[](#l2.15)
+ def test_getmodule_recursion(self): from types import ModuleType name = '__inspect_dummy'