@@ -380,6 +380,11 @@ def html2text(html):
380380
381381
382382class PydocBaseTest (unittest .TestCase ):
383+ def tearDown (self ):
384+ # Self-testing. Mocking only works if sys.modules['pydoc'] and pydoc
385+ # are the same. But some pydoc functions reload the module and change
386+ # sys.modules, so check that it was restored.
387+ self .assertIs (sys .modules ['pydoc' ], pydoc )
383388
384389 def _restricted_walk_packages (self , walk_packages , path = None ):
385390 """
@@ -411,6 +416,8 @@ def call_url_handler(self, url, expected_title):
411416
412417class PydocDocTest (unittest .TestCase ):
413418 maxDiff = None
419+ def tearDown (self ):
420+ self .assertIs (sys .modules ['pydoc' ], pydoc )
414421
415422 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
416423 'trace function introduces __locals__ unexpectedly' )
@@ -1279,12 +1286,15 @@ def test_modules_search_builtin(self):
12791286 self .assertTrue (result .startswith (expected ))
12801287
12811288 def test_importfile (self ):
1282- loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1289+ try :
1290+ loaded_pydoc = pydoc .importfile (pydoc .__file__ )
12831291
1284- self .assertIsNot (loaded_pydoc , pydoc )
1285- self .assertEqual (loaded_pydoc .__name__ , 'pydoc' )
1286- self .assertEqual (loaded_pydoc .__file__ , pydoc .__file__ )
1287- self .assertEqual (loaded_pydoc .__spec__ , pydoc .__spec__ )
1292+ self .assertIsNot (loaded_pydoc , pydoc )
1293+ self .assertEqual (loaded_pydoc .__name__ , 'pydoc' )
1294+ self .assertEqual (loaded_pydoc .__file__ , pydoc .__file__ )
1295+ self .assertEqual (loaded_pydoc .__spec__ , pydoc .__spec__ )
1296+ finally :
1297+ sys .modules ['pydoc' ] = pydoc
12881298
12891299
12901300class Rect :
@@ -1299,6 +1309,8 @@ class Square(Rect):
12991309
13001310
13011311class TestDescriptions (unittest .TestCase ):
1312+ def tearDown (self ):
1313+ self .assertIs (sys .modules ['pydoc' ], pydoc )
13021314
13031315 def test_module (self ):
13041316 # Check that pydocfodder module can be described
@@ -1788,6 +1800,8 @@ def a_fn_with_https_link():
17881800
17891801
17901802class PydocFodderTest (unittest .TestCase ):
1803+ def tearDown (self ):
1804+ self .assertIs (sys .modules ['pydoc' ], pydoc )
17911805
17921806 def getsection (self , text , beginline , endline ):
17931807 lines = text .splitlines ()
@@ -1927,6 +1941,8 @@ def test_html_doc_routines_in_module(self):
19271941)
19281942class PydocServerTest (unittest .TestCase ):
19291943 """Tests for pydoc._start_server"""
1944+ def tearDown (self ):
1945+ self .assertIs (sys .modules ['pydoc' ], pydoc )
19301946
19311947 def test_server (self ):
19321948 # Minimal test that starts the server, checks that it works, then stops
@@ -1989,9 +2005,14 @@ def test_url_requests(self):
19892005 ("foobar" , "Pydoc: Error - foobar" ),
19902006 ]
19912007
1992- with self .restrict_walk_packages ():
1993- for url , title in requests :
1994- self .call_url_handler (url , title )
2008+ self .assertIs (sys .modules ['pydoc' ], pydoc )
2009+ try :
2010+ with self .restrict_walk_packages ():
2011+ for url , title in requests :
2012+ self .call_url_handler (url , title )
2013+ finally :
2014+ # Some requests reload the module and change sys.modules.
2015+ sys .modules ['pydoc' ] = pydoc
19952016
19962017
19972018class TestHelper (unittest .TestCase ):
@@ -2001,6 +2022,9 @@ def test_keywords(self):
20012022
20022023
20032024class PydocWithMetaClasses (unittest .TestCase ):
2025+ def tearDown (self ):
2026+ self .assertIs (sys .modules ['pydoc' ], pydoc )
2027+
20042028 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
20052029 'trace function introduces __locals__ unexpectedly' )
20062030 @requires_docstrings
0 commit comments