diff --git a/Lib/inspect.py b/Lib/inspect.py index ad7e8cb1203e7f0..6582ffcddaa266d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -665,7 +665,7 @@ def getfile(object): if getattr(module, '__file__', None): return module.__file__ raise TypeError('{!r} is a built-in class'.format(object)) - if ismethod(object): + while ismethod(object): object = object.__func__ if isfunction(object): object = object.__code__ @@ -851,7 +851,7 @@ def findsource(object): else: raise OSError('could not find class definition') - if ismethod(object): + while ismethod(object): object = object.__func__ if isfunction(object): object = object.__code__ diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 98a9c0a662a0939..9d344428ade6616 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -504,6 +504,13 @@ def test_getsource(self): self.assertSourceEqual(mod.StupidGit, 21, 51) self.assertSourceEqual(mod.lobbest, 75, 76) + def test_getsource_nested_methodtype(self): + m1 = types.MethodType((lambda self: self) ,object()) + m2 = types.MethodType(m1,object()) + source1 = inspect.getsource(m1) + source2 = inspect.getsource(m2) + self.assertEqual(source1, source2) + def test_getsourcefile(self): self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile) self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile) @@ -547,6 +554,13 @@ class C(metaclass=CM): with self.assertRaises(TypeError): inspect.getfile(C) + def test_getfile_nested_methodtype(self): + m1 = types.MethodType((lambda self: self), object()) + m2 = types.MethodType(m1, object()) + source1 = inspect.getfile(m1) + source2 = inspect.getfile(m2) + self.assertEqual(source1, source2) + def test_getfile_broken_repr(self): class ErrorRepr: def __repr__(self): @@ -704,6 +718,13 @@ def test_findsource_without_filename(self): self.assertRaises(IOError, inspect.findsource, co) self.assertRaises(IOError, inspect.getsource, co) + def test_findsource_nested_methodtype(self): + m1 = types.MethodType((lambda self: self), object()) + m2 = types.MethodType(m1, object()) + source1 = inspect.findsource(m1) + source2 = inspect.findsource(m2) + self.assertEqual(source1, source2) + def test_getsource_on_method(self): self.assertSourceEqual(mod2.ClassWithMethod.method, 118, 119)