mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
General cleanup of test_pydoc (GH-29459)
- Uniform usage of `test.support.requires_docstrings` instead of a local check for `-OO`. - Added `requires_docstrings` to a couple more methods that need it. - Replaced a few instances of `test.test_pydoc` with `__name__` to allow for different methods of running just this test file. - Rewrote `test_server` to run faster and better test the server. - Removed unused import. - Removed unused locals. - Minor whitespace cleanups.
This commit is contained in:
parent
be3cd5c05d
commit
fd41125f0f
1 changed files with 44 additions and 44 deletions
|
@ -10,10 +10,8 @@ import _pickle
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import re
|
import re
|
||||||
import stat
|
import stat
|
||||||
import string
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import test.support
|
import test.support
|
||||||
import time
|
|
||||||
import types
|
import types
|
||||||
import typing
|
import typing
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -23,6 +21,7 @@ import xml.etree.ElementTree
|
||||||
import textwrap
|
import textwrap
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from urllib.request import urlopen, urlcleanup
|
||||||
from test.support import import_helper
|
from test.support import import_helper
|
||||||
from test.support import os_helper
|
from test.support import os_helper
|
||||||
from test.support.script_helper import assert_python_ok, assert_python_failure
|
from test.support.script_helper import assert_python_ok, assert_python_failure
|
||||||
|
@ -379,8 +378,6 @@ class PydocBaseTest(unittest.TestCase):
|
||||||
class PydocDocTest(unittest.TestCase):
|
class PydocDocTest(unittest.TestCase):
|
||||||
maxDiff = None
|
maxDiff = None
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
"Docstrings are omitted with -O2 and above")
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
@requires_docstrings
|
@requires_docstrings
|
||||||
|
@ -396,9 +393,6 @@ class PydocDocTest(unittest.TestCase):
|
||||||
self.assertIn(mod_file, result)
|
self.assertIn(mod_file, result)
|
||||||
self.assertIn(doc_loc, result)
|
self.assertIn(doc_loc, result)
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
"Docstrings are omitted with -O2 and above")
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
@requires_docstrings
|
@requires_docstrings
|
||||||
|
@ -457,8 +451,7 @@ class PydocDocTest(unittest.TestCase):
|
||||||
self.assertEqual(expected, result,
|
self.assertEqual(expected, result,
|
||||||
"documentation for missing module found")
|
"documentation for missing module found")
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@requires_docstrings
|
||||||
'Docstrings are omitted with -OO and above')
|
|
||||||
def test_not_ascii(self):
|
def test_not_ascii(self):
|
||||||
result = run_pydoc('test.test_pydoc.nonascii', PYTHONIOENCODING='ascii')
|
result = run_pydoc('test.test_pydoc.nonascii', PYTHONIOENCODING='ascii')
|
||||||
encoded = nonascii.__doc__.encode('ascii', 'backslashreplace')
|
encoded = nonascii.__doc__.encode('ascii', 'backslashreplace')
|
||||||
|
@ -612,15 +605,12 @@ class PydocDocTest(unittest.TestCase):
|
||||||
# Testing that the subclasses section does not appear
|
# Testing that the subclasses section does not appear
|
||||||
self.assertNotIn('Built-in subclasses', text)
|
self.assertNotIn('Built-in subclasses', text)
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
'Docstrings are omitted with -O2 and above')
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
@requires_docstrings
|
@requires_docstrings
|
||||||
def test_help_output_redirect(self):
|
def test_help_output_redirect(self):
|
||||||
# issue 940286, if output is set in Helper, then all output from
|
# issue 940286, if output is set in Helper, then all output from
|
||||||
# Helper.help should be redirected
|
# Helper.help should be redirected
|
||||||
old_pattern = expected_text_pattern
|
|
||||||
getpager_old = pydoc.getpager
|
getpager_old = pydoc.getpager
|
||||||
getpager_new = lambda: (lambda x: x)
|
getpager_new = lambda: (lambda x: x)
|
||||||
self.maxDiff = None
|
self.maxDiff = None
|
||||||
|
@ -682,8 +672,7 @@ class PydocDocTest(unittest.TestCase):
|
||||||
synopsis = pydoc.synopsis(TESTFN, {})
|
synopsis = pydoc.synopsis(TESTFN, {})
|
||||||
self.assertEqual(synopsis, 'line 1: h\xe9')
|
self.assertEqual(synopsis, 'line 1: h\xe9')
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@requires_docstrings
|
||||||
'Docstrings are omitted with -OO and above')
|
|
||||||
def test_synopsis_sourceless(self):
|
def test_synopsis_sourceless(self):
|
||||||
os = import_helper.import_fresh_module('os')
|
os = import_helper.import_fresh_module('os')
|
||||||
expected = os.__doc__.splitlines()[0]
|
expected = os.__doc__.splitlines()[0]
|
||||||
|
@ -745,6 +734,7 @@ class PydocDocTest(unittest.TestCase):
|
||||||
methods = pydoc.allmethods(TestClass)
|
methods = pydoc.allmethods(TestClass)
|
||||||
self.assertDictEqual(methods, expected)
|
self.assertDictEqual(methods, expected)
|
||||||
|
|
||||||
|
@requires_docstrings
|
||||||
def test_method_aliases(self):
|
def test_method_aliases(self):
|
||||||
class A:
|
class A:
|
||||||
def tkraise(self, aboveThis=None):
|
def tkraise(self, aboveThis=None):
|
||||||
|
@ -801,10 +791,10 @@ class B(A)
|
||||||
''' % __name__)
|
''' % __name__)
|
||||||
|
|
||||||
doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc())
|
doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc())
|
||||||
expected_text = """
|
expected_text = f"""
|
||||||
Python Library Documentation
|
Python Library Documentation
|
||||||
|
|
||||||
class B in module test.test_pydoc
|
class B in module {__name__}
|
||||||
class B(A)
|
class B(A)
|
||||||
Method resolution order:
|
Method resolution order:
|
||||||
B
|
B
|
||||||
|
@ -1210,12 +1200,12 @@ cm(x) method of builtins.type instance
|
||||||
class X:
|
class X:
|
||||||
attr = Descr()
|
attr = Descr()
|
||||||
|
|
||||||
self.assertEqual(self._get_summary_lines(X.attr), """\
|
self.assertEqual(self._get_summary_lines(X.attr), f"""\
|
||||||
<test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""")
|
<{__name__}.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""")
|
||||||
|
|
||||||
X.attr.__doc__ = 'Custom descriptor'
|
X.attr.__doc__ = 'Custom descriptor'
|
||||||
self.assertEqual(self._get_summary_lines(X.attr), """\
|
self.assertEqual(self._get_summary_lines(X.attr), f"""\
|
||||||
<test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
|
<{__name__}.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
|
||||||
Custom descriptor
|
Custom descriptor
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@ -1274,6 +1264,7 @@ foo
|
||||||
'async <a name="-an_async_generator"><strong>an_async_generator',
|
'async <a name="-an_async_generator"><strong>an_async_generator',
|
||||||
html)
|
html)
|
||||||
|
|
||||||
|
@requires_docstrings
|
||||||
def test_html_for_https_links(self):
|
def test_html_for_https_links(self):
|
||||||
def a_fn_with_https_link():
|
def a_fn_with_https_link():
|
||||||
"""a link https://localhost/"""
|
"""a link https://localhost/"""
|
||||||
|
@ -1285,29 +1276,43 @@ foo
|
||||||
html
|
html
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class PydocServerTest(unittest.TestCase):
|
class PydocServerTest(unittest.TestCase):
|
||||||
"""Tests for pydoc._start_server"""
|
"""Tests for pydoc._start_server"""
|
||||||
|
|
||||||
def test_server(self):
|
def test_server(self):
|
||||||
|
# Minimal test that starts the server, checks that it works, then stops
|
||||||
# Minimal test that starts the server, then stops it.
|
# it and checks its cleanup.
|
||||||
def my_url_handler(url, content_type):
|
def my_url_handler(url, content_type):
|
||||||
text = 'the URL sent was: (%s, %s)' % (url, content_type)
|
text = 'the URL sent was: (%s, %s)' % (url, content_type)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
serverthread = pydoc._start_server(my_url_handler, hostname='0.0.0.0', port=0)
|
serverthread = pydoc._start_server(
|
||||||
self.assertIn('0.0.0.0', serverthread.docserver.address)
|
my_url_handler,
|
||||||
|
hostname='localhost',
|
||||||
starttime = time.monotonic()
|
port=0,
|
||||||
timeout = test.support.SHORT_TIMEOUT
|
)
|
||||||
|
|
||||||
while serverthread.serving:
|
|
||||||
time.sleep(.01)
|
|
||||||
if serverthread.serving and time.monotonic() - starttime > timeout:
|
|
||||||
serverthread.stop()
|
|
||||||
break
|
|
||||||
|
|
||||||
self.assertEqual(serverthread.error, None)
|
self.assertEqual(serverthread.error, None)
|
||||||
|
self.assertTrue(serverthread.serving)
|
||||||
|
self.addCleanup(
|
||||||
|
lambda: serverthread.stop() if serverthread.serving else None
|
||||||
|
)
|
||||||
|
self.assertIn('localhost', serverthread.url)
|
||||||
|
|
||||||
|
self.addCleanup(urlcleanup)
|
||||||
|
self.assertEqual(
|
||||||
|
b'the URL sent was: (/test, text/html)',
|
||||||
|
urlopen(urllib.parse.urljoin(serverthread.url, '/test')).read(),
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
b'the URL sent was: (/test.css, text/css)',
|
||||||
|
urlopen(urllib.parse.urljoin(serverthread.url, '/test.css')).read(),
|
||||||
|
)
|
||||||
|
|
||||||
|
serverthread.stop()
|
||||||
|
self.assertFalse(serverthread.serving)
|
||||||
|
self.assertIsNone(serverthread.docserver)
|
||||||
|
self.assertIsNone(serverthread.url)
|
||||||
|
|
||||||
|
|
||||||
class PydocUrlHandlerTest(PydocBaseTest):
|
class PydocUrlHandlerTest(PydocBaseTest):
|
||||||
|
@ -1346,11 +1351,11 @@ class TestHelper(unittest.TestCase):
|
||||||
self.assertEqual(sorted(pydoc.Helper.keywords),
|
self.assertEqual(sorted(pydoc.Helper.keywords),
|
||||||
sorted(keyword.kwlist))
|
sorted(keyword.kwlist))
|
||||||
|
|
||||||
|
|
||||||
class PydocWithMetaClasses(unittest.TestCase):
|
class PydocWithMetaClasses(unittest.TestCase):
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
"Docstrings are omitted with -O2 and above")
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
|
@requires_docstrings
|
||||||
def test_DynamicClassAttribute(self):
|
def test_DynamicClassAttribute(self):
|
||||||
class Meta(type):
|
class Meta(type):
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
|
@ -1371,10 +1376,9 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
result = output.getvalue().strip()
|
result = output.getvalue().strip()
|
||||||
self.assertEqual(expected_text, result)
|
self.assertEqual(expected_text, result)
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
"Docstrings are omitted with -O2 and above")
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
|
@requires_docstrings
|
||||||
def test_virtualClassAttributeWithOneMeta(self):
|
def test_virtualClassAttributeWithOneMeta(self):
|
||||||
class Meta(type):
|
class Meta(type):
|
||||||
def __dir__(cls):
|
def __dir__(cls):
|
||||||
|
@ -1392,10 +1396,9 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
result = output.getvalue().strip()
|
result = output.getvalue().strip()
|
||||||
self.assertEqual(expected_text, result)
|
self.assertEqual(expected_text, result)
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
"Docstrings are omitted with -O2 and above")
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
|
@requires_docstrings
|
||||||
def test_virtualClassAttributeWithTwoMeta(self):
|
def test_virtualClassAttributeWithTwoMeta(self):
|
||||||
class Meta1(type):
|
class Meta1(type):
|
||||||
def __dir__(cls):
|
def __dir__(cls):
|
||||||
|
@ -1424,7 +1427,6 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
pass
|
pass
|
||||||
class Class2(Class1, metaclass=Meta3):
|
class Class2(Class1, metaclass=Meta3):
|
||||||
pass
|
pass
|
||||||
fail1 = fail2 = False
|
|
||||||
output = StringIO()
|
output = StringIO()
|
||||||
helper = pydoc.Helper(output=output)
|
helper = pydoc.Helper(output=output)
|
||||||
helper(Class1)
|
helper(Class1)
|
||||||
|
@ -1438,10 +1440,9 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
result2 = output.getvalue().strip()
|
result2 = output.getvalue().strip()
|
||||||
self.assertEqual(expected_text2, result2)
|
self.assertEqual(expected_text2, result2)
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
||||||
"Docstrings are omitted with -O2 and above")
|
|
||||||
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
|
||||||
'trace function introduces __locals__ unexpectedly')
|
'trace function introduces __locals__ unexpectedly')
|
||||||
|
@requires_docstrings
|
||||||
def test_buggy_dir(self):
|
def test_buggy_dir(self):
|
||||||
class M(type):
|
class M(type):
|
||||||
def __dir__(cls):
|
def __dir__(cls):
|
||||||
|
@ -1502,7 +1503,6 @@ class TestInternalUtilities(unittest.TestCase):
|
||||||
trailing_argv0dir = clean_path + [self.argv0dir]
|
trailing_argv0dir = clean_path + [self.argv0dir]
|
||||||
self.assertEqual(self._get_revised_path(trailing_argv0dir), expected_path)
|
self.assertEqual(self._get_revised_path(trailing_argv0dir), expected_path)
|
||||||
|
|
||||||
|
|
||||||
def test_sys_path_adjustment_protects_pydoc_dir(self):
|
def test_sys_path_adjustment_protects_pydoc_dir(self):
|
||||||
def _get_revised_path(given_path):
|
def _get_revised_path(given_path):
|
||||||
return self._get_revised_path(given_path, argv0=pydoc.__file__)
|
return self._get_revised_path(given_path, argv0=pydoc.__file__)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue