blob: f26fb15f535cbb857cedd8cfaa4c33eed31d397c [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import os
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002import sys
Éric Araujoe64e51b2011-07-29 17:03:55 +02003import builtins
Antoine Pitrou916fc7b2013-05-19 15:44:54 +02004import contextlib
Georg Brandlb533e262008-05-25 18:19:30 +00005import difflib
Georg Brandlb533e262008-05-25 18:19:30 +00006import inspect
Nick Coghlan7bb30b72010-12-03 09:29:11 +00007import pydoc
Ezio Melottib185a042011-04-28 07:42:55 +03008import keyword
Larry Hastings24a882b2014-02-20 23:34:46 -08009import _pickle
Antoine Pitrou916fc7b2013-05-19 15:44:54 +020010import pkgutil
Nick Coghlan7bb30b72010-12-03 09:29:11 +000011import re
12import string
Georg Brandlb533e262008-05-25 18:19:30 +000013import test.support
Nick Coghlan7bb30b72010-12-03 09:29:11 +000014import time
Ethan Furmanb0c84cd2013-10-20 22:37:39 -070015import types
Nick Coghlan7bb30b72010-12-03 09:29:11 +000016import unittest
Brian Curtin49c284c2010-03-31 03:19:28 +000017import xml.etree
Georg Brandld80d5f42010-12-03 07:47:22 +000018import textwrap
19from io import StringIO
Raymond Hettinger1103d052011-03-25 14:15:24 -070020from collections import namedtuple
Antoine Pitrouf7f54752011-07-15 22:42:12 +020021from test.script_helper import assert_python_ok
Antoine Pitroua6e81a22011-07-15 22:32:25 +020022from test.support import (
Ned Deily92a81a12011-10-06 14:19:03 -070023 TESTFN, rmtree,
Antoine Pitrou916fc7b2013-05-19 15:44:54 +020024 reap_children, reap_threads, captured_output, captured_stdout,
Stefan Krah5de32782014-01-18 23:18:39 +010025 captured_stderr, unlink, requires_docstrings
Antoine Pitroua6e81a22011-07-15 22:32:25 +020026)
Georg Brandlb533e262008-05-25 18:19:30 +000027from test import pydoc_mod
28
Victor Stinner62a68f22011-05-20 02:29:13 +020029try:
30 import threading
31except ImportError:
32 threading = None
33
Serhiy Storchaka9d0add02013-01-27 19:47:45 +020034if test.support.HAVE_DOCSTRINGS:
35 expected_data_docstrings = (
36 'dictionary for instance variables (if defined)',
37 'list of weak references to the object (if defined)',
38 ) * 2
39else:
40 expected_data_docstrings = ('', '', '', '')
41
Barry Warsaw28a691b2010-04-17 00:19:56 +000042expected_text_pattern = """
Georg Brandlb533e262008-05-25 18:19:30 +000043NAME
44 test.pydoc_mod - This is a test module for test_pydoc
Georg Brandlb533e262008-05-25 18:19:30 +000045%s
46CLASSES
47 builtins.object
48 A
49 B
50\x20\x20\x20\x20
51 class A(builtins.object)
52 | Hello and goodbye
53 |\x20\x20
54 | Methods defined here:
55 |\x20\x20
56 | __init__()
57 | Wow, I have no function!
58 |\x20\x20
59 | ----------------------------------------------------------------------
60 | Data descriptors defined here:
61 |\x20\x20
Serhiy Storchaka9d0add02013-01-27 19:47:45 +020062 | __dict__%s
Georg Brandlb533e262008-05-25 18:19:30 +000063 |\x20\x20
Serhiy Storchaka9d0add02013-01-27 19:47:45 +020064 | __weakref__%s
Georg Brandlb533e262008-05-25 18:19:30 +000065\x20\x20\x20\x20
66 class B(builtins.object)
67 | Data descriptors defined here:
68 |\x20\x20
Serhiy Storchaka9d0add02013-01-27 19:47:45 +020069 | __dict__%s
Georg Brandlb533e262008-05-25 18:19:30 +000070 |\x20\x20
Serhiy Storchaka9d0add02013-01-27 19:47:45 +020071 | __weakref__%s
Georg Brandlb533e262008-05-25 18:19:30 +000072 |\x20\x20
73 | ----------------------------------------------------------------------
74 | Data and other attributes defined here:
75 |\x20\x20
76 | NO_MEANING = 'eggs'
77
78FUNCTIONS
79 doc_func()
80 This function solves all of the world's problems:
81 hunger
82 lack of Python
83 war
84\x20\x20\x20\x20
85 nodoc_func()
86
87DATA
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000088 __xyz__ = 'X, Y and Z'
Georg Brandlb533e262008-05-25 18:19:30 +000089
90VERSION
91 1.2.3.4
92
93AUTHOR
94 Benjamin Peterson
95
96CREDITS
97 Nobody
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000098
99FILE
100 %s
Georg Brandlb533e262008-05-25 18:19:30 +0000101""".strip()
102
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200103expected_text_data_docstrings = tuple('\n | ' + s if s else ''
104 for s in expected_data_docstrings)
105
Barry Warsaw28a691b2010-04-17 00:19:56 +0000106expected_html_pattern = """
Georg Brandlb533e262008-05-25 18:19:30 +0000107<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
108<tr bgcolor="#7799ee">
109<td valign=bottom>&nbsp;<br>
110<font color="#ffffff" face="helvetica, arial">&nbsp;<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
111><td align=right valign=bottom
112><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
113 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
114<p>
115<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
116<tr bgcolor="#ee77aa">
117<td colspan=3 valign=bottom>&nbsp;<br>
118<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
119\x20\x20\x20\x20
120<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
121<td width="100%%"><dl>
122<dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a>
123</font></dt><dd>
124<dl>
125<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
126</font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
127</font></dt></dl>
128</dd>
129</dl>
130 <p>
131<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
132<tr bgcolor="#ffc8d8">
133<td colspan=3 valign=bottom>&nbsp;<br>
134<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>
135\x20\x20\x20\x20
136<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
137<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
138<tr><td>&nbsp;</td>
139<td width="100%%">Methods defined here:<br>
140<dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow,&nbsp;I&nbsp;have&nbsp;no&nbsp;function!</tt></dd></dl>
141
142<hr>
143Data descriptors defined here:<br>
144<dl><dt><strong>__dict__</strong></dt>
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200145<dd><tt>%s</tt></dd>
Georg Brandlb533e262008-05-25 18:19:30 +0000146</dl>
147<dl><dt><strong>__weakref__</strong></dt>
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200148<dd><tt>%s</tt></dd>
Georg Brandlb533e262008-05-25 18:19:30 +0000149</dl>
150</td></tr></table> <p>
151<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
152<tr bgcolor="#ffc8d8">
153<td colspan=3 valign=bottom>&nbsp;<br>
154<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>
155\x20\x20\x20\x20
156<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
157<td width="100%%">Data descriptors defined here:<br>
158<dl><dt><strong>__dict__</strong></dt>
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200159<dd><tt>%s</tt></dd>
Georg Brandlb533e262008-05-25 18:19:30 +0000160</dl>
161<dl><dt><strong>__weakref__</strong></dt>
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200162<dd><tt>%s</tt></dd>
Georg Brandlb533e262008-05-25 18:19:30 +0000163</dl>
164<hr>
165Data and other attributes defined here:<br>
166<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
167
168</td></tr></table></td></tr></table><p>
169<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
170<tr bgcolor="#eeaa77">
171<td colspan=3 valign=bottom>&nbsp;<br>
172<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
173\x20\x20\x20\x20
174<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
175<td width="100%%"><dl><dt><a name="-doc_func"><strong>doc_func</strong></a>()</dt><dd><tt>This&nbsp;function&nbsp;solves&nbsp;all&nbsp;of&nbsp;the&nbsp;world's&nbsp;problems:<br>
176hunger<br>
177lack&nbsp;of&nbsp;Python<br>
178war</tt></dd></dl>
179 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
180</td></tr></table><p>
181<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
182<tr bgcolor="#55aa55">
183<td colspan=3 valign=bottom>&nbsp;<br>
184<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
185\x20\x20\x20\x20
186<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000187<td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p>
Georg Brandlb533e262008-05-25 18:19:30 +0000188<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
189<tr bgcolor="#7799ee">
190<td colspan=3 valign=bottom>&nbsp;<br>
191<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
192\x20\x20\x20\x20
193<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
194<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
195<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
196<tr bgcolor="#7799ee">
197<td colspan=3 valign=bottom>&nbsp;<br>
198<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
199\x20\x20\x20\x20
200<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
201<td width="100%%">Nobody</td></tr></table>
Barry Warsaw28a691b2010-04-17 00:19:56 +0000202""".strip() # ' <- emacs turd
Georg Brandlb533e262008-05-25 18:19:30 +0000203
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200204expected_html_data_docstrings = tuple(s.replace(' ', '&nbsp;')
205 for s in expected_data_docstrings)
Georg Brandlb533e262008-05-25 18:19:30 +0000206
207# output pattern for missing module
208missing_pattern = "no Python documentation found for '%s'"
209
Benjamin Peterson0289b152009-06-28 17:22:03 +0000210# output pattern for module with bad imports
Brett Cannon679ecb52013-07-04 17:51:50 -0400211badimport_pattern = "problem in %s - ImportError: No module named %r"
Benjamin Peterson0289b152009-06-28 17:22:03 +0000212
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700213expected_dynamicattribute_pattern = """
214Help on class DA in module %s:
215
216class DA(builtins.object)
217 | Data descriptors defined here:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200218 |\x20\x20
Ethan Furman3f2f1922013-10-22 07:30:24 -0700219 | __dict__%s
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200220 |\x20\x20
Ethan Furman3f2f1922013-10-22 07:30:24 -0700221 | __weakref__%s
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200222 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700223 | ham
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200224 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700225 | ----------------------------------------------------------------------
226 | Data and other attributes inherited from Meta:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200227 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700228 | ham = 'spam'
229""".strip()
230
231expected_virtualattribute_pattern1 = """
232Help on class Class in module %s:
233
234class Class(builtins.object)
235 | Data and other attributes inherited from Meta:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200236 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700237 | LIFE = 42
238""".strip()
239
240expected_virtualattribute_pattern2 = """
241Help on class Class1 in module %s:
242
243class Class1(builtins.object)
244 | Data and other attributes inherited from Meta1:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200245 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700246 | one = 1
247""".strip()
248
249expected_virtualattribute_pattern3 = """
250Help on class Class2 in module %s:
251
252class Class2(Class1)
253 | Method resolution order:
254 | Class2
255 | Class1
256 | builtins.object
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200257 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700258 | Data and other attributes inherited from Meta1:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200259 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700260 | one = 1
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200261 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700262 | ----------------------------------------------------------------------
263 | Data and other attributes inherited from Meta3:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200264 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700265 | three = 3
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200266 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700267 | ----------------------------------------------------------------------
268 | Data and other attributes inherited from Meta2:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200269 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700270 | two = 2
271""".strip()
272
273expected_missingattribute_pattern = """
274Help on class C in module %s:
275
276class C(builtins.object)
277 | Data and other attributes defined here:
Charles-François Natali1a82f7e2013-10-21 14:46:34 +0200278 |\x20\x20
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700279 | here = 'present!'
280""".strip()
281
Antoine Pitrouf7f54752011-07-15 22:42:12 +0200282def run_pydoc(module_name, *args, **env):
Georg Brandlb533e262008-05-25 18:19:30 +0000283 """
284 Runs pydoc on the specified module. Returns the stripped
285 output of pydoc.
286 """
Antoine Pitrouf7f54752011-07-15 22:42:12 +0200287 args = args + (module_name,)
Ned Deily92a81a12011-10-06 14:19:03 -0700288 # do not write bytecode files to avoid caching errors
289 rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env)
Antoine Pitrouf7f54752011-07-15 22:42:12 +0200290 return out.strip()
Georg Brandlb533e262008-05-25 18:19:30 +0000291
292def get_pydoc_html(module):
293 "Returns pydoc generated output as html"
294 doc = pydoc.HTMLDoc()
295 output = doc.docmodule(module)
296 loc = doc.getdocloc(pydoc_mod) or ""
297 if loc:
298 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
299 return output.strip(), loc
300
301def get_pydoc_text(module):
302 "Returns pydoc generated output as text"
303 doc = pydoc.TextDoc()
304 loc = doc.getdocloc(pydoc_mod) or ""
305 if loc:
306 loc = "\nMODULE DOCS\n " + loc + "\n"
307
308 output = doc.docmodule(module)
309
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000310 # clean up the extra text formatting that pydoc performs
Georg Brandlb533e262008-05-25 18:19:30 +0000311 patt = re.compile('\b.')
312 output = patt.sub('', output)
313 return output.strip(), loc
314
315def print_diffs(text1, text2):
316 "Prints unified diffs for two texts"
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000317 # XXX now obsolete, use unittest built-in support
Ezio Melottid8b509b2011-09-28 17:37:55 +0300318 lines1 = text1.splitlines(keepends=True)
319 lines2 = text2.splitlines(keepends=True)
Georg Brandlb533e262008-05-25 18:19:30 +0000320 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
321 tofile='got')
322 print('\n' + ''.join(diffs))
323
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000324def get_html_title(text):
Nick Coghlanecace282010-12-03 16:08:46 +0000325 # Bit of hack, but good enough for test purposes
326 header, _, _ = text.partition("</head>")
327 _, _, title = header.partition("<title>")
328 title, _, _ = title.partition("</title>")
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000329 return title
330
Georg Brandlb533e262008-05-25 18:19:30 +0000331
Antoine Pitrou916fc7b2013-05-19 15:44:54 +0200332class PydocBaseTest(unittest.TestCase):
333
334 def _restricted_walk_packages(self, walk_packages, path=None):
335 """
336 A version of pkgutil.walk_packages() that will restrict itself to
337 a given path.
338 """
339 default_path = path or [os.path.dirname(__file__)]
340 def wrapper(path=None, prefix='', onerror=None):
341 return walk_packages(path or default_path, prefix, onerror)
342 return wrapper
343
344 @contextlib.contextmanager
345 def restrict_walk_packages(self, path=None):
346 walk_packages = pkgutil.walk_packages
347 pkgutil.walk_packages = self._restricted_walk_packages(walk_packages,
348 path)
349 try:
350 yield
351 finally:
352 pkgutil.walk_packages = walk_packages
353
354
Georg Brandld2f38572011-01-30 08:37:19 +0000355class PydocDocTest(unittest.TestCase):
Georg Brandlb533e262008-05-25 18:19:30 +0000356
R. David Murray378c0cf2010-02-24 01:46:21 +0000357 @unittest.skipIf(sys.flags.optimize >= 2,
358 "Docstrings are omitted with -O2 and above")
Brett Cannon7a540732011-02-22 03:04:06 +0000359 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
360 'trace function introduces __locals__ unexpectedly')
Georg Brandlb533e262008-05-25 18:19:30 +0000361 def test_html_doc(self):
362 result, doc_loc = get_pydoc_html(pydoc_mod)
363 mod_file = inspect.getabsfile(pydoc_mod)
Benjamin Petersonc5e94642008-06-14 23:04:46 +0000364 if sys.platform == 'win32':
365 import nturl2path
366 mod_url = nturl2path.pathname2url(mod_file)
367 else:
368 mod_url = mod_file
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200369 expected_html = expected_html_pattern % (
370 (mod_url, mod_file, doc_loc) +
371 expected_html_data_docstrings)
Georg Brandlb533e262008-05-25 18:19:30 +0000372 if result != expected_html:
373 print_diffs(expected_html, result)
374 self.fail("outputs are not equal, see diff above")
375
R. David Murray378c0cf2010-02-24 01:46:21 +0000376 @unittest.skipIf(sys.flags.optimize >= 2,
377 "Docstrings are omitted with -O2 and above")
Brett Cannon7a540732011-02-22 03:04:06 +0000378 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
379 'trace function introduces __locals__ unexpectedly')
Georg Brandlb533e262008-05-25 18:19:30 +0000380 def test_text_doc(self):
381 result, doc_loc = get_pydoc_text(pydoc_mod)
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200382 expected_text = expected_text_pattern % (
383 (doc_loc,) +
384 expected_text_data_docstrings +
385 (inspect.getabsfile(pydoc_mod),))
Georg Brandlb533e262008-05-25 18:19:30 +0000386 if result != expected_text:
387 print_diffs(expected_text, result)
388 self.fail("outputs are not equal, see diff above")
389
Serhiy Storchaka056eb022014-02-19 23:05:12 +0200390 def test_text_enum_member_with_value_zero(self):
391 # Test issue #20654 to ensure enum member with value 0 can be
392 # displayed. It used to throw KeyError: 'zero'.
393 import enum
394 class BinaryInteger(enum.IntEnum):
395 zero = 0
396 one = 1
397 doc = pydoc.render_doc(BinaryInteger)
398 self.assertIn('<BinaryInteger.zero: 0>', doc)
399
Brian Curtin49c284c2010-03-31 03:19:28 +0000400 def test_issue8225(self):
401 # Test issue8225 to ensure no doc link appears for xml.etree
402 result, doc_loc = get_pydoc_text(xml.etree)
403 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
404
R David Murrayc43125a2012-04-23 13:23:57 -0400405 def test_non_str_name(self):
406 # issue14638
407 # Treat illegal (non-str) name like no name
408 class A:
409 __name__ = 42
410 class B:
411 pass
412 adoc = pydoc.render_doc(A())
413 bdoc = pydoc.render_doc(B())
414 self.assertEqual(adoc.replace("A", "B"), bdoc)
415
Georg Brandlb533e262008-05-25 18:19:30 +0000416 def test_not_here(self):
417 missing_module = "test.i_am_not_here"
418 result = str(run_pydoc(missing_module), 'ascii')
419 expected = missing_pattern % missing_module
420 self.assertEqual(expected, result,
421 "documentation for missing module found")
422
R. David Murray1f1b9d32009-05-27 20:56:59 +0000423 def test_input_strip(self):
424 missing_module = " test.i_am_not_here "
425 result = str(run_pydoc(missing_module), 'ascii')
426 expected = missing_pattern % missing_module.strip()
427 self.assertEqual(expected, result)
428
Ezio Melotti412c95a2010-02-16 23:31:04 +0000429 def test_stripid(self):
430 # test with strings, other implementations might have different repr()
431 stripid = pydoc.stripid
432 # strip the id
433 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
434 '<function stripid>')
435 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
436 '<function stripid>')
437 # nothing to strip, return the same text
438 self.assertEqual(stripid('42'), '42')
439 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
440 "<type 'exceptions.Exception'>")
441
Georg Brandld80d5f42010-12-03 07:47:22 +0000442 @unittest.skipIf(sys.flags.optimize >= 2,
443 'Docstrings are omitted with -O2 and above')
Brett Cannon7a540732011-02-22 03:04:06 +0000444 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
445 'trace function introduces __locals__ unexpectedly')
Georg Brandld80d5f42010-12-03 07:47:22 +0000446 def test_help_output_redirect(self):
447 # issue 940286, if output is set in Helper, then all output from
448 # Helper.help should be redirected
449 old_pattern = expected_text_pattern
450 getpager_old = pydoc.getpager
451 getpager_new = lambda: (lambda x: x)
452 self.maxDiff = None
453
454 buf = StringIO()
455 helper = pydoc.Helper(output=buf)
456 unused, doc_loc = get_pydoc_text(pydoc_mod)
457 module = "test.pydoc_mod"
458 help_header = """
459 Help on module test.pydoc_mod in test:
460
461 """.lstrip()
462 help_header = textwrap.dedent(help_header)
463 expected_help_pattern = help_header + expected_text_pattern
464
465 pydoc.getpager = getpager_new
466 try:
467 with captured_output('stdout') as output, \
468 captured_output('stderr') as err:
469 helper.help(module)
470 result = buf.getvalue().strip()
Serhiy Storchaka9d0add02013-01-27 19:47:45 +0200471 expected_text = expected_help_pattern % (
472 (doc_loc,) +
473 expected_text_data_docstrings +
474 (inspect.getabsfile(pydoc_mod),))
Georg Brandld80d5f42010-12-03 07:47:22 +0000475 self.assertEqual('', output.getvalue())
476 self.assertEqual('', err.getvalue())
477 self.assertEqual(expected_text, result)
478 finally:
479 pydoc.getpager = getpager_old
480
Raymond Hettinger1103d052011-03-25 14:15:24 -0700481 def test_namedtuple_public_underscore(self):
482 NT = namedtuple('NT', ['abc', 'def'], rename=True)
483 with captured_stdout() as help_io:
Terry Jan Reedy5c811642013-11-04 21:43:26 -0500484 pydoc.help(NT)
Raymond Hettinger1103d052011-03-25 14:15:24 -0700485 helptext = help_io.getvalue()
486 self.assertIn('_1', helptext)
487 self.assertIn('_replace', helptext)
488 self.assertIn('_asdict', helptext)
489
Victor Stinnere6c910e2011-06-30 15:55:43 +0200490 def test_synopsis(self):
491 self.addCleanup(unlink, TESTFN)
492 for encoding in ('ISO-8859-1', 'UTF-8'):
493 with open(TESTFN, 'w', encoding=encoding) as script:
494 if encoding != 'UTF-8':
495 print('#coding: {}'.format(encoding), file=script)
496 print('"""line 1: h\xe9', file=script)
497 print('line 2: hi"""', file=script)
498 synopsis = pydoc.synopsis(TESTFN, {})
499 self.assertEqual(synopsis, 'line 1: h\xe9')
500
Eric Snowaed5b222014-01-04 20:38:11 -0700501 def test_synopsis_sourceless(self):
502 expected = os.__doc__.splitlines()[0]
503 filename = os.__cached__
504 synopsis = pydoc.synopsis(filename)
505
506 self.assertEqual(synopsis, expected)
507
R David Murray455f2962013-03-19 00:00:33 -0400508 def test_splitdoc_with_description(self):
509 example_string = "I Am A Doc\n\n\nHere is my description"
510 self.assertEqual(pydoc.splitdoc(example_string),
511 ('I Am A Doc', '\nHere is my description'))
512
513 def test_is_object_or_method(self):
514 doc = pydoc.Doc()
515 # Bound Method
516 self.assertTrue(pydoc._is_some_method(doc.fail))
517 # Method Descriptor
518 self.assertTrue(pydoc._is_some_method(int.__add__))
519 # String
520 self.assertFalse(pydoc._is_some_method("I am not a method"))
521
522 def test_is_package_when_not_package(self):
523 with test.support.temp_cwd() as test_dir:
524 self.assertFalse(pydoc.ispackage(test_dir))
525
526 def test_is_package_when_is_package(self):
527 with test.support.temp_cwd() as test_dir:
528 init_path = os.path.join(test_dir, '__init__.py')
529 open(init_path, 'w').close()
530 self.assertTrue(pydoc.ispackage(test_dir))
531 os.remove(init_path)
532
R David Murrayac0cea52013-03-19 02:47:44 -0400533 def test_allmethods(self):
534 # issue 17476: allmethods was no longer returning unbound methods.
535 # This test is a bit fragile in the face of changes to object and type,
536 # but I can't think of a better way to do it without duplicating the
537 # logic of the function under test.
538
539 class TestClass(object):
540 def method_returning_true(self):
541 return True
542
543 # What we expect to get back: everything on object...
544 expected = dict(vars(object))
545 # ...plus our unbound method...
546 expected['method_returning_true'] = TestClass.method_returning_true
547 # ...but not the non-methods on object.
548 del expected['__doc__']
549 del expected['__class__']
550 # inspect resolves descriptors on type into methods, but vars doesn't,
551 # so we need to update __subclasshook__.
552 expected['__subclasshook__'] = TestClass.__subclasshook__
553
554 methods = pydoc.allmethods(TestClass)
555 self.assertDictEqual(methods, expected)
556
Georg Brandlb533e262008-05-25 18:19:30 +0000557
Antoine Pitrou916fc7b2013-05-19 15:44:54 +0200558class PydocImportTest(PydocBaseTest):
Ned Deily92a81a12011-10-06 14:19:03 -0700559
560 def setUp(self):
561 self.test_dir = os.mkdir(TESTFN)
562 self.addCleanup(rmtree, TESTFN)
563
564 def test_badimport(self):
565 # This tests the fix for issue 5230, where if pydoc found the module
566 # but the module had an internal import error pydoc would report no doc
567 # found.
568 modname = 'testmod_xyzzy'
569 testpairs = (
570 ('i_am_not_here', 'i_am_not_here'),
Brett Cannonfd074152012-04-14 14:10:13 -0400571 ('test.i_am_not_here_either', 'test.i_am_not_here_either'),
572 ('test.i_am_not_here.neither_am_i', 'test.i_am_not_here'),
573 ('i_am_not_here.{}'.format(modname), 'i_am_not_here'),
574 ('test.{}'.format(modname), 'test.{}'.format(modname)),
Ned Deily92a81a12011-10-06 14:19:03 -0700575 )
576
577 sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py"
578 for importstring, expectedinmsg in testpairs:
579 with open(sourcefn, 'w') as f:
580 f.write("import {}\n".format(importstring))
581 result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii")
582 expected = badimport_pattern % (modname, expectedinmsg)
583 self.assertEqual(expected, result)
584
585 def test_apropos_with_bad_package(self):
586 # Issue 7425 - pydoc -k failed when bad package on path
587 pkgdir = os.path.join(TESTFN, "syntaxerr")
588 os.mkdir(pkgdir)
589 badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py"
590 with open(badsyntax, 'w') as f:
591 f.write("invalid python syntax = $1\n")
Antoine Pitrou916fc7b2013-05-19 15:44:54 +0200592 with self.restrict_walk_packages(path=[TESTFN]):
593 with captured_stdout() as out:
594 with captured_stderr() as err:
595 pydoc.apropos('xyzzy')
596 # No result, no error
597 self.assertEqual(out.getvalue(), '')
598 self.assertEqual(err.getvalue(), '')
599 # The package name is still matched
600 with captured_stdout() as out:
601 with captured_stderr() as err:
602 pydoc.apropos('syntaxerr')
603 self.assertEqual(out.getvalue().strip(), 'syntaxerr')
604 self.assertEqual(err.getvalue(), '')
Ned Deily92a81a12011-10-06 14:19:03 -0700605
606 def test_apropos_with_unreadable_dir(self):
607 # Issue 7367 - pydoc -k failed when unreadable dir on path
608 self.unreadable_dir = os.path.join(TESTFN, "unreadable")
609 os.mkdir(self.unreadable_dir, 0)
610 self.addCleanup(os.rmdir, self.unreadable_dir)
611 # Note, on Windows the directory appears to be still
612 # readable so this is not really testing the issue there
Antoine Pitrou916fc7b2013-05-19 15:44:54 +0200613 with self.restrict_walk_packages(path=[TESTFN]):
614 with captured_stdout() as out:
615 with captured_stderr() as err:
616 pydoc.apropos('SOMEKEY')
617 # No result, no error
618 self.assertEqual(out.getvalue(), '')
619 self.assertEqual(err.getvalue(), '')
Ned Deily92a81a12011-10-06 14:19:03 -0700620
Eric Snowa46ef702014-02-22 13:57:08 -0700621 @unittest.skip('causes undesireable side-effects (#20128)')
Eric Snowaed5b222014-01-04 20:38:11 -0700622 def test_modules(self):
623 # See Helper.listmodules().
624 num_header_lines = 2
625 num_module_lines_min = 5 # Playing it safe.
626 num_footer_lines = 3
627 expected = num_header_lines + num_module_lines_min + num_footer_lines
628
629 output = StringIO()
630 helper = pydoc.Helper(output=output)
631 helper('modules')
632 result = output.getvalue().strip()
633 num_lines = len(result.splitlines())
634
635 self.assertGreaterEqual(num_lines, expected)
636
Eric Snowa46ef702014-02-22 13:57:08 -0700637 @unittest.skip('causes undesireable side-effects (#20128)')
Eric Snowaed5b222014-01-04 20:38:11 -0700638 def test_modules_search(self):
639 # See Helper.listmodules().
640 expected = 'pydoc - '
641
642 output = StringIO()
643 helper = pydoc.Helper(output=output)
644 with captured_stdout() as help_io:
645 helper('modules pydoc')
646 result = help_io.getvalue()
647
648 self.assertIn(expected, result)
649
Eric Snowa46ef702014-02-22 13:57:08 -0700650 @unittest.skip('some buildbots are not cooperating (#20128)')
Eric Snowaed5b222014-01-04 20:38:11 -0700651 def test_modules_search_builtin(self):
Eric Snow5ea97502014-01-04 23:04:27 -0700652 expected = 'gc - '
Eric Snowaed5b222014-01-04 20:38:11 -0700653
654 output = StringIO()
655 helper = pydoc.Helper(output=output)
656 with captured_stdout() as help_io:
Eric Snow5ea97502014-01-04 23:04:27 -0700657 helper('modules garbage')
Eric Snowaed5b222014-01-04 20:38:11 -0700658 result = help_io.getvalue()
659
660 self.assertTrue(result.startswith(expected))
661
662 def test_importfile(self):
663 loaded_pydoc = pydoc.importfile(pydoc.__file__)
664
Eric Snow3a62d142014-01-06 20:42:59 -0700665 self.assertIsNot(loaded_pydoc, pydoc)
Eric Snowaed5b222014-01-04 20:38:11 -0700666 self.assertEqual(loaded_pydoc.__name__, 'pydoc')
667 self.assertEqual(loaded_pydoc.__file__, pydoc.__file__)
Eric Snow3a62d142014-01-06 20:42:59 -0700668 self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__)
Eric Snowaed5b222014-01-04 20:38:11 -0700669
Ned Deily92a81a12011-10-06 14:19:03 -0700670
Georg Brandlb533e262008-05-25 18:19:30 +0000671class TestDescriptions(unittest.TestCase):
672
673 def test_module(self):
674 # Check that pydocfodder module can be described
675 from test import pydocfodder
676 doc = pydoc.render_doc(pydocfodder)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000677 self.assertIn("pydocfodder", doc)
Georg Brandlb533e262008-05-25 18:19:30 +0000678
Georg Brandlb533e262008-05-25 18:19:30 +0000679 def test_class(self):
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000680 class C: "New-style class"
Georg Brandlb533e262008-05-25 18:19:30 +0000681 c = C()
682
683 self.assertEqual(pydoc.describe(C), 'class C')
684 self.assertEqual(pydoc.describe(c), 'C')
685 expected = 'C in module %s object' % __name__
Benjamin Peterson577473f2010-01-19 00:09:57 +0000686 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandlb533e262008-05-25 18:19:30 +0000687
Éric Araujoe64e51b2011-07-29 17:03:55 +0200688 def test_builtin(self):
689 for name in ('str', 'str.translate', 'builtins.str',
690 'builtins.str.translate'):
691 # test low-level function
692 self.assertIsNotNone(pydoc.locate(name))
693 # test high-level function
694 try:
695 pydoc.render_doc(name)
696 except ImportError:
697 self.fail('finding the doc of {!r} failed'.format(o))
698
699 for name in ('notbuiltins', 'strrr', 'strr.translate',
700 'str.trrrranslate', 'builtins.strrr',
701 'builtins.str.trrranslate'):
702 self.assertIsNone(pydoc.locate(name))
703 self.assertRaises(ImportError, pydoc.render_doc, name)
704
Larry Hastings24a882b2014-02-20 23:34:46 -0800705 @staticmethod
706 def _get_summary_line(o):
707 text = pydoc.plain(pydoc.render_doc(o))
708 lines = text.split('\n')
709 assert len(lines) >= 2
710 return lines[2]
711
712 # these should include "self"
713 def test_unbound_python_method(self):
714 self.assertEqual(self._get_summary_line(textwrap.TextWrapper.wrap),
715 "wrap(self, text)")
716
Stefan Krah5de32782014-01-18 23:18:39 +0100717 @requires_docstrings
Larry Hastings24a882b2014-02-20 23:34:46 -0800718 def test_unbound_builtin_method(self):
719 self.assertEqual(self._get_summary_line(_pickle.Pickler.dump),
720 "dump(self, obj, /)")
721
722 # these no longer include "self"
723 def test_bound_python_method(self):
724 t = textwrap.TextWrapper()
725 self.assertEqual(self._get_summary_line(t.wrap),
726 "wrap(text) method of textwrap.TextWrapper instance")
727
728 @requires_docstrings
729 def test_bound_builtin_method(self):
730 s = StringIO()
731 p = _pickle.Pickler(s)
732 self.assertEqual(self._get_summary_line(p.dump),
733 "dump(obj, /) method of _pickle.Pickler instance")
734
735 # this should *never* include self!
736 @requires_docstrings
737 def test_module_level_callable(self):
738 self.assertEqual(self._get_summary_line(os.stat),
739 "stat(path, *, dir_fd=None, follow_symlinks=True)")
Larry Hastings1abd7082014-01-16 14:15:03 -0800740
Georg Brandlb533e262008-05-25 18:19:30 +0000741
Victor Stinner62a68f22011-05-20 02:29:13 +0200742@unittest.skipUnless(threading, 'Threading required for this test.')
Georg Brandld2f38572011-01-30 08:37:19 +0000743class PydocServerTest(unittest.TestCase):
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000744 """Tests for pydoc._start_server"""
745
746 def test_server(self):
747
748 # Minimal test that starts the server, then stops it.
749 def my_url_handler(url, content_type):
750 text = 'the URL sent was: (%s, %s)' % (url, content_type)
751 return text
752
753 serverthread = pydoc._start_server(my_url_handler, port=0)
754 starttime = time.time()
755 timeout = 1 #seconds
756
757 while serverthread.serving:
758 time.sleep(.01)
759 if serverthread.serving and time.time() - starttime > timeout:
760 serverthread.stop()
761 break
762
763 self.assertEqual(serverthread.error, None)
764
765
Antoine Pitrou916fc7b2013-05-19 15:44:54 +0200766class PydocUrlHandlerTest(PydocBaseTest):
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000767 """Tests for pydoc._url_handler"""
768
769 def test_content_type_err(self):
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000770 f = pydoc._url_handler
Georg Brandld2f38572011-01-30 08:37:19 +0000771 self.assertRaises(TypeError, f, 'A', '')
772 self.assertRaises(TypeError, f, 'B', 'foobar')
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000773
774 def test_url_requests(self):
775 # Test for the correct title in the html pages returned.
776 # This tests the different parts of the URL handler without
777 # getting too picky about the exact html.
778 requests = [
Georg Brandld2f38572011-01-30 08:37:19 +0000779 ("", "Pydoc: Index of Modules"),
780 ("get?key=", "Pydoc: Index of Modules"),
781 ("index", "Pydoc: Index of Modules"),
782 ("topics", "Pydoc: Topics"),
783 ("keywords", "Pydoc: Keywords"),
784 ("pydoc", "Pydoc: module pydoc"),
785 ("get?key=pydoc", "Pydoc: module pydoc"),
786 ("search?key=pydoc", "Pydoc: Search Results"),
787 ("topic?key=def", "Pydoc: KEYWORD def"),
788 ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"),
789 ("foobar", "Pydoc: Error - foobar"),
790 ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"),
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000791 ]
792
Antoine Pitrou916fc7b2013-05-19 15:44:54 +0200793 with self.restrict_walk_packages():
794 for url, title in requests:
795 text = pydoc._url_handler(url, "text/html")
796 result = get_html_title(text)
797 self.assertEqual(result, title, text)
798
799 path = string.__file__
800 title = "Pydoc: getfile " + path
801 url = "getfile?key=" + path
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000802 text = pydoc._url_handler(url, "text/html")
803 result = get_html_title(text)
804 self.assertEqual(result, title)
805
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000806
Ezio Melottib185a042011-04-28 07:42:55 +0300807class TestHelper(unittest.TestCase):
808 def test_keywords(self):
809 self.assertEqual(sorted(pydoc.Helper.keywords),
810 sorted(keyword.kwlist))
811
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700812class PydocWithMetaClasses(unittest.TestCase):
Ethan Furman3f2f1922013-10-22 07:30:24 -0700813 @unittest.skipIf(sys.flags.optimize >= 2,
814 "Docstrings are omitted with -O2 and above")
815 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
816 'trace function introduces __locals__ unexpectedly')
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700817 def test_DynamicClassAttribute(self):
818 class Meta(type):
819 def __getattr__(self, name):
820 if name == 'ham':
821 return 'spam'
822 return super().__getattr__(name)
823 class DA(metaclass=Meta):
824 @types.DynamicClassAttribute
825 def ham(self):
826 return 'eggs'
Ethan Furman3f2f1922013-10-22 07:30:24 -0700827 expected_text_data_docstrings = tuple('\n | ' + s if s else ''
828 for s in expected_data_docstrings)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700829 output = StringIO()
830 helper = pydoc.Helper(output=output)
831 helper(DA)
Ethan Furman3f2f1922013-10-22 07:30:24 -0700832 expected_text = expected_dynamicattribute_pattern % (
833 (__name__,) + expected_text_data_docstrings[:2])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700834 result = output.getvalue().strip()
835 if result != expected_text:
836 print_diffs(expected_text, result)
837 self.fail("outputs are not equal, see diff above")
838
Ethan Furman3f2f1922013-10-22 07:30:24 -0700839 @unittest.skipIf(sys.flags.optimize >= 2,
840 "Docstrings are omitted with -O2 and above")
841 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
842 'trace function introduces __locals__ unexpectedly')
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700843 def test_virtualClassAttributeWithOneMeta(self):
844 class Meta(type):
845 def __dir__(cls):
846 return ['__class__', '__module__', '__name__', 'LIFE']
847 def __getattr__(self, name):
848 if name =='LIFE':
849 return 42
850 return super().__getattr(name)
851 class Class(metaclass=Meta):
852 pass
853 output = StringIO()
854 helper = pydoc.Helper(output=output)
855 helper(Class)
856 expected_text = expected_virtualattribute_pattern1 % __name__
857 result = output.getvalue().strip()
858 if result != expected_text:
859 print_diffs(expected_text, result)
860 self.fail("outputs are not equal, see diff above")
861
Ethan Furman3f2f1922013-10-22 07:30:24 -0700862 @unittest.skipIf(sys.flags.optimize >= 2,
863 "Docstrings are omitted with -O2 and above")
864 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
865 'trace function introduces __locals__ unexpectedly')
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700866 def test_virtualClassAttributeWithTwoMeta(self):
867 class Meta1(type):
868 def __dir__(cls):
869 return ['__class__', '__module__', '__name__', 'one']
870 def __getattr__(self, name):
871 if name =='one':
872 return 1
873 return super().__getattr__(name)
874 class Meta2(type):
875 def __dir__(cls):
876 return ['__class__', '__module__', '__name__', 'two']
877 def __getattr__(self, name):
878 if name =='two':
879 return 2
880 return super().__getattr__(name)
881 class Meta3(Meta1, Meta2):
882 def __dir__(cls):
883 return list(sorted(set(
884 ['__class__', '__module__', '__name__', 'three'] +
885 Meta1.__dir__(cls) + Meta2.__dir__(cls))))
886 def __getattr__(self, name):
887 if name =='three':
888 return 3
889 return super().__getattr__(name)
890 class Class1(metaclass=Meta1):
891 pass
892 class Class2(Class1, metaclass=Meta3):
893 pass
894 fail1 = fail2 = False
895 output = StringIO()
896 helper = pydoc.Helper(output=output)
897 helper(Class1)
898 expected_text1 = expected_virtualattribute_pattern2 % __name__
899 result1 = output.getvalue().strip()
900 if result1 != expected_text1:
901 print_diffs(expected_text1, result1)
902 fail1 = True
903 output = StringIO()
904 helper = pydoc.Helper(output=output)
905 helper(Class2)
906 expected_text2 = expected_virtualattribute_pattern3 % __name__
907 result2 = output.getvalue().strip()
908 if result2 != expected_text2:
909 print_diffs(expected_text2, result2)
910 fail2 = True
911 if fail1 or fail2:
912 self.fail("outputs are not equal, see diff above")
913
Ethan Furman3f2f1922013-10-22 07:30:24 -0700914 @unittest.skipIf(sys.flags.optimize >= 2,
915 "Docstrings are omitted with -O2 and above")
916 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
917 'trace function introduces __locals__ unexpectedly')
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700918 def test_buggy_dir(self):
919 class M(type):
920 def __dir__(cls):
921 return ['__class__', '__name__', 'missing', 'here']
922 class C(metaclass=M):
923 here = 'present!'
924 output = StringIO()
925 helper = pydoc.Helper(output=output)
926 helper(C)
927 expected_text = expected_missingattribute_pattern % __name__
928 result = output.getvalue().strip()
929 if result != expected_text:
930 print_diffs(expected_text, result)
931 self.fail("outputs are not equal, see diff above")
932
Eric Snowaed5b222014-01-04 20:38:11 -0700933
Antoine Pitroua6e81a22011-07-15 22:32:25 +0200934@reap_threads
Georg Brandlb533e262008-05-25 18:19:30 +0000935def test_main():
Antoine Pitroua6e81a22011-07-15 22:32:25 +0200936 try:
937 test.support.run_unittest(PydocDocTest,
Ned Deily92a81a12011-10-06 14:19:03 -0700938 PydocImportTest,
Antoine Pitroua6e81a22011-07-15 22:32:25 +0200939 TestDescriptions,
940 PydocServerTest,
941 PydocUrlHandlerTest,
942 TestHelper,
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700943 PydocWithMetaClasses,
Antoine Pitroua6e81a22011-07-15 22:32:25 +0200944 )
945 finally:
946 reap_children()
Georg Brandlb533e262008-05-25 18:19:30 +0000947
948if __name__ == "__main__":
949 test_main()