1010import pkgutil
1111import re
1212import stat
13- import string
1413import tempfile
1514import test .support
16- import time
1715import types
1816import typing
1917import unittest
2321import textwrap
2422from io import StringIO
2523from collections import namedtuple
24+ from urllib .request import urlopen , urlcleanup
2625from test .support import import_helper
2726from test .support import os_helper
2827from test .support .script_helper import assert_python_ok , assert_python_failure
@@ -379,8 +378,6 @@ def call_url_handler(self, url, expected_title):
379378class PydocDocTest (unittest .TestCase ):
380379 maxDiff = None
381380
382- @unittest .skipIf (sys .flags .optimize >= 2 ,
383- "Docstrings are omitted with -O2 and above" )
384381 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
385382 'trace function introduces __locals__ unexpectedly' )
386383 @requires_docstrings
@@ -396,9 +393,6 @@ def test_html_doc(self):
396393 self .assertIn (mod_file , result )
397394 self .assertIn (doc_loc , result )
398395
399-
400- @unittest .skipIf (sys .flags .optimize >= 2 ,
401- "Docstrings are omitted with -O2 and above" )
402396 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
403397 'trace function introduces __locals__ unexpectedly' )
404398 @requires_docstrings
@@ -457,8 +451,7 @@ def test_not_here(self):
457451 self .assertEqual (expected , result ,
458452 "documentation for missing module found" )
459453
460- @unittest .skipIf (sys .flags .optimize >= 2 ,
461- 'Docstrings are omitted with -OO and above' )
454+ @requires_docstrings
462455 def test_not_ascii (self ):
463456 result = run_pydoc ('test.test_pydoc.nonascii' , PYTHONIOENCODING = 'ascii' )
464457 encoded = nonascii .__doc__ .encode ('ascii' , 'backslashreplace' )
@@ -612,15 +605,12 @@ def test_builtin_on_metaclasses(self):
612605 # Testing that the subclasses section does not appear
613606 self .assertNotIn ('Built-in subclasses' , text )
614607
615- @unittest .skipIf (sys .flags .optimize >= 2 ,
616- 'Docstrings are omitted with -O2 and above' )
617608 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
618609 'trace function introduces __locals__ unexpectedly' )
619610 @requires_docstrings
620611 def test_help_output_redirect (self ):
621612 # issue 940286, if output is set in Helper, then all output from
622613 # Helper.help should be redirected
623- old_pattern = expected_text_pattern
624614 getpager_old = pydoc .getpager
625615 getpager_new = lambda : (lambda x : x )
626616 self .maxDiff = None
@@ -682,8 +672,7 @@ def test_synopsis(self):
682672 synopsis = pydoc .synopsis (TESTFN , {})
683673 self .assertEqual (synopsis , 'line 1: h\xe9 ' )
684674
685- @unittest .skipIf (sys .flags .optimize >= 2 ,
686- 'Docstrings are omitted with -OO and above' )
675+ @requires_docstrings
687676 def test_synopsis_sourceless (self ):
688677 os = import_helper .import_fresh_module ('os' )
689678 expected = os .__doc__ .splitlines ()[0 ]
@@ -745,6 +734,7 @@ def method_returning_true(self):
745734 methods = pydoc .allmethods (TestClass )
746735 self .assertDictEqual (methods , expected )
747736
737+ @requires_docstrings
748738 def test_method_aliases (self ):
749739 class A :
750740 def tkraise (self , aboveThis = None ):
@@ -801,10 +791,10 @@ class B(A)
801791''' % __name__ )
802792
803793 doc = pydoc .render_doc (B , renderer = pydoc .HTMLDoc ())
804- expected_text = """
794+ expected_text = f """
805795Python Library Documentation
806796
807- class B in module test.test_pydoc
797+ class B in module { __name__ }
808798class B(A)
809799 Method resolution order:
810800 B
@@ -1210,12 +1200,12 @@ def __get__(self, obj, cls):
12101200 class X :
12111201 attr = Descr ()
12121202
1213- self .assertEqual (self ._get_summary_lines (X .attr ), """\
1214- <test.test_pydoc .TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""" )
1203+ self .assertEqual (self ._get_summary_lines (X .attr ), f """\
1204+ <{ __name__ } .TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""" )
12151205
12161206 X .attr .__doc__ = 'Custom descriptor'
1217- self .assertEqual (self ._get_summary_lines (X .attr ), """\
1218- <test.test_pydoc .TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
1207+ self .assertEqual (self ._get_summary_lines (X .attr ), f """\
1208+ <{ __name__ } .TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
12191209 Custom descriptor
12201210""" )
12211211
@@ -1274,6 +1264,7 @@ async def an_async_generator():
12741264 'async <a name="-an_async_generator"><strong>an_async_generator' ,
12751265 html )
12761266
1267+ @requires_docstrings
12771268 def test_html_for_https_links (self ):
12781269 def a_fn_with_https_link ():
12791270 """a link https://localhost/"""
@@ -1285,29 +1276,43 @@ def a_fn_with_https_link():
12851276 html
12861277 )
12871278
1279+
12881280class PydocServerTest (unittest .TestCase ):
12891281 """Tests for pydoc._start_server"""
12901282
12911283 def test_server (self ):
1292-
1293- # Minimal test that starts the server, then stops it .
1284+ # Minimal test that starts the server, checks that it works, then stops
1285+ # it and checks its cleanup .
12941286 def my_url_handler (url , content_type ):
12951287 text = 'the URL sent was: (%s, %s)' % (url , content_type )
12961288 return text
12971289
1298- serverthread = pydoc ._start_server (my_url_handler , hostname = '0.0.0.0' , port = 0 )
1299- self .assertIn ('0.0.0.0' , serverthread .docserver .address )
1300-
1301- starttime = time .monotonic ()
1302- timeout = test .support .SHORT_TIMEOUT
1290+ serverthread = pydoc ._start_server (
1291+ my_url_handler ,
1292+ hostname = 'localhost' ,
1293+ port = 0 ,
1294+ )
1295+ self .assertEqual (serverthread .error , None )
1296+ self .assertTrue (serverthread .serving )
1297+ self .addCleanup (
1298+ lambda : serverthread .stop () if serverthread .serving else None
1299+ )
1300+ self .assertIn ('localhost' , serverthread .url )
13031301
1304- while serverthread .serving :
1305- time .sleep (.01 )
1306- if serverthread .serving and time .monotonic () - starttime > timeout :
1307- serverthread .stop ()
1308- break
1302+ self .addCleanup (urlcleanup )
1303+ self .assertEqual (
1304+ b'the URL sent was: (/test, text/html)' ,
1305+ urlopen (urllib .parse .urljoin (serverthread .url , '/test' )).read (),
1306+ )
1307+ self .assertEqual (
1308+ b'the URL sent was: (/test.css, text/css)' ,
1309+ urlopen (urllib .parse .urljoin (serverthread .url , '/test.css' )).read (),
1310+ )
13091311
1310- self .assertEqual (serverthread .error , None )
1312+ serverthread .stop ()
1313+ self .assertFalse (serverthread .serving )
1314+ self .assertIsNone (serverthread .docserver )
1315+ self .assertIsNone (serverthread .url )
13111316
13121317
13131318class PydocUrlHandlerTest (PydocBaseTest ):
@@ -1346,11 +1351,11 @@ def test_keywords(self):
13461351 self .assertEqual (sorted (pydoc .Helper .keywords ),
13471352 sorted (keyword .kwlist ))
13481353
1354+
13491355class PydocWithMetaClasses (unittest .TestCase ):
1350- @unittest .skipIf (sys .flags .optimize >= 2 ,
1351- "Docstrings are omitted with -O2 and above" )
13521356 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
13531357 'trace function introduces __locals__ unexpectedly' )
1358+ @requires_docstrings
13541359 def test_DynamicClassAttribute (self ):
13551360 class Meta (type ):
13561361 def __getattr__ (self , name ):
@@ -1371,10 +1376,9 @@ def ham(self):
13711376 result = output .getvalue ().strip ()
13721377 self .assertEqual (expected_text , result )
13731378
1374- @unittest .skipIf (sys .flags .optimize >= 2 ,
1375- "Docstrings are omitted with -O2 and above" )
13761379 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
13771380 'trace function introduces __locals__ unexpectedly' )
1381+ @requires_docstrings
13781382 def test_virtualClassAttributeWithOneMeta (self ):
13791383 class Meta (type ):
13801384 def __dir__ (cls ):
@@ -1392,10 +1396,9 @@ class Class(metaclass=Meta):
13921396 result = output .getvalue ().strip ()
13931397 self .assertEqual (expected_text , result )
13941398
1395- @unittest .skipIf (sys .flags .optimize >= 2 ,
1396- "Docstrings are omitted with -O2 and above" )
13971399 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
13981400 'trace function introduces __locals__ unexpectedly' )
1401+ @requires_docstrings
13991402 def test_virtualClassAttributeWithTwoMeta (self ):
14001403 class Meta1 (type ):
14011404 def __dir__ (cls ):
@@ -1424,7 +1427,6 @@ class Class1(metaclass=Meta1):
14241427 pass
14251428 class Class2 (Class1 , metaclass = Meta3 ):
14261429 pass
1427- fail1 = fail2 = False
14281430 output = StringIO ()
14291431 helper = pydoc .Helper (output = output )
14301432 helper (Class1 )
@@ -1438,10 +1440,9 @@ class Class2(Class1, metaclass=Meta3):
14381440 result2 = output .getvalue ().strip ()
14391441 self .assertEqual (expected_text2 , result2 )
14401442
1441- @unittest .skipIf (sys .flags .optimize >= 2 ,
1442- "Docstrings are omitted with -O2 and above" )
14431443 @unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
14441444 'trace function introduces __locals__ unexpectedly' )
1445+ @requires_docstrings
14451446 def test_buggy_dir (self ):
14461447 class M (type ):
14471448 def __dir__ (cls ):
@@ -1502,7 +1503,6 @@ def test_sys_path_adjustment_removes_argv0_dir(self):
15021503 trailing_argv0dir = clean_path + [self .argv0dir ]
15031504 self .assertEqual (self ._get_revised_path (trailing_argv0dir ), expected_path )
15041505
1505-
15061506 def test_sys_path_adjustment_protects_pydoc_dir (self ):
15071507 def _get_revised_path (given_path ):
15081508 return self ._get_revised_path (given_path , argv0 = pydoc .__file__ )
0 commit comments