Skip to content
Open
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
6 changes: 6 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,12 @@ def indentsize(line):
return len(expline) - len(expline.lstrip())

def _findclass(func):
try:
idx = func.__code__.co_freevars.index('__class__')
except ValueError:
pass
else:
return func.__closure__[0].cell_contents
cls = sys.modules.get(func.__module__)
if cls is None:
return None
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_inspect/inspect_fodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,15 @@ async def asyncf(self):
# a closing parenthesis with the opening paren being in another line
(
); after_closing = lambda: 1

def gen_subclass(abuse_msg):

class subclass(StupidGit):
def abuse(self, a, b, c):
print(abuse_msg)
super().abuse(a, b, c)

return subclass

DynamicSubclass = gen_subclass("some message")
del gen_subclass
13 changes: 10 additions & 3 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ class TestRetrievingSourceCode(GetSourceBase):
def test_getclasses(self):
classes = inspect.getmembers(mod, inspect.isclass)
self.assertEqual(classes,
[('FesteringGob', mod.FesteringGob),
[('DynamicSubclass', mod.DynamicSubclass),
('FesteringGob', mod.FesteringGob),
('MalodorousPervert', mod.MalodorousPervert),
('ParrotDroppings', mod.ParrotDroppings),
('StupidGit', mod.StupidGit),
Expand All @@ -649,7 +650,8 @@ def test_getclasses(self):
mod.ParrotDroppings))
]
],
(mod.WhichComments, (object,),)
(mod.WhichComments, (object,),),
(mod.DynamicSubclass, (mod.StupidGit,)),
]
])
tree = inspect.getclasstree([cls[1] for cls in classes], True)
Expand All @@ -662,7 +664,8 @@ def test_getclasses(self):
mod.ParrotDroppings))
]
],
(mod.WhichComments, (object,),)
(mod.WhichComments, (object,),),
(mod.DynamicSubclass, (mod.StupidGit,)),
]
])

Expand Down Expand Up @@ -697,6 +700,10 @@ def test_getdoc_inherited(self):
'Another\n\ndocstring\n\ncontaining\n\ntabs')
self.assertEqual(inspect.getdoc(mod.FesteringGob.contradiction),
'The automatic gainsaying.')
self.assertEqual(inspect.getdoc(mod.DynamicSubclass.abuse),
'Another\n\ndocstring\n\ncontaining\n\ntabs')
self.assertEqual(inspect.getdoc(mod.DynamicSubclass().abuse),
'Another\n\ndocstring\n\ncontaining\n\ntabs')

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`inspect.getdoc` now correctly inherits docstrings in dynamically generated
subclasses for methods that rely on `super()` (as the owning class can be
retrieved by inspecting the `__class__` cell).
Loading