@@ -378,6 +378,11 @@ def html2text(html):
378
378
379
379
380
380
class PydocBaseTest (unittest .TestCase ):
381
+ def tearDown (self ):
382
+ # Self-testing. Mocking only works if sys.modules['pydoc'] and pydoc
383
+ # are the same. But some pydoc functions reload the module and change
384
+ # sys.modules, so check that it was restored.
385
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
381
386
382
387
def _restricted_walk_packages (self , walk_packages , path = None ):
383
388
"""
@@ -409,6 +414,8 @@ def call_url_handler(self, url, expected_title):
409
414
410
415
class PydocDocTest (unittest .TestCase ):
411
416
maxDiff = None
417
+ def tearDown (self ):
418
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
412
419
413
420
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
414
421
'trace function introduces __locals__ unexpectedly' )
@@ -1151,15 +1158,20 @@ def test_modules_search_builtin(self):
1151
1158
self .assertTrue (result .startswith (expected ))
1152
1159
1153
1160
def test_importfile (self ):
1154
- loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1161
+ try :
1162
+ loaded_pydoc = pydoc .importfile (pydoc .__file__ )
1155
1163
1156
- self .assertIsNot (loaded_pydoc , pydoc )
1157
- self .assertEqual (loaded_pydoc .__name__ , 'pydoc' )
1158
- self .assertEqual (loaded_pydoc .__file__ , pydoc .__file__ )
1159
- self .assertEqual (loaded_pydoc .__spec__ , pydoc .__spec__ )
1164
+ self .assertIsNot (loaded_pydoc , pydoc )
1165
+ self .assertEqual (loaded_pydoc .__name__ , 'pydoc' )
1166
+ self .assertEqual (loaded_pydoc .__file__ , pydoc .__file__ )
1167
+ self .assertEqual (loaded_pydoc .__spec__ , pydoc .__spec__ )
1168
+ finally :
1169
+ sys .modules ['pydoc' ] = pydoc
1160
1170
1161
1171
1162
1172
class TestDescriptions (unittest .TestCase ):
1173
+ def tearDown (self ):
1174
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1163
1175
1164
1176
def test_module (self ):
1165
1177
# Check that pydocfodder module can be described
@@ -1505,6 +1517,8 @@ def a_fn_with_https_link():
1505
1517
1506
1518
1507
1519
class PydocFodderTest (unittest .TestCase ):
1520
+ def tearDown (self ):
1521
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1508
1522
1509
1523
def getsection (self , text , beginline , endline ):
1510
1524
lines = text .splitlines ()
@@ -1632,6 +1646,8 @@ def test_html_doc_routines_in_module(self):
1632
1646
)
1633
1647
class PydocServerTest (unittest .TestCase ):
1634
1648
"""Tests for pydoc._start_server"""
1649
+ def tearDown (self ):
1650
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1635
1651
1636
1652
def test_server (self ):
1637
1653
# Minimal test that starts the server, checks that it works, then stops
@@ -1694,9 +1710,14 @@ def test_url_requests(self):
1694
1710
("foobar" , "Pydoc: Error - foobar" ),
1695
1711
]
1696
1712
1697
- with self .restrict_walk_packages ():
1698
- for url , title in requests :
1699
- self .call_url_handler (url , title )
1713
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1714
+ try :
1715
+ with self .restrict_walk_packages ():
1716
+ for url , title in requests :
1717
+ self .call_url_handler (url , title )
1718
+ finally :
1719
+ # Some requests reload the module and change sys.modules.
1720
+ sys .modules ['pydoc' ] = pydoc
1700
1721
1701
1722
1702
1723
class TestHelper (unittest .TestCase ):
@@ -1706,6 +1727,9 @@ def test_keywords(self):
1706
1727
1707
1728
1708
1729
class PydocWithMetaClasses (unittest .TestCase ):
1730
+ def tearDown (self ):
1731
+ self .assertIs (sys .modules ['pydoc' ], pydoc )
1732
+
1709
1733
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
1710
1734
'trace function introduces __locals__ unexpectedly' )
1711
1735
@requires_docstrings
0 commit comments