Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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__
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down