@@ -380,6 +380,11 @@ def html2text(html):
380
380
381
381
382
382
class 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 )
383
388
384
389
def _restricted_walk_packages (self , walk_packages , path = None ):
385
390
"""
@@ -411,6 +416,8 @@ def call_url_handler(self, url, expected_title):
411
416
412
417
class PydocDocTest (unittest .TestCase ):
413
418
maxDiff = None
419
+ def tearDown (self ):
420
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
414
421
415
422
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
416
423
'trace function introduces __locals__ unexpectedly' )
@@ -1279,12 +1286,15 @@ def test_modules_search_builtin(self):
1279
1286
self .assertTrue (result .startswith (expected ))
1280
1287
1281
1288
def test_importfile (self ):
1282
- loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1289
+ try :
1290
+ loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1283
1291
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
1288
1298
1289
1299
1290
1300
class Rect :
@@ -1299,6 +1309,8 @@ class Square(Rect):
1299
1309
1300
1310
1301
1311
class TestDescriptions (unittest .TestCase ):
1312
+ def tearDown (self ):
1313
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1302
1314
1303
1315
def test_module (self ):
1304
1316
# Check that pydocfodder module can be described
@@ -1788,6 +1800,8 @@ def a_fn_with_https_link():
1788
1800
1789
1801
1790
1802
class PydocFodderTest (unittest .TestCase ):
1803
+ def tearDown (self ):
1804
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1791
1805
1792
1806
def getsection (self , text , beginline , endline ):
1793
1807
lines = text .splitlines ()
@@ -1927,6 +1941,8 @@ def test_html_doc_routines_in_module(self):
1927
1941
)
1928
1942
class PydocServerTest (unittest .TestCase ):
1929
1943
"""Tests for pydoc._start_server"""
1944
+ def tearDown (self ):
1945
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1930
1946
1931
1947
def test_server (self ):
1932
1948
# Minimal test that starts the server, checks that it works, then stops
@@ -1989,9 +2005,14 @@ def test_url_requests(self):
1989
2005
("foobar" , "Pydoc: Error - foobar" ),
1990
2006
]
1991
2007
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
1995
2016
1996
2017
1997
2018
class TestHelper (unittest .TestCase ):
@@ -2001,6 +2022,9 @@ def test_keywords(self):
2001
2022
2002
2023
2003
2024
class PydocWithMetaClasses (unittest .TestCase ):
2025
+ def tearDown (self ):
2026
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
2027
+
2004
2028
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
2005
2029
'trace function introduces __locals__ unexpectedly' )
2006
2030
@requires_docstrings
0 commit comments