bpo-42116: Fix inspect.getsource handling of trailing comments (GH-23630)
(cherry picked from commit 6e1eec71f59c344fb23c7977061dc2c97b77d51b)
Co-authored-by: Irit Katriel <iritkatriel@yahoo.com>
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index e3e2be5..05485a0 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -388,6 +388,7 @@
('ParrotDroppings', mod.ParrotDroppings),
('StupidGit', mod.StupidGit),
('Tit', mod.MalodorousPervert),
+ ('WhichComments', mod.WhichComments),
])
tree = inspect.getclasstree([cls[1] for cls in classes])
self.assertEqual(tree,
@@ -401,7 +402,8 @@
[(mod.FesteringGob, (mod.MalodorousPervert,
mod.ParrotDroppings))
]
- ]
+ ],
+ (mod.WhichComments, (object,),)
]
])
tree = inspect.getclasstree([cls[1] for cls in classes], True)
@@ -413,7 +415,8 @@
[(mod.FesteringGob, (mod.MalodorousPervert,
mod.ParrotDroppings))
]
- ]
+ ],
+ (mod.WhichComments, (object,),)
]
])
@@ -644,6 +647,18 @@
# as argument to another function.
self.assertSourceEqual(mod2.anonymous, 55, 55)
+class TestBlockComments(GetSourceBase):
+ fodderModule = mod
+
+ def test_toplevel_class(self):
+ self.assertSourceEqual(mod.WhichComments, 96, 114)
+
+ def test_class_method(self):
+ self.assertSourceEqual(mod.WhichComments.f, 99, 104)
+
+ def test_class_async_method(self):
+ self.assertSourceEqual(mod.WhichComments.asyncf, 109, 112)
+
class TestBuggyCases(GetSourceBase):
fodderModule = mod2
@@ -4016,8 +4031,8 @@
def test_main():
run_unittest(
- TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
- TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
+ TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBlockComments,
+ TestBuggyCases, TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
TestGetcallargsFunctions, TestGetcallargsMethods,
TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,