Skip to content

Commit a0e011e

Browse files
serhiy-storchakamrahtz
authored andcommitted
pythongh-120590: Fix test_pydoc in the refleak hunting mode (pythonGH-120615)
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.
1 parent 9267431 commit a0e011e

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
@@ -385,6 +385,11 @@ def html2text(html):
385385

386386

387387
class PydocBaseTest(unittest.TestCase):
388+
def tearDown(self):
389+
# Self-testing. Mocking only works if sys.modules['pydoc'] and pydoc
390+
# are the same. But some pydoc functions reload the module and change
391+
# sys.modules, so check that it was restored.
392+
self.assertIs(sys.modules['pydoc'], pydoc)
388393

389394
def _restricted_walk_packages(self, walk_packages, path=None):
390395
"""
@@ -416,6 +421,8 @@ def call_url_handler(self, url, expected_title):
416421

417422
class PydocDocTest(unittest.TestCase):
418423
maxDiff = None
424+
def tearDown(self):
425+
self.assertIs(sys.modules['pydoc'], pydoc)
419426

420427
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
421428
'trace function introduces __locals__ unexpectedly')
@@ -1284,12 +1291,15 @@ def test_modules_search_builtin(self):
12841291
self.assertTrue(result.startswith(expected))
12851292

12861293
def test_importfile(self):
1287-
loaded_pydoc = pydoc.importfile(pydoc.__file__)
1294+
try:
1295+
loaded_pydoc = pydoc.importfile(pydoc.__file__)
12881296

1289-
self.assertIsNot(loaded_pydoc, pydoc)
1290-
self.assertEqual(loaded_pydoc.__name__, 'pydoc')
1291-
self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
1292-
self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
1297+
self.assertIsNot(loaded_pydoc, pydoc)
1298+
self.assertEqual(loaded_pydoc.__name__, 'pydoc')
1299+
self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
1300+
self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
1301+
finally:
1302+
sys.modules['pydoc'] = pydoc
12931303

12941304

12951305
class Rect:
@@ -1304,6 +1314,8 @@ class Square(Rect):
13041314

13051315

13061316
class TestDescriptions(unittest.TestCase):
1317+
def tearDown(self):
1318+
self.assertIs(sys.modules['pydoc'], pydoc)
13071319

13081320
def test_module(self):
13091321
# Check that pydocfodder module can be described
@@ -1793,6 +1805,8 @@ def a_fn_with_https_link():
17931805

17941806

17951807
class PydocFodderTest(unittest.TestCase):
1808+
def tearDown(self):
1809+
self.assertIs(sys.modules['pydoc'], pydoc)
17961810

17971811
def getsection(self, text, beginline, endline):
17981812
lines = text.splitlines()
@@ -1932,6 +1946,8 @@ def test_html_doc_routines_in_module(self):
19321946
)
19331947
class PydocServerTest(unittest.TestCase):
19341948
"""Tests for pydoc._start_server"""
1949+
def tearDown(self):
1950+
self.assertIs(sys.modules['pydoc'], pydoc)
19351951

19361952
def test_server(self):
19371953
# Minimal test that starts the server, checks that it works, then stops
@@ -1994,9 +2010,14 @@ def test_url_requests(self):
19942010
("foobar", "Pydoc: Error - foobar"),
19952011
]
19962012

1997-
with self.restrict_walk_packages():
1998-
for url, title in requests:
1999-
self.call_url_handler(url, title)
2013+
self.assertIs(sys.modules['pydoc'], pydoc)
2014+
try:
2015+
with self.restrict_walk_packages():
2016+
for url, title in requests:
2017+
self.call_url_handler(url, title)
2018+
finally:
2019+
# Some requests reload the module and change sys.modules.
2020+
sys.modules['pydoc'] = pydoc
20002021

20012022

20022023
class TestHelper(unittest.TestCase):
@@ -2006,6 +2027,9 @@ def test_keywords(self):
20062027

20072028

20082029
class PydocWithMetaClasses(unittest.TestCase):
2030+
def tearDown(self):
2031+
self.assertIs(sys.modules['pydoc'], pydoc)
2032+
20092033
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
20102034
'trace function introduces __locals__ unexpectedly')
20112035
@requires_docstrings

0 commit comments

Comments
 (0)