1 | n/a | import os |
---|
2 | n/a | import sys |
---|
3 | n/a | import builtins |
---|
4 | n/a | import contextlib |
---|
5 | n/a | import importlib.util |
---|
6 | n/a | import inspect |
---|
7 | n/a | import pydoc |
---|
8 | n/a | import py_compile |
---|
9 | n/a | import keyword |
---|
10 | n/a | import _pickle |
---|
11 | n/a | import pkgutil |
---|
12 | n/a | import re |
---|
13 | n/a | import stat |
---|
14 | n/a | import string |
---|
15 | n/a | import test.support |
---|
16 | n/a | import time |
---|
17 | n/a | import types |
---|
18 | n/a | import typing |
---|
19 | n/a | import unittest |
---|
20 | n/a | import urllib.parse |
---|
21 | n/a | import xml.etree |
---|
22 | n/a | import xml.etree.ElementTree |
---|
23 | n/a | import textwrap |
---|
24 | n/a | from io import StringIO |
---|
25 | n/a | from collections import namedtuple |
---|
26 | n/a | from test.support.script_helper import assert_python_ok |
---|
27 | n/a | from test.support import ( |
---|
28 | n/a | TESTFN, rmtree, |
---|
29 | n/a | reap_children, reap_threads, captured_output, captured_stdout, |
---|
30 | n/a | captured_stderr, unlink, requires_docstrings |
---|
31 | n/a | ) |
---|
32 | n/a | from test import pydoc_mod |
---|
33 | n/a | |
---|
34 | n/a | try: |
---|
35 | n/a | import threading |
---|
36 | n/a | except ImportError: |
---|
37 | n/a | threading = None |
---|
38 | n/a | |
---|
39 | n/a | class nonascii: |
---|
40 | n/a | 'Це не лаÑиниÑÑ' |
---|
41 | n/a | pass |
---|
42 | n/a | |
---|
43 | n/a | if test.support.HAVE_DOCSTRINGS: |
---|
44 | n/a | expected_data_docstrings = ( |
---|
45 | n/a | 'dictionary for instance variables (if defined)', |
---|
46 | n/a | 'list of weak references to the object (if defined)', |
---|
47 | n/a | ) * 2 |
---|
48 | n/a | else: |
---|
49 | n/a | expected_data_docstrings = ('', '', '', '') |
---|
50 | n/a | |
---|
51 | n/a | expected_text_pattern = """ |
---|
52 | n/a | NAME |
---|
53 | n/a | test.pydoc_mod - This is a test module for test_pydoc |
---|
54 | n/a | %s |
---|
55 | n/a | CLASSES |
---|
56 | n/a | builtins.object |
---|
57 | n/a | A |
---|
58 | n/a | B |
---|
59 | n/a | C |
---|
60 | n/a | \x20\x20\x20\x20 |
---|
61 | n/a | class A(builtins.object) |
---|
62 | n/a | | Hello and goodbye |
---|
63 | n/a | |\x20\x20 |
---|
64 | n/a | | Methods defined here: |
---|
65 | n/a | |\x20\x20 |
---|
66 | n/a | | __init__() |
---|
67 | n/a | | Wow, I have no function! |
---|
68 | n/a | |\x20\x20 |
---|
69 | n/a | | ---------------------------------------------------------------------- |
---|
70 | n/a | | Data descriptors defined here: |
---|
71 | n/a | |\x20\x20 |
---|
72 | n/a | | __dict__%s |
---|
73 | n/a | |\x20\x20 |
---|
74 | n/a | | __weakref__%s |
---|
75 | n/a | \x20\x20\x20\x20 |
---|
76 | n/a | class B(builtins.object) |
---|
77 | n/a | | Data descriptors defined here: |
---|
78 | n/a | |\x20\x20 |
---|
79 | n/a | | __dict__%s |
---|
80 | n/a | |\x20\x20 |
---|
81 | n/a | | __weakref__%s |
---|
82 | n/a | |\x20\x20 |
---|
83 | n/a | | ---------------------------------------------------------------------- |
---|
84 | n/a | | Data and other attributes defined here: |
---|
85 | n/a | |\x20\x20 |
---|
86 | n/a | | NO_MEANING = 'eggs' |
---|
87 | n/a | |\x20\x20 |
---|
88 | n/a | | __annotations__ = {'NO_MEANING': <class 'str'>} |
---|
89 | n/a | \x20\x20\x20\x20 |
---|
90 | n/a | class C(builtins.object) |
---|
91 | n/a | | Methods defined here: |
---|
92 | n/a | |\x20\x20 |
---|
93 | n/a | | get_answer(self) |
---|
94 | n/a | | Return say_no() |
---|
95 | n/a | |\x20\x20 |
---|
96 | n/a | | is_it_true(self) |
---|
97 | n/a | | Return self.get_answer() |
---|
98 | n/a | |\x20\x20 |
---|
99 | n/a | | say_no(self) |
---|
100 | n/a | |\x20\x20 |
---|
101 | n/a | | ---------------------------------------------------------------------- |
---|
102 | n/a | | Data descriptors defined here: |
---|
103 | n/a | |\x20\x20 |
---|
104 | n/a | | __dict__ |
---|
105 | n/a | | dictionary for instance variables (if defined) |
---|
106 | n/a | |\x20\x20 |
---|
107 | n/a | | __weakref__ |
---|
108 | n/a | | list of weak references to the object (if defined) |
---|
109 | n/a | |
---|
110 | n/a | FUNCTIONS |
---|
111 | n/a | doc_func() |
---|
112 | n/a | This function solves all of the world's problems: |
---|
113 | n/a | hunger |
---|
114 | n/a | lack of Python |
---|
115 | n/a | war |
---|
116 | n/a | \x20\x20\x20\x20 |
---|
117 | n/a | nodoc_func() |
---|
118 | n/a | |
---|
119 | n/a | DATA |
---|
120 | n/a | __xyz__ = 'X, Y and Z' |
---|
121 | n/a | |
---|
122 | n/a | VERSION |
---|
123 | n/a | 1.2.3.4 |
---|
124 | n/a | |
---|
125 | n/a | AUTHOR |
---|
126 | n/a | Benjamin Peterson |
---|
127 | n/a | |
---|
128 | n/a | CREDITS |
---|
129 | n/a | Nobody |
---|
130 | n/a | |
---|
131 | n/a | FILE |
---|
132 | n/a | %s |
---|
133 | n/a | """.strip() |
---|
134 | n/a | |
---|
135 | n/a | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
---|
136 | n/a | for s in expected_data_docstrings) |
---|
137 | n/a | |
---|
138 | n/a | expected_html_pattern = """ |
---|
139 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
---|
140 | n/a | <tr bgcolor="#7799ee"> |
---|
141 | n/a | <td valign=bottom> <br> |
---|
142 | n/a | <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="test.html"><font color="#ffffff">test</font></a>.pydoc_mod</strong></big></big> (version 1.2.3.4)</font></td |
---|
143 | n/a | ><td align=right valign=bottom |
---|
144 | n/a | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
---|
145 | n/a | <p><tt>This is a test module for test_pydoc</tt></p> |
---|
146 | n/a | <p> |
---|
147 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
148 | n/a | <tr bgcolor="#ee77aa"> |
---|
149 | n/a | <td colspan=3 valign=bottom> <br> |
---|
150 | n/a | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
---|
151 | n/a | \x20\x20\x20\x20 |
---|
152 | n/a | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
---|
153 | n/a | <td width="100%%"><dl> |
---|
154 | n/a | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
---|
155 | n/a | </font></dt><dd> |
---|
156 | n/a | <dl> |
---|
157 | n/a | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
---|
158 | n/a | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
---|
159 | n/a | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#C">C</a> |
---|
160 | n/a | </font></dt></dl> |
---|
161 | n/a | </dd> |
---|
162 | n/a | </dl> |
---|
163 | n/a | <p> |
---|
164 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
165 | n/a | <tr bgcolor="#ffc8d8"> |
---|
166 | n/a | <td colspan=3 valign=bottom> <br> |
---|
167 | n/a | <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
---|
168 | n/a | \x20\x20\x20\x20 |
---|
169 | n/a | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
---|
170 | n/a | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
---|
171 | n/a | <tr><td> </td> |
---|
172 | n/a | <td width="100%%">Methods defined here:<br> |
---|
173 | n/a | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
---|
174 | n/a | |
---|
175 | n/a | <hr> |
---|
176 | n/a | Data descriptors defined here:<br> |
---|
177 | n/a | <dl><dt><strong>__dict__</strong></dt> |
---|
178 | n/a | <dd><tt>%s</tt></dd> |
---|
179 | n/a | </dl> |
---|
180 | n/a | <dl><dt><strong>__weakref__</strong></dt> |
---|
181 | n/a | <dd><tt>%s</tt></dd> |
---|
182 | n/a | </dl> |
---|
183 | n/a | </td></tr></table> <p> |
---|
184 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
185 | n/a | <tr bgcolor="#ffc8d8"> |
---|
186 | n/a | <td colspan=3 valign=bottom> <br> |
---|
187 | n/a | <font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
---|
188 | n/a | \x20\x20\x20\x20 |
---|
189 | n/a | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
---|
190 | n/a | <td width="100%%">Data descriptors defined here:<br> |
---|
191 | n/a | <dl><dt><strong>__dict__</strong></dt> |
---|
192 | n/a | <dd><tt>%s</tt></dd> |
---|
193 | n/a | </dl> |
---|
194 | n/a | <dl><dt><strong>__weakref__</strong></dt> |
---|
195 | n/a | <dd><tt>%s</tt></dd> |
---|
196 | n/a | </dl> |
---|
197 | n/a | <hr> |
---|
198 | n/a | Data and other attributes defined here:<br> |
---|
199 | n/a | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
---|
200 | n/a | |
---|
201 | n/a | <dl><dt><strong>__annotations__</strong> = {'NO_MEANING': <class 'str'>}</dl> |
---|
202 | n/a | |
---|
203 | n/a | </td></tr></table> <p> |
---|
204 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
205 | n/a | <tr bgcolor="#ffc8d8"> |
---|
206 | n/a | <td colspan=3 valign=bottom> <br> |
---|
207 | n/a | <font color="#000000" face="helvetica, arial"><a name="C">class <strong>C</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
---|
208 | n/a | \x20\x20\x20\x20 |
---|
209 | n/a | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
---|
210 | n/a | <td width="100%%">Methods defined here:<br> |
---|
211 | n/a | <dl><dt><a name="C-get_answer"><strong>get_answer</strong></a>(self)</dt><dd><tt>Return <a href="#C-say_no">say_no</a>()</tt></dd></dl> |
---|
212 | n/a | |
---|
213 | n/a | <dl><dt><a name="C-is_it_true"><strong>is_it_true</strong></a>(self)</dt><dd><tt>Return self.<a href="#C-get_answer">get_answer</a>()</tt></dd></dl> |
---|
214 | n/a | |
---|
215 | n/a | <dl><dt><a name="C-say_no"><strong>say_no</strong></a>(self)</dt></dl> |
---|
216 | n/a | |
---|
217 | n/a | <hr> |
---|
218 | n/a | Data descriptors defined here:<br> |
---|
219 | n/a | <dl><dt><strong>__dict__</strong></dt> |
---|
220 | n/a | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
---|
221 | n/a | </dl> |
---|
222 | n/a | <dl><dt><strong>__weakref__</strong></dt> |
---|
223 | n/a | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
---|
224 | n/a | </dl> |
---|
225 | n/a | </td></tr></table></td></tr></table><p> |
---|
226 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
227 | n/a | <tr bgcolor="#eeaa77"> |
---|
228 | n/a | <td colspan=3 valign=bottom> <br> |
---|
229 | n/a | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
---|
230 | n/a | \x20\x20\x20\x20 |
---|
231 | n/a | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
---|
232 | n/a | <td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This function solves all of the world's problems:<br> |
---|
233 | n/a | hunger<br> |
---|
234 | n/a | lack of Python<br> |
---|
235 | n/a | war</tt></dd></dl> |
---|
236 | n/a | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
---|
237 | n/a | </td></tr></table><p> |
---|
238 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
239 | n/a | <tr bgcolor="#55aa55"> |
---|
240 | n/a | <td colspan=3 valign=bottom> <br> |
---|
241 | n/a | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
---|
242 | n/a | \x20\x20\x20\x20 |
---|
243 | n/a | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
---|
244 | n/a | <td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p> |
---|
245 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
246 | n/a | <tr bgcolor="#7799ee"> |
---|
247 | n/a | <td colspan=3 valign=bottom> <br> |
---|
248 | n/a | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
---|
249 | n/a | \x20\x20\x20\x20 |
---|
250 | n/a | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
---|
251 | n/a | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
---|
252 | n/a | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
---|
253 | n/a | <tr bgcolor="#7799ee"> |
---|
254 | n/a | <td colspan=3 valign=bottom> <br> |
---|
255 | n/a | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
---|
256 | n/a | \x20\x20\x20\x20 |
---|
257 | n/a | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
---|
258 | n/a | <td width="100%%">Nobody</td></tr></table> |
---|
259 | n/a | """.strip() # ' <- emacs turd |
---|
260 | n/a | |
---|
261 | n/a | expected_html_data_docstrings = tuple(s.replace(' ', ' ') |
---|
262 | n/a | for s in expected_data_docstrings) |
---|
263 | n/a | |
---|
264 | n/a | # output pattern for missing module |
---|
265 | n/a | missing_pattern = '''\ |
---|
266 | n/a | No Python documentation found for %r. |
---|
267 | n/a | Use help() to get the interactive help utility. |
---|
268 | n/a | Use help(str) for help on the str class.'''.replace('\n', os.linesep) |
---|
269 | n/a | |
---|
270 | n/a | # output pattern for module with bad imports |
---|
271 | n/a | badimport_pattern = "problem in %s - ModuleNotFoundError: No module named %r" |
---|
272 | n/a | |
---|
273 | n/a | expected_dynamicattribute_pattern = """ |
---|
274 | n/a | Help on class DA in module %s: |
---|
275 | n/a | |
---|
276 | n/a | class DA(builtins.object) |
---|
277 | n/a | | Data descriptors defined here: |
---|
278 | n/a | |\x20\x20 |
---|
279 | n/a | | __dict__%s |
---|
280 | n/a | |\x20\x20 |
---|
281 | n/a | | __weakref__%s |
---|
282 | n/a | |\x20\x20 |
---|
283 | n/a | | ham |
---|
284 | n/a | |\x20\x20 |
---|
285 | n/a | | ---------------------------------------------------------------------- |
---|
286 | n/a | | Data and other attributes inherited from Meta: |
---|
287 | n/a | |\x20\x20 |
---|
288 | n/a | | ham = 'spam' |
---|
289 | n/a | """.strip() |
---|
290 | n/a | |
---|
291 | n/a | expected_virtualattribute_pattern1 = """ |
---|
292 | n/a | Help on class Class in module %s: |
---|
293 | n/a | |
---|
294 | n/a | class Class(builtins.object) |
---|
295 | n/a | | Data and other attributes inherited from Meta: |
---|
296 | n/a | |\x20\x20 |
---|
297 | n/a | | LIFE = 42 |
---|
298 | n/a | """.strip() |
---|
299 | n/a | |
---|
300 | n/a | expected_virtualattribute_pattern2 = """ |
---|
301 | n/a | Help on class Class1 in module %s: |
---|
302 | n/a | |
---|
303 | n/a | class Class1(builtins.object) |
---|
304 | n/a | | Data and other attributes inherited from Meta1: |
---|
305 | n/a | |\x20\x20 |
---|
306 | n/a | | one = 1 |
---|
307 | n/a | """.strip() |
---|
308 | n/a | |
---|
309 | n/a | expected_virtualattribute_pattern3 = """ |
---|
310 | n/a | Help on class Class2 in module %s: |
---|
311 | n/a | |
---|
312 | n/a | class Class2(Class1) |
---|
313 | n/a | | Method resolution order: |
---|
314 | n/a | | Class2 |
---|
315 | n/a | | Class1 |
---|
316 | n/a | | builtins.object |
---|
317 | n/a | |\x20\x20 |
---|
318 | n/a | | Data and other attributes inherited from Meta1: |
---|
319 | n/a | |\x20\x20 |
---|
320 | n/a | | one = 1 |
---|
321 | n/a | |\x20\x20 |
---|
322 | n/a | | ---------------------------------------------------------------------- |
---|
323 | n/a | | Data and other attributes inherited from Meta3: |
---|
324 | n/a | |\x20\x20 |
---|
325 | n/a | | three = 3 |
---|
326 | n/a | |\x20\x20 |
---|
327 | n/a | | ---------------------------------------------------------------------- |
---|
328 | n/a | | Data and other attributes inherited from Meta2: |
---|
329 | n/a | |\x20\x20 |
---|
330 | n/a | | two = 2 |
---|
331 | n/a | """.strip() |
---|
332 | n/a | |
---|
333 | n/a | expected_missingattribute_pattern = """ |
---|
334 | n/a | Help on class C in module %s: |
---|
335 | n/a | |
---|
336 | n/a | class C(builtins.object) |
---|
337 | n/a | | Data and other attributes defined here: |
---|
338 | n/a | |\x20\x20 |
---|
339 | n/a | | here = 'present!' |
---|
340 | n/a | """.strip() |
---|
341 | n/a | |
---|
342 | n/a | def run_pydoc(module_name, *args, **env): |
---|
343 | n/a | """ |
---|
344 | n/a | Runs pydoc on the specified module. Returns the stripped |
---|
345 | n/a | output of pydoc. |
---|
346 | n/a | """ |
---|
347 | n/a | args = args + (module_name,) |
---|
348 | n/a | # do not write bytecode files to avoid caching errors |
---|
349 | n/a | rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env) |
---|
350 | n/a | return out.strip() |
---|
351 | n/a | |
---|
352 | n/a | def get_pydoc_html(module): |
---|
353 | n/a | "Returns pydoc generated output as html" |
---|
354 | n/a | doc = pydoc.HTMLDoc() |
---|
355 | n/a | output = doc.docmodule(module) |
---|
356 | n/a | loc = doc.getdocloc(pydoc_mod) or "" |
---|
357 | n/a | if loc: |
---|
358 | n/a | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
---|
359 | n/a | return output.strip(), loc |
---|
360 | n/a | |
---|
361 | n/a | def get_pydoc_link(module): |
---|
362 | n/a | "Returns a documentation web link of a module" |
---|
363 | n/a | dirname = os.path.dirname |
---|
364 | n/a | basedir = dirname(dirname(__file__)) |
---|
365 | n/a | doc = pydoc.TextDoc() |
---|
366 | n/a | loc = doc.getdocloc(module, basedir=basedir) |
---|
367 | n/a | return loc |
---|
368 | n/a | |
---|
369 | n/a | def get_pydoc_text(module): |
---|
370 | n/a | "Returns pydoc generated output as text" |
---|
371 | n/a | doc = pydoc.TextDoc() |
---|
372 | n/a | loc = doc.getdocloc(pydoc_mod) or "" |
---|
373 | n/a | if loc: |
---|
374 | n/a | loc = "\nMODULE DOCS\n " + loc + "\n" |
---|
375 | n/a | |
---|
376 | n/a | output = doc.docmodule(module) |
---|
377 | n/a | |
---|
378 | n/a | # clean up the extra text formatting that pydoc performs |
---|
379 | n/a | patt = re.compile('\b.') |
---|
380 | n/a | output = patt.sub('', output) |
---|
381 | n/a | return output.strip(), loc |
---|
382 | n/a | |
---|
383 | n/a | def get_html_title(text): |
---|
384 | n/a | # Bit of hack, but good enough for test purposes |
---|
385 | n/a | header, _, _ = text.partition("</head>") |
---|
386 | n/a | _, _, title = header.partition("<title>") |
---|
387 | n/a | title, _, _ = title.partition("</title>") |
---|
388 | n/a | return title |
---|
389 | n/a | |
---|
390 | n/a | |
---|
391 | n/a | class PydocBaseTest(unittest.TestCase): |
---|
392 | n/a | |
---|
393 | n/a | def _restricted_walk_packages(self, walk_packages, path=None): |
---|
394 | n/a | """ |
---|
395 | n/a | A version of pkgutil.walk_packages() that will restrict itself to |
---|
396 | n/a | a given path. |
---|
397 | n/a | """ |
---|
398 | n/a | default_path = path or [os.path.dirname(__file__)] |
---|
399 | n/a | def wrapper(path=None, prefix='', onerror=None): |
---|
400 | n/a | return walk_packages(path or default_path, prefix, onerror) |
---|
401 | n/a | return wrapper |
---|
402 | n/a | |
---|
403 | n/a | @contextlib.contextmanager |
---|
404 | n/a | def restrict_walk_packages(self, path=None): |
---|
405 | n/a | walk_packages = pkgutil.walk_packages |
---|
406 | n/a | pkgutil.walk_packages = self._restricted_walk_packages(walk_packages, |
---|
407 | n/a | path) |
---|
408 | n/a | try: |
---|
409 | n/a | yield |
---|
410 | n/a | finally: |
---|
411 | n/a | pkgutil.walk_packages = walk_packages |
---|
412 | n/a | |
---|
413 | n/a | def call_url_handler(self, url, expected_title): |
---|
414 | n/a | text = pydoc._url_handler(url, "text/html") |
---|
415 | n/a | result = get_html_title(text) |
---|
416 | n/a | # Check the title to ensure an unexpected error page was not returned |
---|
417 | n/a | self.assertEqual(result, expected_title, text) |
---|
418 | n/a | return text |
---|
419 | n/a | |
---|
420 | n/a | |
---|
421 | n/a | class PydocDocTest(unittest.TestCase): |
---|
422 | n/a | |
---|
423 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
424 | n/a | "Docstrings are omitted with -O2 and above") |
---|
425 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
426 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
427 | n/a | @requires_docstrings |
---|
428 | n/a | def test_html_doc(self): |
---|
429 | n/a | result, doc_loc = get_pydoc_html(pydoc_mod) |
---|
430 | n/a | mod_file = inspect.getabsfile(pydoc_mod) |
---|
431 | n/a | mod_url = urllib.parse.quote(mod_file) |
---|
432 | n/a | expected_html = expected_html_pattern % ( |
---|
433 | n/a | (mod_url, mod_file, doc_loc) + |
---|
434 | n/a | expected_html_data_docstrings) |
---|
435 | n/a | self.assertEqual(result, expected_html) |
---|
436 | n/a | |
---|
437 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
438 | n/a | "Docstrings are omitted with -O2 and above") |
---|
439 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
440 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
441 | n/a | @requires_docstrings |
---|
442 | n/a | def test_text_doc(self): |
---|
443 | n/a | result, doc_loc = get_pydoc_text(pydoc_mod) |
---|
444 | n/a | expected_text = expected_text_pattern % ( |
---|
445 | n/a | (doc_loc,) + |
---|
446 | n/a | expected_text_data_docstrings + |
---|
447 | n/a | (inspect.getabsfile(pydoc_mod),)) |
---|
448 | n/a | self.assertEqual(expected_text, result) |
---|
449 | n/a | |
---|
450 | n/a | def test_text_enum_member_with_value_zero(self): |
---|
451 | n/a | # Test issue #20654 to ensure enum member with value 0 can be |
---|
452 | n/a | # displayed. It used to throw KeyError: 'zero'. |
---|
453 | n/a | import enum |
---|
454 | n/a | class BinaryInteger(enum.IntEnum): |
---|
455 | n/a | zero = 0 |
---|
456 | n/a | one = 1 |
---|
457 | n/a | doc = pydoc.render_doc(BinaryInteger) |
---|
458 | n/a | self.assertIn('<BinaryInteger.zero: 0>', doc) |
---|
459 | n/a | |
---|
460 | n/a | def test_mixed_case_module_names_are_lower_cased(self): |
---|
461 | n/a | # issue16484 |
---|
462 | n/a | doc_link = get_pydoc_link(xml.etree.ElementTree) |
---|
463 | n/a | self.assertIn('xml.etree.elementtree', doc_link) |
---|
464 | n/a | |
---|
465 | n/a | def test_issue8225(self): |
---|
466 | n/a | # Test issue8225 to ensure no doc link appears for xml.etree |
---|
467 | n/a | result, doc_loc = get_pydoc_text(xml.etree) |
---|
468 | n/a | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
---|
469 | n/a | |
---|
470 | n/a | def test_getpager_with_stdin_none(self): |
---|
471 | n/a | previous_stdin = sys.stdin |
---|
472 | n/a | try: |
---|
473 | n/a | sys.stdin = None |
---|
474 | n/a | pydoc.getpager() # Shouldn't fail. |
---|
475 | n/a | finally: |
---|
476 | n/a | sys.stdin = previous_stdin |
---|
477 | n/a | |
---|
478 | n/a | def test_non_str_name(self): |
---|
479 | n/a | # issue14638 |
---|
480 | n/a | # Treat illegal (non-str) name like no name |
---|
481 | n/a | class A: |
---|
482 | n/a | __name__ = 42 |
---|
483 | n/a | class B: |
---|
484 | n/a | pass |
---|
485 | n/a | adoc = pydoc.render_doc(A()) |
---|
486 | n/a | bdoc = pydoc.render_doc(B()) |
---|
487 | n/a | self.assertEqual(adoc.replace("A", "B"), bdoc) |
---|
488 | n/a | |
---|
489 | n/a | def test_not_here(self): |
---|
490 | n/a | missing_module = "test.i_am_not_here" |
---|
491 | n/a | result = str(run_pydoc(missing_module), 'ascii') |
---|
492 | n/a | expected = missing_pattern % missing_module |
---|
493 | n/a | self.assertEqual(expected, result, |
---|
494 | n/a | "documentation for missing module found") |
---|
495 | n/a | |
---|
496 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
497 | n/a | 'Docstrings are omitted with -OO and above') |
---|
498 | n/a | def test_not_ascii(self): |
---|
499 | n/a | result = run_pydoc('test.test_pydoc.nonascii', PYTHONIOENCODING='ascii') |
---|
500 | n/a | encoded = nonascii.__doc__.encode('ascii', 'backslashreplace') |
---|
501 | n/a | self.assertIn(encoded, result) |
---|
502 | n/a | |
---|
503 | n/a | def test_input_strip(self): |
---|
504 | n/a | missing_module = " test.i_am_not_here " |
---|
505 | n/a | result = str(run_pydoc(missing_module), 'ascii') |
---|
506 | n/a | expected = missing_pattern % missing_module.strip() |
---|
507 | n/a | self.assertEqual(expected, result) |
---|
508 | n/a | |
---|
509 | n/a | def test_stripid(self): |
---|
510 | n/a | # test with strings, other implementations might have different repr() |
---|
511 | n/a | stripid = pydoc.stripid |
---|
512 | n/a | # strip the id |
---|
513 | n/a | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
---|
514 | n/a | '<function stripid>') |
---|
515 | n/a | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
---|
516 | n/a | '<function stripid>') |
---|
517 | n/a | # nothing to strip, return the same text |
---|
518 | n/a | self.assertEqual(stripid('42'), '42') |
---|
519 | n/a | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
---|
520 | n/a | "<type 'exceptions.Exception'>") |
---|
521 | n/a | |
---|
522 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
523 | n/a | 'Docstrings are omitted with -O2 and above') |
---|
524 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
525 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
526 | n/a | @requires_docstrings |
---|
527 | n/a | def test_help_output_redirect(self): |
---|
528 | n/a | # issue 940286, if output is set in Helper, then all output from |
---|
529 | n/a | # Helper.help should be redirected |
---|
530 | n/a | old_pattern = expected_text_pattern |
---|
531 | n/a | getpager_old = pydoc.getpager |
---|
532 | n/a | getpager_new = lambda: (lambda x: x) |
---|
533 | n/a | self.maxDiff = None |
---|
534 | n/a | |
---|
535 | n/a | buf = StringIO() |
---|
536 | n/a | helper = pydoc.Helper(output=buf) |
---|
537 | n/a | unused, doc_loc = get_pydoc_text(pydoc_mod) |
---|
538 | n/a | module = "test.pydoc_mod" |
---|
539 | n/a | help_header = """ |
---|
540 | n/a | Help on module test.pydoc_mod in test: |
---|
541 | n/a | |
---|
542 | n/a | """.lstrip() |
---|
543 | n/a | help_header = textwrap.dedent(help_header) |
---|
544 | n/a | expected_help_pattern = help_header + expected_text_pattern |
---|
545 | n/a | |
---|
546 | n/a | pydoc.getpager = getpager_new |
---|
547 | n/a | try: |
---|
548 | n/a | with captured_output('stdout') as output, \ |
---|
549 | n/a | captured_output('stderr') as err: |
---|
550 | n/a | helper.help(module) |
---|
551 | n/a | result = buf.getvalue().strip() |
---|
552 | n/a | expected_text = expected_help_pattern % ( |
---|
553 | n/a | (doc_loc,) + |
---|
554 | n/a | expected_text_data_docstrings + |
---|
555 | n/a | (inspect.getabsfile(pydoc_mod),)) |
---|
556 | n/a | self.assertEqual('', output.getvalue()) |
---|
557 | n/a | self.assertEqual('', err.getvalue()) |
---|
558 | n/a | self.assertEqual(expected_text, result) |
---|
559 | n/a | finally: |
---|
560 | n/a | pydoc.getpager = getpager_old |
---|
561 | n/a | |
---|
562 | n/a | def test_namedtuple_public_underscore(self): |
---|
563 | n/a | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
---|
564 | n/a | with captured_stdout() as help_io: |
---|
565 | n/a | pydoc.help(NT) |
---|
566 | n/a | helptext = help_io.getvalue() |
---|
567 | n/a | self.assertIn('_1', helptext) |
---|
568 | n/a | self.assertIn('_replace', helptext) |
---|
569 | n/a | self.assertIn('_asdict', helptext) |
---|
570 | n/a | |
---|
571 | n/a | def test_synopsis(self): |
---|
572 | n/a | self.addCleanup(unlink, TESTFN) |
---|
573 | n/a | for encoding in ('ISO-8859-1', 'UTF-8'): |
---|
574 | n/a | with open(TESTFN, 'w', encoding=encoding) as script: |
---|
575 | n/a | if encoding != 'UTF-8': |
---|
576 | n/a | print('#coding: {}'.format(encoding), file=script) |
---|
577 | n/a | print('"""line 1: h\xe9', file=script) |
---|
578 | n/a | print('line 2: hi"""', file=script) |
---|
579 | n/a | synopsis = pydoc.synopsis(TESTFN, {}) |
---|
580 | n/a | self.assertEqual(synopsis, 'line 1: h\xe9') |
---|
581 | n/a | |
---|
582 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
583 | n/a | 'Docstrings are omitted with -OO and above') |
---|
584 | n/a | def test_synopsis_sourceless(self): |
---|
585 | n/a | expected = os.__doc__.splitlines()[0] |
---|
586 | n/a | filename = os.__cached__ |
---|
587 | n/a | synopsis = pydoc.synopsis(filename) |
---|
588 | n/a | |
---|
589 | n/a | self.assertEqual(synopsis, expected) |
---|
590 | n/a | |
---|
591 | n/a | def test_synopsis_sourceless_empty_doc(self): |
---|
592 | n/a | with test.support.temp_cwd() as test_dir: |
---|
593 | n/a | init_path = os.path.join(test_dir, 'foomod42.py') |
---|
594 | n/a | cached_path = importlib.util.cache_from_source(init_path) |
---|
595 | n/a | with open(init_path, 'w') as fobj: |
---|
596 | n/a | fobj.write("foo = 1") |
---|
597 | n/a | py_compile.compile(init_path) |
---|
598 | n/a | synopsis = pydoc.synopsis(init_path, {}) |
---|
599 | n/a | self.assertIsNone(synopsis) |
---|
600 | n/a | synopsis_cached = pydoc.synopsis(cached_path, {}) |
---|
601 | n/a | self.assertIsNone(synopsis_cached) |
---|
602 | n/a | |
---|
603 | n/a | def test_splitdoc_with_description(self): |
---|
604 | n/a | example_string = "I Am A Doc\n\n\nHere is my description" |
---|
605 | n/a | self.assertEqual(pydoc.splitdoc(example_string), |
---|
606 | n/a | ('I Am A Doc', '\nHere is my description')) |
---|
607 | n/a | |
---|
608 | n/a | def test_is_object_or_method(self): |
---|
609 | n/a | doc = pydoc.Doc() |
---|
610 | n/a | # Bound Method |
---|
611 | n/a | self.assertTrue(pydoc._is_some_method(doc.fail)) |
---|
612 | n/a | # Method Descriptor |
---|
613 | n/a | self.assertTrue(pydoc._is_some_method(int.__add__)) |
---|
614 | n/a | # String |
---|
615 | n/a | self.assertFalse(pydoc._is_some_method("I am not a method")) |
---|
616 | n/a | |
---|
617 | n/a | def test_is_package_when_not_package(self): |
---|
618 | n/a | with test.support.temp_cwd() as test_dir: |
---|
619 | n/a | self.assertFalse(pydoc.ispackage(test_dir)) |
---|
620 | n/a | |
---|
621 | n/a | def test_is_package_when_is_package(self): |
---|
622 | n/a | with test.support.temp_cwd() as test_dir: |
---|
623 | n/a | init_path = os.path.join(test_dir, '__init__.py') |
---|
624 | n/a | open(init_path, 'w').close() |
---|
625 | n/a | self.assertTrue(pydoc.ispackage(test_dir)) |
---|
626 | n/a | os.remove(init_path) |
---|
627 | n/a | |
---|
628 | n/a | def test_allmethods(self): |
---|
629 | n/a | # issue 17476: allmethods was no longer returning unbound methods. |
---|
630 | n/a | # This test is a bit fragile in the face of changes to object and type, |
---|
631 | n/a | # but I can't think of a better way to do it without duplicating the |
---|
632 | n/a | # logic of the function under test. |
---|
633 | n/a | |
---|
634 | n/a | class TestClass(object): |
---|
635 | n/a | def method_returning_true(self): |
---|
636 | n/a | return True |
---|
637 | n/a | |
---|
638 | n/a | # What we expect to get back: everything on object... |
---|
639 | n/a | expected = dict(vars(object)) |
---|
640 | n/a | # ...plus our unbound method... |
---|
641 | n/a | expected['method_returning_true'] = TestClass.method_returning_true |
---|
642 | n/a | # ...but not the non-methods on object. |
---|
643 | n/a | del expected['__doc__'] |
---|
644 | n/a | del expected['__class__'] |
---|
645 | n/a | # inspect resolves descriptors on type into methods, but vars doesn't, |
---|
646 | n/a | # so we need to update __subclasshook__ and __init_subclass__. |
---|
647 | n/a | expected['__subclasshook__'] = TestClass.__subclasshook__ |
---|
648 | n/a | expected['__init_subclass__'] = TestClass.__init_subclass__ |
---|
649 | n/a | |
---|
650 | n/a | methods = pydoc.allmethods(TestClass) |
---|
651 | n/a | self.assertDictEqual(methods, expected) |
---|
652 | n/a | |
---|
653 | n/a | |
---|
654 | n/a | class PydocImportTest(PydocBaseTest): |
---|
655 | n/a | |
---|
656 | n/a | def setUp(self): |
---|
657 | n/a | self.test_dir = os.mkdir(TESTFN) |
---|
658 | n/a | self.addCleanup(rmtree, TESTFN) |
---|
659 | n/a | importlib.invalidate_caches() |
---|
660 | n/a | |
---|
661 | n/a | def test_badimport(self): |
---|
662 | n/a | # This tests the fix for issue 5230, where if pydoc found the module |
---|
663 | n/a | # but the module had an internal import error pydoc would report no doc |
---|
664 | n/a | # found. |
---|
665 | n/a | modname = 'testmod_xyzzy' |
---|
666 | n/a | testpairs = ( |
---|
667 | n/a | ('i_am_not_here', 'i_am_not_here'), |
---|
668 | n/a | ('test.i_am_not_here_either', 'test.i_am_not_here_either'), |
---|
669 | n/a | ('test.i_am_not_here.neither_am_i', 'test.i_am_not_here'), |
---|
670 | n/a | ('i_am_not_here.{}'.format(modname), 'i_am_not_here'), |
---|
671 | n/a | ('test.{}'.format(modname), 'test.{}'.format(modname)), |
---|
672 | n/a | ) |
---|
673 | n/a | |
---|
674 | n/a | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
---|
675 | n/a | for importstring, expectedinmsg in testpairs: |
---|
676 | n/a | with open(sourcefn, 'w') as f: |
---|
677 | n/a | f.write("import {}\n".format(importstring)) |
---|
678 | n/a | result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
---|
679 | n/a | expected = badimport_pattern % (modname, expectedinmsg) |
---|
680 | n/a | self.assertEqual(expected, result) |
---|
681 | n/a | |
---|
682 | n/a | def test_apropos_with_bad_package(self): |
---|
683 | n/a | # Issue 7425 - pydoc -k failed when bad package on path |
---|
684 | n/a | pkgdir = os.path.join(TESTFN, "syntaxerr") |
---|
685 | n/a | os.mkdir(pkgdir) |
---|
686 | n/a | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
---|
687 | n/a | with open(badsyntax, 'w') as f: |
---|
688 | n/a | f.write("invalid python syntax = $1\n") |
---|
689 | n/a | with self.restrict_walk_packages(path=[TESTFN]): |
---|
690 | n/a | with captured_stdout() as out: |
---|
691 | n/a | with captured_stderr() as err: |
---|
692 | n/a | pydoc.apropos('xyzzy') |
---|
693 | n/a | # No result, no error |
---|
694 | n/a | self.assertEqual(out.getvalue(), '') |
---|
695 | n/a | self.assertEqual(err.getvalue(), '') |
---|
696 | n/a | # The package name is still matched |
---|
697 | n/a | with captured_stdout() as out: |
---|
698 | n/a | with captured_stderr() as err: |
---|
699 | n/a | pydoc.apropos('syntaxerr') |
---|
700 | n/a | self.assertEqual(out.getvalue().strip(), 'syntaxerr') |
---|
701 | n/a | self.assertEqual(err.getvalue(), '') |
---|
702 | n/a | |
---|
703 | n/a | def test_apropos_with_unreadable_dir(self): |
---|
704 | n/a | # Issue 7367 - pydoc -k failed when unreadable dir on path |
---|
705 | n/a | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
---|
706 | n/a | os.mkdir(self.unreadable_dir, 0) |
---|
707 | n/a | self.addCleanup(os.rmdir, self.unreadable_dir) |
---|
708 | n/a | # Note, on Windows the directory appears to be still |
---|
709 | n/a | # readable so this is not really testing the issue there |
---|
710 | n/a | with self.restrict_walk_packages(path=[TESTFN]): |
---|
711 | n/a | with captured_stdout() as out: |
---|
712 | n/a | with captured_stderr() as err: |
---|
713 | n/a | pydoc.apropos('SOMEKEY') |
---|
714 | n/a | # No result, no error |
---|
715 | n/a | self.assertEqual(out.getvalue(), '') |
---|
716 | n/a | self.assertEqual(err.getvalue(), '') |
---|
717 | n/a | |
---|
718 | n/a | def test_apropos_empty_doc(self): |
---|
719 | n/a | pkgdir = os.path.join(TESTFN, 'walkpkg') |
---|
720 | n/a | os.mkdir(pkgdir) |
---|
721 | n/a | self.addCleanup(rmtree, pkgdir) |
---|
722 | n/a | init_path = os.path.join(pkgdir, '__init__.py') |
---|
723 | n/a | with open(init_path, 'w') as fobj: |
---|
724 | n/a | fobj.write("foo = 1") |
---|
725 | n/a | current_mode = stat.S_IMODE(os.stat(pkgdir).st_mode) |
---|
726 | n/a | try: |
---|
727 | n/a | os.chmod(pkgdir, current_mode & ~stat.S_IEXEC) |
---|
728 | n/a | with self.restrict_walk_packages(path=[TESTFN]), captured_stdout() as stdout: |
---|
729 | n/a | pydoc.apropos('') |
---|
730 | n/a | self.assertIn('walkpkg', stdout.getvalue()) |
---|
731 | n/a | finally: |
---|
732 | n/a | os.chmod(pkgdir, current_mode) |
---|
733 | n/a | |
---|
734 | n/a | def test_url_search_package_error(self): |
---|
735 | n/a | # URL handler search should cope with packages that raise exceptions |
---|
736 | n/a | pkgdir = os.path.join(TESTFN, "test_error_package") |
---|
737 | n/a | os.mkdir(pkgdir) |
---|
738 | n/a | init = os.path.join(pkgdir, "__init__.py") |
---|
739 | n/a | with open(init, "wt", encoding="ascii") as f: |
---|
740 | n/a | f.write("""raise ValueError("ouch")\n""") |
---|
741 | n/a | with self.restrict_walk_packages(path=[TESTFN]): |
---|
742 | n/a | # Package has to be importable for the error to have any effect |
---|
743 | n/a | saved_paths = tuple(sys.path) |
---|
744 | n/a | sys.path.insert(0, TESTFN) |
---|
745 | n/a | try: |
---|
746 | n/a | with self.assertRaisesRegex(ValueError, "ouch"): |
---|
747 | n/a | import test_error_package # Sanity check |
---|
748 | n/a | |
---|
749 | n/a | text = self.call_url_handler("search?key=test_error_package", |
---|
750 | n/a | "Pydoc: Search Results") |
---|
751 | n/a | found = ('<a href="test_error_package.html">' |
---|
752 | n/a | 'test_error_package</a>') |
---|
753 | n/a | self.assertIn(found, text) |
---|
754 | n/a | finally: |
---|
755 | n/a | sys.path[:] = saved_paths |
---|
756 | n/a | |
---|
757 | n/a | @unittest.skip('causes undesirable side-effects (#20128)') |
---|
758 | n/a | def test_modules(self): |
---|
759 | n/a | # See Helper.listmodules(). |
---|
760 | n/a | num_header_lines = 2 |
---|
761 | n/a | num_module_lines_min = 5 # Playing it safe. |
---|
762 | n/a | num_footer_lines = 3 |
---|
763 | n/a | expected = num_header_lines + num_module_lines_min + num_footer_lines |
---|
764 | n/a | |
---|
765 | n/a | output = StringIO() |
---|
766 | n/a | helper = pydoc.Helper(output=output) |
---|
767 | n/a | helper('modules') |
---|
768 | n/a | result = output.getvalue().strip() |
---|
769 | n/a | num_lines = len(result.splitlines()) |
---|
770 | n/a | |
---|
771 | n/a | self.assertGreaterEqual(num_lines, expected) |
---|
772 | n/a | |
---|
773 | n/a | @unittest.skip('causes undesirable side-effects (#20128)') |
---|
774 | n/a | def test_modules_search(self): |
---|
775 | n/a | # See Helper.listmodules(). |
---|
776 | n/a | expected = 'pydoc - ' |
---|
777 | n/a | |
---|
778 | n/a | output = StringIO() |
---|
779 | n/a | helper = pydoc.Helper(output=output) |
---|
780 | n/a | with captured_stdout() as help_io: |
---|
781 | n/a | helper('modules pydoc') |
---|
782 | n/a | result = help_io.getvalue() |
---|
783 | n/a | |
---|
784 | n/a | self.assertIn(expected, result) |
---|
785 | n/a | |
---|
786 | n/a | @unittest.skip('some buildbots are not cooperating (#20128)') |
---|
787 | n/a | def test_modules_search_builtin(self): |
---|
788 | n/a | expected = 'gc - ' |
---|
789 | n/a | |
---|
790 | n/a | output = StringIO() |
---|
791 | n/a | helper = pydoc.Helper(output=output) |
---|
792 | n/a | with captured_stdout() as help_io: |
---|
793 | n/a | helper('modules garbage') |
---|
794 | n/a | result = help_io.getvalue() |
---|
795 | n/a | |
---|
796 | n/a | self.assertTrue(result.startswith(expected)) |
---|
797 | n/a | |
---|
798 | n/a | def test_importfile(self): |
---|
799 | n/a | loaded_pydoc = pydoc.importfile(pydoc.__file__) |
---|
800 | n/a | |
---|
801 | n/a | self.assertIsNot(loaded_pydoc, pydoc) |
---|
802 | n/a | self.assertEqual(loaded_pydoc.__name__, 'pydoc') |
---|
803 | n/a | self.assertEqual(loaded_pydoc.__file__, pydoc.__file__) |
---|
804 | n/a | self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__) |
---|
805 | n/a | |
---|
806 | n/a | |
---|
807 | n/a | class TestDescriptions(unittest.TestCase): |
---|
808 | n/a | |
---|
809 | n/a | def test_module(self): |
---|
810 | n/a | # Check that pydocfodder module can be described |
---|
811 | n/a | from test import pydocfodder |
---|
812 | n/a | doc = pydoc.render_doc(pydocfodder) |
---|
813 | n/a | self.assertIn("pydocfodder", doc) |
---|
814 | n/a | |
---|
815 | n/a | def test_class(self): |
---|
816 | n/a | class C: "New-style class" |
---|
817 | n/a | c = C() |
---|
818 | n/a | |
---|
819 | n/a | self.assertEqual(pydoc.describe(C), 'class C') |
---|
820 | n/a | self.assertEqual(pydoc.describe(c), 'C') |
---|
821 | n/a | expected = 'C in module %s object' % __name__ |
---|
822 | n/a | self.assertIn(expected, pydoc.render_doc(c)) |
---|
823 | n/a | |
---|
824 | n/a | def test_typing_pydoc(self): |
---|
825 | n/a | def foo(data: typing.List[typing.Any], |
---|
826 | n/a | x: int) -> typing.Iterator[typing.Tuple[int, typing.Any]]: |
---|
827 | n/a | ... |
---|
828 | n/a | T = typing.TypeVar('T') |
---|
829 | n/a | class C(typing.Generic[T], typing.Mapping[int, str]): ... |
---|
830 | n/a | self.assertEqual(pydoc.render_doc(foo).splitlines()[-1], |
---|
831 | n/a | 'f\x08fo\x08oo\x08o(data:List[Any], x:int)' |
---|
832 | n/a | ' -> Iterator[Tuple[int, Any]]') |
---|
833 | n/a | self.assertEqual(pydoc.render_doc(C).splitlines()[2], |
---|
834 | n/a | 'class C\x08C(typing.Mapping)') |
---|
835 | n/a | |
---|
836 | n/a | def test_builtin(self): |
---|
837 | n/a | for name in ('str', 'str.translate', 'builtins.str', |
---|
838 | n/a | 'builtins.str.translate'): |
---|
839 | n/a | # test low-level function |
---|
840 | n/a | self.assertIsNotNone(pydoc.locate(name)) |
---|
841 | n/a | # test high-level function |
---|
842 | n/a | try: |
---|
843 | n/a | pydoc.render_doc(name) |
---|
844 | n/a | except ImportError: |
---|
845 | n/a | self.fail('finding the doc of {!r} failed'.format(name)) |
---|
846 | n/a | |
---|
847 | n/a | for name in ('notbuiltins', 'strrr', 'strr.translate', |
---|
848 | n/a | 'str.trrrranslate', 'builtins.strrr', |
---|
849 | n/a | 'builtins.str.trrranslate'): |
---|
850 | n/a | self.assertIsNone(pydoc.locate(name)) |
---|
851 | n/a | self.assertRaises(ImportError, pydoc.render_doc, name) |
---|
852 | n/a | |
---|
853 | n/a | @staticmethod |
---|
854 | n/a | def _get_summary_line(o): |
---|
855 | n/a | text = pydoc.plain(pydoc.render_doc(o)) |
---|
856 | n/a | lines = text.split('\n') |
---|
857 | n/a | assert len(lines) >= 2 |
---|
858 | n/a | return lines[2] |
---|
859 | n/a | |
---|
860 | n/a | # these should include "self" |
---|
861 | n/a | def test_unbound_python_method(self): |
---|
862 | n/a | self.assertEqual(self._get_summary_line(textwrap.TextWrapper.wrap), |
---|
863 | n/a | "wrap(self, text)") |
---|
864 | n/a | |
---|
865 | n/a | @requires_docstrings |
---|
866 | n/a | def test_unbound_builtin_method(self): |
---|
867 | n/a | self.assertEqual(self._get_summary_line(_pickle.Pickler.dump), |
---|
868 | n/a | "dump(self, obj, /)") |
---|
869 | n/a | |
---|
870 | n/a | # these no longer include "self" |
---|
871 | n/a | def test_bound_python_method(self): |
---|
872 | n/a | t = textwrap.TextWrapper() |
---|
873 | n/a | self.assertEqual(self._get_summary_line(t.wrap), |
---|
874 | n/a | "wrap(text) method of textwrap.TextWrapper instance") |
---|
875 | n/a | |
---|
876 | n/a | def test_field_order_for_named_tuples(self): |
---|
877 | n/a | Person = namedtuple('Person', ['nickname', 'firstname', 'agegroup']) |
---|
878 | n/a | s = pydoc.render_doc(Person) |
---|
879 | n/a | self.assertLess(s.index('nickname'), s.index('firstname')) |
---|
880 | n/a | self.assertLess(s.index('firstname'), s.index('agegroup')) |
---|
881 | n/a | |
---|
882 | n/a | class NonIterableFields: |
---|
883 | n/a | _fields = None |
---|
884 | n/a | |
---|
885 | n/a | class NonHashableFields: |
---|
886 | n/a | _fields = [[]] |
---|
887 | n/a | |
---|
888 | n/a | # Make sure these doesn't fail |
---|
889 | n/a | pydoc.render_doc(NonIterableFields) |
---|
890 | n/a | pydoc.render_doc(NonHashableFields) |
---|
891 | n/a | |
---|
892 | n/a | @requires_docstrings |
---|
893 | n/a | def test_bound_builtin_method(self): |
---|
894 | n/a | s = StringIO() |
---|
895 | n/a | p = _pickle.Pickler(s) |
---|
896 | n/a | self.assertEqual(self._get_summary_line(p.dump), |
---|
897 | n/a | "dump(obj, /) method of _pickle.Pickler instance") |
---|
898 | n/a | |
---|
899 | n/a | # this should *never* include self! |
---|
900 | n/a | @requires_docstrings |
---|
901 | n/a | def test_module_level_callable(self): |
---|
902 | n/a | self.assertEqual(self._get_summary_line(os.stat), |
---|
903 | n/a | "stat(path, *, dir_fd=None, follow_symlinks=True)") |
---|
904 | n/a | |
---|
905 | n/a | |
---|
906 | n/a | @unittest.skipUnless(threading, 'Threading required for this test.') |
---|
907 | n/a | class PydocServerTest(unittest.TestCase): |
---|
908 | n/a | """Tests for pydoc._start_server""" |
---|
909 | n/a | |
---|
910 | n/a | def test_server(self): |
---|
911 | n/a | |
---|
912 | n/a | # Minimal test that starts the server, then stops it. |
---|
913 | n/a | def my_url_handler(url, content_type): |
---|
914 | n/a | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
---|
915 | n/a | return text |
---|
916 | n/a | |
---|
917 | n/a | serverthread = pydoc._start_server(my_url_handler, port=0) |
---|
918 | n/a | self.assertIn('localhost', serverthread.docserver.address) |
---|
919 | n/a | |
---|
920 | n/a | starttime = time.time() |
---|
921 | n/a | timeout = 1 #seconds |
---|
922 | n/a | |
---|
923 | n/a | while serverthread.serving: |
---|
924 | n/a | time.sleep(.01) |
---|
925 | n/a | if serverthread.serving and time.time() - starttime > timeout: |
---|
926 | n/a | serverthread.stop() |
---|
927 | n/a | break |
---|
928 | n/a | |
---|
929 | n/a | self.assertEqual(serverthread.error, None) |
---|
930 | n/a | |
---|
931 | n/a | |
---|
932 | n/a | class PydocUrlHandlerTest(PydocBaseTest): |
---|
933 | n/a | """Tests for pydoc._url_handler""" |
---|
934 | n/a | |
---|
935 | n/a | def test_content_type_err(self): |
---|
936 | n/a | f = pydoc._url_handler |
---|
937 | n/a | self.assertRaises(TypeError, f, 'A', '') |
---|
938 | n/a | self.assertRaises(TypeError, f, 'B', 'foobar') |
---|
939 | n/a | |
---|
940 | n/a | def test_url_requests(self): |
---|
941 | n/a | # Test for the correct title in the html pages returned. |
---|
942 | n/a | # This tests the different parts of the URL handler without |
---|
943 | n/a | # getting too picky about the exact html. |
---|
944 | n/a | requests = [ |
---|
945 | n/a | ("", "Pydoc: Index of Modules"), |
---|
946 | n/a | ("get?key=", "Pydoc: Index of Modules"), |
---|
947 | n/a | ("index", "Pydoc: Index of Modules"), |
---|
948 | n/a | ("topics", "Pydoc: Topics"), |
---|
949 | n/a | ("keywords", "Pydoc: Keywords"), |
---|
950 | n/a | ("pydoc", "Pydoc: module pydoc"), |
---|
951 | n/a | ("get?key=pydoc", "Pydoc: module pydoc"), |
---|
952 | n/a | ("search?key=pydoc", "Pydoc: Search Results"), |
---|
953 | n/a | ("topic?key=def", "Pydoc: KEYWORD def"), |
---|
954 | n/a | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
---|
955 | n/a | ("foobar", "Pydoc: Error - foobar"), |
---|
956 | n/a | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
---|
957 | n/a | ] |
---|
958 | n/a | |
---|
959 | n/a | with self.restrict_walk_packages(): |
---|
960 | n/a | for url, title in requests: |
---|
961 | n/a | self.call_url_handler(url, title) |
---|
962 | n/a | |
---|
963 | n/a | path = string.__file__ |
---|
964 | n/a | title = "Pydoc: getfile " + path |
---|
965 | n/a | url = "getfile?key=" + path |
---|
966 | n/a | self.call_url_handler(url, title) |
---|
967 | n/a | |
---|
968 | n/a | |
---|
969 | n/a | class TestHelper(unittest.TestCase): |
---|
970 | n/a | def test_keywords(self): |
---|
971 | n/a | self.assertEqual(sorted(pydoc.Helper.keywords), |
---|
972 | n/a | sorted(keyword.kwlist)) |
---|
973 | n/a | |
---|
974 | n/a | class PydocWithMetaClasses(unittest.TestCase): |
---|
975 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
976 | n/a | "Docstrings are omitted with -O2 and above") |
---|
977 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
978 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
979 | n/a | def test_DynamicClassAttribute(self): |
---|
980 | n/a | class Meta(type): |
---|
981 | n/a | def __getattr__(self, name): |
---|
982 | n/a | if name == 'ham': |
---|
983 | n/a | return 'spam' |
---|
984 | n/a | return super().__getattr__(name) |
---|
985 | n/a | class DA(metaclass=Meta): |
---|
986 | n/a | @types.DynamicClassAttribute |
---|
987 | n/a | def ham(self): |
---|
988 | n/a | return 'eggs' |
---|
989 | n/a | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
---|
990 | n/a | for s in expected_data_docstrings) |
---|
991 | n/a | output = StringIO() |
---|
992 | n/a | helper = pydoc.Helper(output=output) |
---|
993 | n/a | helper(DA) |
---|
994 | n/a | expected_text = expected_dynamicattribute_pattern % ( |
---|
995 | n/a | (__name__,) + expected_text_data_docstrings[:2]) |
---|
996 | n/a | result = output.getvalue().strip() |
---|
997 | n/a | self.assertEqual(expected_text, result) |
---|
998 | n/a | |
---|
999 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
1000 | n/a | "Docstrings are omitted with -O2 and above") |
---|
1001 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
1002 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
1003 | n/a | def test_virtualClassAttributeWithOneMeta(self): |
---|
1004 | n/a | class Meta(type): |
---|
1005 | n/a | def __dir__(cls): |
---|
1006 | n/a | return ['__class__', '__module__', '__name__', 'LIFE'] |
---|
1007 | n/a | def __getattr__(self, name): |
---|
1008 | n/a | if name =='LIFE': |
---|
1009 | n/a | return 42 |
---|
1010 | n/a | return super().__getattr(name) |
---|
1011 | n/a | class Class(metaclass=Meta): |
---|
1012 | n/a | pass |
---|
1013 | n/a | output = StringIO() |
---|
1014 | n/a | helper = pydoc.Helper(output=output) |
---|
1015 | n/a | helper(Class) |
---|
1016 | n/a | expected_text = expected_virtualattribute_pattern1 % __name__ |
---|
1017 | n/a | result = output.getvalue().strip() |
---|
1018 | n/a | self.assertEqual(expected_text, result) |
---|
1019 | n/a | |
---|
1020 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
1021 | n/a | "Docstrings are omitted with -O2 and above") |
---|
1022 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
1023 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
1024 | n/a | def test_virtualClassAttributeWithTwoMeta(self): |
---|
1025 | n/a | class Meta1(type): |
---|
1026 | n/a | def __dir__(cls): |
---|
1027 | n/a | return ['__class__', '__module__', '__name__', 'one'] |
---|
1028 | n/a | def __getattr__(self, name): |
---|
1029 | n/a | if name =='one': |
---|
1030 | n/a | return 1 |
---|
1031 | n/a | return super().__getattr__(name) |
---|
1032 | n/a | class Meta2(type): |
---|
1033 | n/a | def __dir__(cls): |
---|
1034 | n/a | return ['__class__', '__module__', '__name__', 'two'] |
---|
1035 | n/a | def __getattr__(self, name): |
---|
1036 | n/a | if name =='two': |
---|
1037 | n/a | return 2 |
---|
1038 | n/a | return super().__getattr__(name) |
---|
1039 | n/a | class Meta3(Meta1, Meta2): |
---|
1040 | n/a | def __dir__(cls): |
---|
1041 | n/a | return list(sorted(set( |
---|
1042 | n/a | ['__class__', '__module__', '__name__', 'three'] + |
---|
1043 | n/a | Meta1.__dir__(cls) + Meta2.__dir__(cls)))) |
---|
1044 | n/a | def __getattr__(self, name): |
---|
1045 | n/a | if name =='three': |
---|
1046 | n/a | return 3 |
---|
1047 | n/a | return super().__getattr__(name) |
---|
1048 | n/a | class Class1(metaclass=Meta1): |
---|
1049 | n/a | pass |
---|
1050 | n/a | class Class2(Class1, metaclass=Meta3): |
---|
1051 | n/a | pass |
---|
1052 | n/a | fail1 = fail2 = False |
---|
1053 | n/a | output = StringIO() |
---|
1054 | n/a | helper = pydoc.Helper(output=output) |
---|
1055 | n/a | helper(Class1) |
---|
1056 | n/a | expected_text1 = expected_virtualattribute_pattern2 % __name__ |
---|
1057 | n/a | result1 = output.getvalue().strip() |
---|
1058 | n/a | self.assertEqual(expected_text1, result1) |
---|
1059 | n/a | output = StringIO() |
---|
1060 | n/a | helper = pydoc.Helper(output=output) |
---|
1061 | n/a | helper(Class2) |
---|
1062 | n/a | expected_text2 = expected_virtualattribute_pattern3 % __name__ |
---|
1063 | n/a | result2 = output.getvalue().strip() |
---|
1064 | n/a | self.assertEqual(expected_text2, result2) |
---|
1065 | n/a | |
---|
1066 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
1067 | n/a | "Docstrings are omitted with -O2 and above") |
---|
1068 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
1069 | n/a | 'trace function introduces __locals__ unexpectedly') |
---|
1070 | n/a | def test_buggy_dir(self): |
---|
1071 | n/a | class M(type): |
---|
1072 | n/a | def __dir__(cls): |
---|
1073 | n/a | return ['__class__', '__name__', 'missing', 'here'] |
---|
1074 | n/a | class C(metaclass=M): |
---|
1075 | n/a | here = 'present!' |
---|
1076 | n/a | output = StringIO() |
---|
1077 | n/a | helper = pydoc.Helper(output=output) |
---|
1078 | n/a | helper(C) |
---|
1079 | n/a | expected_text = expected_missingattribute_pattern % __name__ |
---|
1080 | n/a | result = output.getvalue().strip() |
---|
1081 | n/a | self.assertEqual(expected_text, result) |
---|
1082 | n/a | |
---|
1083 | n/a | def test_resolve_false(self): |
---|
1084 | n/a | # Issue #23008: pydoc enum.{,Int}Enum failed |
---|
1085 | n/a | # because bool(enum.Enum) is False. |
---|
1086 | n/a | with captured_stdout() as help_io: |
---|
1087 | n/a | pydoc.help('enum.Enum') |
---|
1088 | n/a | helptext = help_io.getvalue() |
---|
1089 | n/a | self.assertIn('class Enum', helptext) |
---|
1090 | n/a | |
---|
1091 | n/a | |
---|
1092 | n/a | @reap_threads |
---|
1093 | n/a | def test_main(): |
---|
1094 | n/a | try: |
---|
1095 | n/a | test.support.run_unittest(PydocDocTest, |
---|
1096 | n/a | PydocImportTest, |
---|
1097 | n/a | TestDescriptions, |
---|
1098 | n/a | PydocServerTest, |
---|
1099 | n/a | PydocUrlHandlerTest, |
---|
1100 | n/a | TestHelper, |
---|
1101 | n/a | PydocWithMetaClasses, |
---|
1102 | n/a | ) |
---|
1103 | n/a | finally: |
---|
1104 | n/a | reap_children() |
---|
1105 | n/a | |
---|
1106 | n/a | if __name__ == "__main__": |
---|
1107 | n/a | test_main() |
---|