Skip to content

gh-120590: Fix test_pydoc in the refleak hunting mode #120615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
40 changes: 32 additions & 8 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ def html2text(html):


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

def _restricted_walk_packages(self, walk_packages, path=None):
"""
Expand Down Expand Up @@ -416,6 +421,8 @@ def call_url_handler(self, url, expected_title):

class PydocDocTest(unittest.TestCase):
maxDiff = None
def tearDown(self):
self.assertIs(sys.modules['pydoc'], pydoc)

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

def test_importfile(self):
loaded_pydoc = pydoc.importfile(pydoc.__file__)
try:
loaded_pydoc = pydoc.importfile(pydoc.__file__)

self.assertIsNot(loaded_pydoc, pydoc)
self.assertEqual(loaded_pydoc.__name__, 'pydoc')
self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
self.assertIsNot(loaded_pydoc, pydoc)
self.assertEqual(loaded_pydoc.__name__, 'pydoc')
self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
finally:
sys.modules['pydoc'] = pydoc


class Rect:
Expand All @@ -1304,6 +1314,8 @@ class Square(Rect):


class TestDescriptions(unittest.TestCase):
def tearDown(self):
self.assertIs(sys.modules['pydoc'], pydoc)

def test_module(self):
# Check that pydocfodder module can be described
Expand Down Expand Up @@ -1793,6 +1805,8 @@ def a_fn_with_https_link():


class PydocFodderTest(unittest.TestCase):
def tearDown(self):
self.assertIs(sys.modules['pydoc'], pydoc)

def getsection(self, text, beginline, endline):
lines = text.splitlines()
Expand Down Expand Up @@ -1932,6 +1946,8 @@ def test_html_doc_routines_in_module(self):
)
class PydocServerTest(unittest.TestCase):
"""Tests for pydoc._start_server"""
def tearDown(self):
self.assertIs(sys.modules['pydoc'], pydoc)

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

with self.restrict_walk_packages():
for url, title in requests:
self.call_url_handler(url, title)
self.assertIs(sys.modules['pydoc'], pydoc)
try:
with self.restrict_walk_packages():
for url, title in requests:
self.call_url_handler(url, title)
finally:
# Some requests reload the module and change sys.modules.
sys.modules['pydoc'] = pydoc


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


class PydocWithMetaClasses(unittest.TestCase):
def tearDown(self):
self.assertIs(sys.modules['pydoc'], pydoc)

@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
Expand Down
Loading