Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import pkgutil
import re
import stat
import string
import tempfile
import test.support
import time
import types
import typing
import unittest
Expand All @@ -23,6 +21,7 @@
import textwrap
from io import StringIO
from collections import namedtuple
from urllib.request import urlopen, urlcleanup
from test.support import import_helper
from test.support import os_helper
from test.support.script_helper import assert_python_ok, assert_python_failure
Expand Down Expand Up @@ -379,8 +378,6 @@ def call_url_handler(self, url, expected_title):
class PydocDocTest(unittest.TestCase):
maxDiff = None

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
Expand All @@ -396,9 +393,6 @@ def test_html_doc(self):
self.assertIn(mod_file, 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(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
Expand Down Expand Up @@ -457,8 +451,7 @@ def test_not_here(self):
self.assertEqual(expected, result,
"documentation for missing module found")

@unittest.skipIf(sys.flags.optimize >= 2,
'Docstrings are omitted with -OO and above')
@requires_docstrings
def test_not_ascii(self):
result = run_pydoc('test.test_pydoc.nonascii', PYTHONIOENCODING='ascii')
encoded = nonascii.__doc__.encode('ascii', 'backslashreplace')
Expand Down Expand Up @@ -612,15 +605,12 @@ def test_builtin_on_metaclasses(self):
# Testing that the subclasses section does not appear
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(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
def test_help_output_redirect(self):
# issue 940286, if output is set in Helper, then all output from
# Helper.help should be redirected
old_pattern = expected_text_pattern
getpager_old = pydoc.getpager
getpager_new = lambda: (lambda x: x)
self.maxDiff = None
Expand Down Expand Up @@ -682,8 +672,7 @@ def test_synopsis(self):
synopsis = pydoc.synopsis(TESTFN, {})
self.assertEqual(synopsis, 'line 1: h\xe9')

@unittest.skipIf(sys.flags.optimize >= 2,
'Docstrings are omitted with -OO and above')
@requires_docstrings
def test_synopsis_sourceless(self):
os = import_helper.import_fresh_module('os')
expected = os.__doc__.splitlines()[0]
Expand Down Expand Up @@ -745,6 +734,7 @@ def method_returning_true(self):
methods = pydoc.allmethods(TestClass)
self.assertDictEqual(methods, expected)

@requires_docstrings
def test_method_aliases(self):
class A:
def tkraise(self, aboveThis=None):
Expand Down Expand Up @@ -801,10 +791,10 @@ class B(A)
''' % __name__)

doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc())
expected_text = """
expected_text = f"""
Python Library Documentation

class B in module test.test_pydoc
class B in module {__name__}
class B(A)
Method resolution order:
B
Expand Down Expand Up @@ -1210,12 +1200,12 @@ def __get__(self, obj, cls):
class X:
attr = Descr()

self.assertEqual(self._get_summary_lines(X.attr), """\
<test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""")
self.assertEqual(self._get_summary_lines(X.attr), f"""\
<{__name__}.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""")

X.attr.__doc__ = 'Custom descriptor'
self.assertEqual(self._get_summary_lines(X.attr), """\
<test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
self.assertEqual(self._get_summary_lines(X.attr), f"""\
<{__name__}.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
Custom descriptor
""")

Expand Down Expand Up @@ -1274,6 +1264,7 @@ async def an_async_generator():
'async <a name="-an_async_generator"><strong>an_async_generator',
html)

@requires_docstrings
def test_html_for_https_links(self):
def a_fn_with_https_link():
"""a link https://localhost/"""
Expand All @@ -1285,29 +1276,43 @@ def a_fn_with_https_link():
html
)


class PydocServerTest(unittest.TestCase):
"""Tests for pydoc._start_server"""

def test_server(self):

# Minimal test that starts the server, then stops it.
# Minimal test that starts the server, checks that it works, then stops
# it and checks its cleanup.
def my_url_handler(url, content_type):
text = 'the URL sent was: (%s, %s)' % (url, content_type)
return text

serverthread = pydoc._start_server(my_url_handler, hostname='0.0.0.0', port=0)
self.assertIn('0.0.0.0', serverthread.docserver.address)

starttime = time.monotonic()
timeout = test.support.SHORT_TIMEOUT
serverthread = pydoc._start_server(
my_url_handler,
hostname='localhost',
port=0,
)
self.assertEqual(serverthread.error, None)
self.assertTrue(serverthread.serving)
self.addCleanup(
lambda: serverthread.stop() if serverthread.serving else None
)
self.assertIn('localhost', serverthread.url)

while serverthread.serving:
time.sleep(.01)
if serverthread.serving and time.monotonic() - starttime > timeout:
serverthread.stop()
break
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(),
)

self.assertEqual(serverthread.error, None)
serverthread.stop()
self.assertFalse(serverthread.serving)
self.assertIsNone(serverthread.docserver)
self.assertIsNone(serverthread.url)


class PydocUrlHandlerTest(PydocBaseTest):
Expand Down Expand Up @@ -1346,11 +1351,11 @@ def test_keywords(self):
self.assertEqual(sorted(pydoc.Helper.keywords),
sorted(keyword.kwlist))


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(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
def test_DynamicClassAttribute(self):
class Meta(type):
def __getattr__(self, name):
Expand All @@ -1371,10 +1376,9 @@ def ham(self):
result = output.getvalue().strip()
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(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
def test_virtualClassAttributeWithOneMeta(self):
class Meta(type):
def __dir__(cls):
Expand All @@ -1392,10 +1396,9 @@ class Class(metaclass=Meta):
result = output.getvalue().strip()
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(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
def test_virtualClassAttributeWithTwoMeta(self):
class Meta1(type):
def __dir__(cls):
Expand Down Expand Up @@ -1424,7 +1427,6 @@ class Class1(metaclass=Meta1):
pass
class Class2(Class1, metaclass=Meta3):
pass
fail1 = fail2 = False
output = StringIO()
helper = pydoc.Helper(output=output)
helper(Class1)
Expand All @@ -1438,10 +1440,9 @@ class Class2(Class1, metaclass=Meta3):
result2 = output.getvalue().strip()
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(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
def test_buggy_dir(self):
class M(type):
def __dir__(cls):
Expand Down Expand Up @@ -1502,7 +1503,6 @@ def test_sys_path_adjustment_removes_argv0_dir(self):
trailing_argv0dir = clean_path + [self.argv0dir]
self.assertEqual(self._get_revised_path(trailing_argv0dir), expected_path)


def test_sys_path_adjustment_protects_pydoc_dir(self):
def _get_revised_path(given_path):
return self._get_revised_path(given_path, argv0=pydoc.__file__)
Expand Down