10
10
import pkgutil
11
11
import re
12
12
import stat
13
- import string
14
13
import tempfile
15
14
import test .support
16
- import time
17
15
import types
18
16
import typing
19
17
import unittest
23
21
import textwrap
24
22
from io import StringIO
25
23
from collections import namedtuple
24
+ from urllib .request import urlopen , urlcleanup
26
25
from test .support import import_helper
27
26
from test .support import os_helper
28
27
from test .support .script_helper import assert_python_ok , assert_python_failure
@@ -379,8 +378,6 @@ def call_url_handler(self, url, expected_title):
379
378
class PydocDocTest (unittest .TestCase ):
380
379
maxDiff = None
381
380
382
- @unittest .skipIf (sys .flags .optimize >= 2 ,
383
- "Docstrings are omitted with -O2 and above" )
384
381
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
385
382
'trace function introduces __locals__ unexpectedly' )
386
383
@requires_docstrings
@@ -396,9 +393,6 @@ def test_html_doc(self):
396
393
self .assertIn (mod_file , result )
397
394
self .assertIn (doc_loc , result )
398
395
399
-
400
- @unittest .skipIf (sys .flags .optimize >= 2 ,
401
- "Docstrings are omitted with -O2 and above" )
402
396
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
403
397
'trace function introduces __locals__ unexpectedly' )
404
398
@requires_docstrings
@@ -457,8 +451,7 @@ def test_not_here(self):
457
451
self .assertEqual (expected , result ,
458
452
"documentation for missing module found" )
459
453
460
- @unittest .skipIf (sys .flags .optimize >= 2 ,
461
- 'Docstrings are omitted with -OO and above' )
454
+ @requires_docstrings
462
455
def test_not_ascii (self ):
463
456
result = run_pydoc ('test.test_pydoc.nonascii' , PYTHONIOENCODING = 'ascii' )
464
457
encoded = nonascii .__doc__ .encode ('ascii' , 'backslashreplace' )
@@ -612,15 +605,12 @@ def test_builtin_on_metaclasses(self):
612
605
# Testing that the subclasses section does not appear
613
606
self .assertNotIn ('Built-in subclasses' , text )
614
607
615
- @unittest .skipIf (sys .flags .optimize >= 2 ,
616
- 'Docstrings are omitted with -O2 and above' )
617
608
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
618
609
'trace function introduces __locals__ unexpectedly' )
619
610
@requires_docstrings
620
611
def test_help_output_redirect (self ):
621
612
# issue 940286, if output is set in Helper, then all output from
622
613
# Helper.help should be redirected
623
- old_pattern = expected_text_pattern
624
614
getpager_old = pydoc .getpager
625
615
getpager_new = lambda : (lambda x : x )
626
616
self .maxDiff = None
@@ -682,8 +672,7 @@ def test_synopsis(self):
682
672
synopsis = pydoc .synopsis (TESTFN , {})
683
673
self .assertEqual (synopsis , 'line 1: h\xe9 ' )
684
674
685
- @unittest .skipIf (sys .flags .optimize >= 2 ,
686
- 'Docstrings are omitted with -OO and above' )
675
+ @requires_docstrings
687
676
def test_synopsis_sourceless (self ):
688
677
os = import_helper .import_fresh_module ('os' )
689
678
expected = os .__doc__ .splitlines ()[0 ]
@@ -745,6 +734,7 @@ def method_returning_true(self):
745
734
methods = pydoc .allmethods (TestClass )
746
735
self .assertDictEqual (methods , expected )
747
736
737
+ @requires_docstrings
748
738
def test_method_aliases (self ):
749
739
class A :
750
740
def tkraise (self , aboveThis = None ):
@@ -801,10 +791,10 @@ class B(A)
801
791
''' % __name__ )
802
792
803
793
doc = pydoc .render_doc (B , renderer = pydoc .HTMLDoc ())
804
- expected_text = """
794
+ expected_text = f """
805
795
Python Library Documentation
806
796
807
- class B in module test.test_pydoc
797
+ class B in module { __name__ }
808
798
class B(A)
809
799
Method resolution order:
810
800
B
@@ -1210,12 +1200,12 @@ def __get__(self, obj, cls):
1210
1200
class X :
1211
1201
attr = Descr ()
1212
1202
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>""" )
1215
1205
1216
1206
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>
1219
1209
Custom descriptor
1220
1210
""" )
1221
1211
@@ -1274,6 +1264,7 @@ async def an_async_generator():
1274
1264
'async <a name="-an_async_generator"><strong>an_async_generator' ,
1275
1265
html )
1276
1266
1267
+ @requires_docstrings
1277
1268
def test_html_for_https_links (self ):
1278
1269
def a_fn_with_https_link ():
1279
1270
"""a link https://localhost/"""
@@ -1285,29 +1276,43 @@ def a_fn_with_https_link():
1285
1276
html
1286
1277
)
1287
1278
1279
+
1288
1280
class PydocServerTest (unittest .TestCase ):
1289
1281
"""Tests for pydoc._start_server"""
1290
1282
1291
1283
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 .
1294
1286
def my_url_handler (url , content_type ):
1295
1287
text = 'the URL sent was: (%s, %s)' % (url , content_type )
1296
1288
return text
1297
1289
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 )
1303
1301
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
+ )
1309
1311
1310
- self .assertEqual (serverthread .error , None )
1312
+ serverthread .stop ()
1313
+ self .assertFalse (serverthread .serving )
1314
+ self .assertIsNone (serverthread .docserver )
1315
+ self .assertIsNone (serverthread .url )
1311
1316
1312
1317
1313
1318
class PydocUrlHandlerTest (PydocBaseTest ):
@@ -1346,11 +1351,11 @@ def test_keywords(self):
1346
1351
self .assertEqual (sorted (pydoc .Helper .keywords ),
1347
1352
sorted (keyword .kwlist ))
1348
1353
1354
+
1349
1355
class PydocWithMetaClasses (unittest .TestCase ):
1350
- @unittest .skipIf (sys .flags .optimize >= 2 ,
1351
- "Docstrings are omitted with -O2 and above" )
1352
1356
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
1353
1357
'trace function introduces __locals__ unexpectedly' )
1358
+ @requires_docstrings
1354
1359
def test_DynamicClassAttribute (self ):
1355
1360
class Meta (type ):
1356
1361
def __getattr__ (self , name ):
@@ -1371,10 +1376,9 @@ def ham(self):
1371
1376
result = output .getvalue ().strip ()
1372
1377
self .assertEqual (expected_text , result )
1373
1378
1374
- @unittest .skipIf (sys .flags .optimize >= 2 ,
1375
- "Docstrings are omitted with -O2 and above" )
1376
1379
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
1377
1380
'trace function introduces __locals__ unexpectedly' )
1381
+ @requires_docstrings
1378
1382
def test_virtualClassAttributeWithOneMeta (self ):
1379
1383
class Meta (type ):
1380
1384
def __dir__ (cls ):
@@ -1392,10 +1396,9 @@ class Class(metaclass=Meta):
1392
1396
result = output .getvalue ().strip ()
1393
1397
self .assertEqual (expected_text , result )
1394
1398
1395
- @unittest .skipIf (sys .flags .optimize >= 2 ,
1396
- "Docstrings are omitted with -O2 and above" )
1397
1399
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
1398
1400
'trace function introduces __locals__ unexpectedly' )
1401
+ @requires_docstrings
1399
1402
def test_virtualClassAttributeWithTwoMeta (self ):
1400
1403
class Meta1 (type ):
1401
1404
def __dir__ (cls ):
@@ -1424,7 +1427,6 @@ class Class1(metaclass=Meta1):
1424
1427
pass
1425
1428
class Class2 (Class1 , metaclass = Meta3 ):
1426
1429
pass
1427
- fail1 = fail2 = False
1428
1430
output = StringIO ()
1429
1431
helper = pydoc .Helper (output = output )
1430
1432
helper (Class1 )
@@ -1438,10 +1440,9 @@ class Class2(Class1, metaclass=Meta3):
1438
1440
result2 = output .getvalue ().strip ()
1439
1441
self .assertEqual (expected_text2 , result2 )
1440
1442
1441
- @unittest .skipIf (sys .flags .optimize >= 2 ,
1442
- "Docstrings are omitted with -O2 and above" )
1443
1443
@unittest .skipIf (hasattr (sys , 'gettrace' ) and sys .gettrace (),
1444
1444
'trace function introduces __locals__ unexpectedly' )
1445
+ @requires_docstrings
1445
1446
def test_buggy_dir (self ):
1446
1447
class M (type ):
1447
1448
def __dir__ (cls ):
@@ -1502,7 +1503,6 @@ def test_sys_path_adjustment_removes_argv0_dir(self):
1502
1503
trailing_argv0dir = clean_path + [self .argv0dir ]
1503
1504
self .assertEqual (self ._get_revised_path (trailing_argv0dir ), expected_path )
1504
1505
1505
-
1506
1506
def test_sys_path_adjustment_protects_pydoc_dir (self ):
1507
1507
def _get_revised_path (given_path ):
1508
1508
return self ._get_revised_path (given_path , argv0 = pydoc .__file__ )
0 commit comments