@@ -385,6 +385,11 @@ def html2text(html):
385
385
386
386
387
387
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 )
388
393
389
394
def _restricted_walk_packages (self , walk_packages , path = None ):
390
395
"""
@@ -416,6 +421,8 @@ def call_url_handler(self, url, expected_title):
416
421
417
422
class PydocDocTest (unittest .TestCase ):
418
423
maxDiff = None
424
+ def tearDown (self ):
425
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
419
426
420
427
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
421
428
'trace function introduces __locals__ unexpectedly' )
@@ -1284,12 +1291,15 @@ def test_modules_search_builtin(self):
1284
1291
self .assertTrue (result .startswith (expected ))
1285
1292
1286
1293
def test_importfile (self ):
1287
- loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1294
+ try :
1295
+ loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1288
1296
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
1293
1303
1294
1304
1295
1305
class Rect :
@@ -1304,6 +1314,8 @@ class Square(Rect):
1304
1314
1305
1315
1306
1316
class TestDescriptions (unittest .TestCase ):
1317
+ def tearDown (self ):
1318
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1307
1319
1308
1320
def test_module (self ):
1309
1321
# Check that pydocfodder module can be described
@@ -1793,6 +1805,8 @@ def a_fn_with_https_link():
1793
1805
1794
1806
1795
1807
class PydocFodderTest (unittest .TestCase ):
1808
+ def tearDown (self ):
1809
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1796
1810
1797
1811
def getsection (self , text , beginline , endline ):
1798
1812
lines = text .splitlines ()
@@ -1932,6 +1946,8 @@ def test_html_doc_routines_in_module(self):
1932
1946
)
1933
1947
class PydocServerTest (unittest .TestCase ):
1934
1948
"""Tests for pydoc._start_server"""
1949
+ def tearDown (self ):
1950
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1935
1951
1936
1952
def test_server (self ):
1937
1953
# Minimal test that starts the server, checks that it works, then stops
@@ -1994,9 +2010,14 @@ def test_url_requests(self):
1994
2010
("foobar" , "Pydoc: Error - foobar" ),
1995
2011
]
1996
2012
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
2000
2021
2001
2022
2002
2023
class TestHelper (unittest .TestCase ):
@@ -2006,6 +2027,9 @@ def test_keywords(self):
2006
2027
2007
2028
2008
2029
class PydocWithMetaClasses (unittest .TestCase ):
2030
+ def tearDown (self ):
2031
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
2032
+
2009
2033
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
2010
2034
'trace function introduces __locals__ unexpectedly' )
2011
2035
@requires_docstrings
0 commit comments