Skip to content

Commit 4b53ea8

Browse files
[3.12] gh-120590: Fix test_pydoc in the refleak hunting mode (GH-120615) (GH-120670)
Mocking only works if sys.modules['pydoc'] and pydoc are the same, but some pydoc functions reload the module and change sys.modules. Ensure that sys.modules['pydoc'] is always restored after the corresponding tests. (cherry picked from commit 2cf4738) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent e4f1fed commit 4b53ea8

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

Lib/test/test_pydoc/test_pydoc.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ def html2text(html):
378378

379379

380380
class PydocBaseTest(unittest.TestCase):
381+
def tearDown(self):
382+
# Self-testing. Mocking only works if sys.modules['pydoc'] and pydoc
383+
# are the same. But some pydoc functions reload the module and change
384+
# sys.modules, so check that it was restored.
385+
self.assertIs(sys.modules['pydoc'], pydoc)
381386

382387
def _restricted_walk_packages(self, walk_packages, path=None):
383388
"""
@@ -409,6 +414,8 @@ def call_url_handler(self, url, expected_title):
409414

410415
class PydocDocTest(unittest.TestCase):
411416
maxDiff = None
417+
def tearDown(self):
418+
self.assertIs(sys.modules['pydoc'], pydoc)
412419

413420
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
414421
'trace function introduces __locals__ unexpectedly')
@@ -1151,15 +1158,20 @@ def test_modules_search_builtin(self):
11511158
self.assertTrue(result.startswith(expected))
11521159

11531160
def test_importfile(self):
1154-
loaded_pydoc = pydoc.importfile(pydoc.__file__)
1161+
try:
1162+
loaded_pydoc = pydoc.importfile(pydoc.__file__)
11551163

1156-
self.assertIsNot(loaded_pydoc, pydoc)
1157-
self.assertEqual(loaded_pydoc.__name__, 'pydoc')
1158-
self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
1159-
self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
1164+
self.assertIsNot(loaded_pydoc, pydoc)
1165+
self.assertEqual(loaded_pydoc.__name__, 'pydoc')
1166+
self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
1167+
self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
1168+
finally:
1169+
sys.modules['pydoc'] = pydoc
11601170

11611171

11621172
class TestDescriptions(unittest.TestCase):
1173+
def tearDown(self):
1174+
self.assertIs(sys.modules['pydoc'], pydoc)
11631175

11641176
def test_module(self):
11651177
# Check that pydocfodder module can be described
@@ -1505,6 +1517,8 @@ def a_fn_with_https_link():
15051517

15061518

15071519
class PydocFodderTest(unittest.TestCase):
1520+
def tearDown(self):
1521+
self.assertIs(sys.modules['pydoc'], pydoc)
15081522

15091523
def getsection(self, text, beginline, endline):
15101524
lines = text.splitlines()
@@ -1632,6 +1646,8 @@ def test_html_doc_routines_in_module(self):
16321646
)
16331647
class PydocServerTest(unittest.TestCase):
16341648
"""Tests for pydoc._start_server"""
1649+
def tearDown(self):
1650+
self.assertIs(sys.modules['pydoc'], pydoc)
16351651

16361652
def test_server(self):
16371653
# Minimal test that starts the server, checks that it works, then stops
@@ -1694,9 +1710,14 @@ def test_url_requests(self):
16941710
("foobar", "Pydoc: Error - foobar"),
16951711
]
16961712

1697-
with self.restrict_walk_packages():
1698-
for url, title in requests:
1699-
self.call_url_handler(url, title)
1713+
self.assertIs(sys.modules['pydoc'], pydoc)
1714+
try:
1715+
with self.restrict_walk_packages():
1716+
for url, title in requests:
1717+
self.call_url_handler(url, title)
1718+
finally:
1719+
# Some requests reload the module and change sys.modules.
1720+
sys.modules['pydoc'] = pydoc
17001721

17011722

17021723
class TestHelper(unittest.TestCase):
@@ -1706,6 +1727,9 @@ def test_keywords(self):
17061727

17071728

17081729
class PydocWithMetaClasses(unittest.TestCase):
1730+
def tearDown(self):
1731+
self.assertIs(sys.modules['pydoc'], pydoc)
1732+
17091733
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
17101734
'trace function introduces __locals__ unexpectedly')
17111735
@requires_docstrings

0 commit comments

Comments
 (0)