blob: b433850fcdfaac780d171176966b0c1d74e11aa7 [file] [log] [blame]
Benjamin Peterson3a7305e2008-05-22 23:09:26 +00001import os
Éric Araujo9a528302011-07-29 17:34:35 +02002import sys
Georg Brandl8632cc22008-05-18 16:32:48 +00003import difflib
Éric Araujo9a528302011-07-29 17:34:35 +02004import __builtin__
Georg Brandl8632cc22008-05-18 16:32:48 +00005import re
6import pydoc
Georg Brandlb7e419e2008-05-20 08:10:03 +00007import inspect
Ezio Melottibdfa2e62011-04-28 07:59:33 +03008import keyword
Georg Brandl8632cc22008-05-18 16:32:48 +00009import unittest
Brian Curtinaeb2e822010-03-31 03:10:21 +000010import xml.etree
Georg Brandl8632cc22008-05-18 16:32:48 +000011import test.test_support
Raymond Hettinger9aa5a342011-03-25 16:00:13 -070012from collections import namedtuple
Ned Deily1a96f8d2011-10-06 14:17:34 -070013from test.script_helper import assert_python_ok
Antoine Pitrou161df942009-10-30 21:41:22 +000014from test.test_support import (
Ned Deily1a96f8d2011-10-06 14:17:34 -070015 TESTFN, rmtree, reap_children, captured_stdout)
Benjamin Petersonf5c38da2008-05-18 20:48:07 +000016
Georg Brandl8632cc22008-05-18 16:32:48 +000017from test import pydoc_mod
18
19expected_text_pattern = \
20"""
21NAME
22 test.pydoc_mod - This is a test module for test_pydoc
23
24FILE
25 %s
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000026%s
Georg Brandl8632cc22008-05-18 16:32:48 +000027CLASSES
28 __builtin__.object
29 B
30 A
31\x20\x20\x20\x20
32 class A
33 | Hello and goodbye
34 |\x20\x20
35 | Methods defined here:
36 |\x20\x20
37 | __init__()
38 | Wow, I have no function!
39\x20\x20\x20\x20
40 class B(__builtin__.object)
41 | Data descriptors defined here:
42 |\x20\x20
43 | __dict__
44 | dictionary for instance variables (if defined)
45 |\x20\x20
46 | __weakref__
47 | list of weak references to the object (if defined)
48 |\x20\x20
49 | ----------------------------------------------------------------------
50 | Data and other attributes defined here:
51 |\x20\x20
52 | NO_MEANING = 'eggs'
53
54FUNCTIONS
55 doc_func()
56 This function solves all of the world's problems:
57 hunger
58 lack of Python
59 war
60\x20\x20\x20\x20
61 nodoc_func()
62
63DATA
64 __author__ = 'Benjamin Peterson'
65 __credits__ = 'Nobody'
Georg Brandl8632cc22008-05-18 16:32:48 +000066 __version__ = '1.2.3.4'
67
68VERSION
69 1.2.3.4
70
71AUTHOR
72 Benjamin Peterson
73
74CREDITS
75 Nobody
76""".strip()
77
78expected_html_pattern = \
79"""
80<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
81<tr bgcolor="#7799ee">
82<td valign=bottom>&nbsp;<br>
83<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
84><td align=right valign=bottom
Benjamin Peterson3a7305e2008-05-22 23:09:26 +000085><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table>
Georg Brandl8632cc22008-05-18 16:32:48 +000086 <p><tt>This&nbsp;is&nbsp;a&nbsp;test&nbsp;module&nbsp;for&nbsp;test_pydoc</tt></p>
87<p>
88<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
89<tr bgcolor="#ee77aa">
90<td colspan=3 valign=bottom>&nbsp;<br>
91<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
92\x20\x20\x20\x20
93<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
94<td width="100%%"><dl>
95<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
96</font></dt><dd>
97<dl>
98<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a>
99</font></dt></dl>
100</dd>
101<dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a>
102</font></dt></dl>
103 <p>
104<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
105<tr bgcolor="#ffc8d8">
106<td colspan=3 valign=bottom>&nbsp;<br>
107<font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a></font></td></tr>
108\x20\x20\x20\x20
109<tr bgcolor="#ffc8d8"><td rowspan=2><tt>&nbsp;&nbsp;&nbsp;</tt></td>
110<td colspan=2><tt>Hello&nbsp;and&nbsp;goodbye<br>&nbsp;</tt></td></tr>
111<tr><td>&nbsp;</td>
112<td width="100%%">Methods defined here:<br>
113<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>
114
115</td></tr></table> <p>
116<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
117<tr bgcolor="#ffc8d8">
118<td colspan=3 valign=bottom>&nbsp;<br>
119<font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
120\x20\x20\x20\x20
121<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
122<td width="100%%">Data descriptors defined here:<br>
123<dl><dt><strong>__dict__</strong></dt>
124<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
125</dl>
126<dl><dt><strong>__weakref__</strong></dt>
127<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
128</dl>
129<hr>
130Data and other attributes defined here:<br>
131<dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl>
132
133</td></tr></table></td></tr></table><p>
134<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
135<tr bgcolor="#eeaa77">
136<td colspan=3 valign=bottom>&nbsp;<br>
137<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
138\x20\x20\x20\x20
139<tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
140<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>
141hunger<br>
142lack&nbsp;of&nbsp;Python<br>
143war</tt></dd></dl>
144 <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl>
145</td></tr></table><p>
146<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
147<tr bgcolor="#55aa55">
148<td colspan=3 valign=bottom>&nbsp;<br>
149<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
150\x20\x20\x20\x20
151<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
152<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
153<strong>__credits__</strong> = 'Nobody'<br>
Georg Brandl8632cc22008-05-18 16:32:48 +0000154<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
155<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
156<tr bgcolor="#7799ee">
157<td colspan=3 valign=bottom>&nbsp;<br>
158<font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr>
159\x20\x20\x20\x20
160<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
161<td width="100%%">Benjamin&nbsp;Peterson</td></tr></table><p>
162<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
163<tr bgcolor="#7799ee">
164<td colspan=3 valign=bottom>&nbsp;<br>
165<font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr>
166\x20\x20\x20\x20
167<tr><td bgcolor="#7799ee"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
168<td width="100%%">Nobody</td></tr></table>
169""".strip()
170
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000171
Georg Brandl8632cc22008-05-18 16:32:48 +0000172# output pattern for missing module
173missing_pattern = "no Python documentation found for '%s'"
174
R. David Murrayef087da2009-06-23 18:02:46 +0000175# output pattern for module with bad imports
176badimport_pattern = "problem in %s - <type 'exceptions.ImportError'>: No module named %s"
177
Ned Deily1a96f8d2011-10-06 14:17:34 -0700178def run_pydoc(module_name, *args, **env):
Georg Brandl8632cc22008-05-18 16:32:48 +0000179 """
180 Runs pydoc on the specified module. Returns the stripped
181 output of pydoc.
182 """
Ned Deily1a96f8d2011-10-06 14:17:34 -0700183 args = args + (module_name,)
184 # do not write bytecode files to avoid caching errors
185 rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env)
186 return out.strip()
Georg Brandl8632cc22008-05-18 16:32:48 +0000187
188def get_pydoc_html(module):
189 "Returns pydoc generated output as html"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000190 doc = pydoc.HTMLDoc()
191 output = doc.docmodule(module)
192 loc = doc.getdocloc(pydoc_mod) or ""
193 if loc:
194 loc = "<br><a href=\"" + loc + "\">Module Docs</a>"
195 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000196
197def get_pydoc_text(module):
198 "Returns pydoc generated output as text"
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000199 doc = pydoc.TextDoc()
200 loc = doc.getdocloc(pydoc_mod) or ""
201 if loc:
202 loc = "\nMODULE DOCS\n " + loc + "\n"
203
204 output = doc.docmodule(module)
Georg Brandl8632cc22008-05-18 16:32:48 +0000205
206 # cleanup the extra text formatting that pydoc preforms
207 patt = re.compile('\b.')
208 output = patt.sub('', output)
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000209 return output.strip(), loc
Georg Brandl8632cc22008-05-18 16:32:48 +0000210
211def print_diffs(text1, text2):
212 "Prints unified diffs for two texts"
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000213 lines1 = text1.splitlines(True)
214 lines2 = text2.splitlines(True)
215 diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
216 tofile='got')
217 print '\n' + ''.join(diffs)
Georg Brandl8632cc22008-05-18 16:32:48 +0000218
Georg Brandl8632cc22008-05-18 16:32:48 +0000219
Georg Brandl8632cc22008-05-18 16:32:48 +0000220class PyDocDocTest(unittest.TestCase):
221
R. David Murrayf28fd242010-02-23 00:24:49 +0000222 @unittest.skipIf(sys.flags.optimize >= 2,
223 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000224 def test_html_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000225 result, doc_loc = get_pydoc_html(pydoc_mod)
Georg Brandlb7e419e2008-05-20 08:10:03 +0000226 mod_file = inspect.getabsfile(pydoc_mod)
Amaury Forgeot d'Arc8e8de4a2008-06-10 21:37:15 +0000227 if sys.platform == 'win32':
228 import nturl2path
229 mod_url = nturl2path.pathname2url(mod_file)
230 else:
231 mod_url = mod_file
232 expected_html = expected_html_pattern % (mod_url, mod_file, doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000233 if result != expected_html:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000234 print_diffs(expected_html, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000235 self.fail("outputs are not equal, see diff above")
236
R. David Murrayf28fd242010-02-23 00:24:49 +0000237 @unittest.skipIf(sys.flags.optimize >= 2,
238 "Docstrings are omitted with -O2 and above")
Georg Brandl8632cc22008-05-18 16:32:48 +0000239 def test_text_doc(self):
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000240 result, doc_loc = get_pydoc_text(pydoc_mod)
241 expected_text = expected_text_pattern % \
242 (inspect.getabsfile(pydoc_mod), doc_loc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000243 if result != expected_text:
Georg Brandlfb3de1f2008-05-20 08:07:36 +0000244 print_diffs(expected_text, result)
Georg Brandl8632cc22008-05-18 16:32:48 +0000245 self.fail("outputs are not equal, see diff above")
246
Brian Curtinaeb2e822010-03-31 03:10:21 +0000247 def test_issue8225(self):
248 # Test issue8225 to ensure no doc link appears for xml.etree
249 result, doc_loc = get_pydoc_text(xml.etree)
250 self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link")
251
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000252 def test_not_here(self):
253 missing_module = "test.i_am_not_here"
254 result = run_pydoc(missing_module)
255 expected = missing_pattern % missing_module
256 self.assertEqual(expected, result,
257 "documentation for missing module found")
258
R. David Murrayd67ea7d2009-05-27 20:07:21 +0000259 def test_input_strip(self):
260 missing_module = " test.i_am_not_here "
261 result = run_pydoc(missing_module)
262 expected = missing_pattern % missing_module.strip()
263 self.assertEqual(expected, result,
264 "white space was not stripped from module name "
265 "or other error output mismatch")
266
Ezio Melottie511fc72010-02-16 23:26:09 +0000267 def test_stripid(self):
268 # test with strings, other implementations might have different repr()
269 stripid = pydoc.stripid
270 # strip the id
271 self.assertEqual(stripid('<function stripid at 0x88dcee4>'),
272 '<function stripid>')
273 self.assertEqual(stripid('<function stripid at 0x01F65390>'),
274 '<function stripid>')
275 # nothing to strip, return the same text
276 self.assertEqual(stripid('42'), '42')
277 self.assertEqual(stripid("<type 'exceptions.Exception'>"),
278 "<type 'exceptions.Exception'>")
279
Georg Brandl8632cc22008-05-18 16:32:48 +0000280
Ned Deily1a96f8d2011-10-06 14:17:34 -0700281class PydocImportTest(unittest.TestCase):
282
283 def setUp(self):
284 self.test_dir = os.mkdir(TESTFN)
285 self.addCleanup(rmtree, TESTFN)
286
287 def test_badimport(self):
288 # This tests the fix for issue 5230, where if pydoc found the module
289 # but the module had an internal import error pydoc would report no doc
290 # found.
291 modname = 'testmod_xyzzy'
292 testpairs = (
293 ('i_am_not_here', 'i_am_not_here'),
294 ('test.i_am_not_here_either', 'i_am_not_here_either'),
295 ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
296 ('i_am_not_here.{}'.format(modname),
297 'i_am_not_here.{}'.format(modname)),
298 ('test.{}'.format(modname), modname),
299 )
300
301 sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py"
302 for importstring, expectedinmsg in testpairs:
303 with open(sourcefn, 'w') as f:
304 f.write("import {}\n".format(importstring))
305 result = run_pydoc(modname, PYTHONPATH=TESTFN)
306 expected = badimport_pattern % (modname, expectedinmsg)
307 self.assertEqual(expected, result)
308
309 def test_apropos_with_bad_package(self):
310 # Issue 7425 - pydoc -k failed when bad package on path
311 pkgdir = os.path.join(TESTFN, "syntaxerr")
312 os.mkdir(pkgdir)
313 badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py"
314 with open(badsyntax, 'w') as f:
315 f.write("invalid python syntax = $1\n")
316 result = run_pydoc('nothing', '-k', PYTHONPATH=TESTFN)
317 self.assertEqual('', result)
318
319 def test_apropos_with_unreadable_dir(self):
320 # Issue 7367 - pydoc -k failed when unreadable dir on path
321 self.unreadable_dir = os.path.join(TESTFN, "unreadable")
322 os.mkdir(self.unreadable_dir, 0)
323 self.addCleanup(os.rmdir, self.unreadable_dir)
324 # Note, on Windows the directory appears to be still
325 # readable so this is not really testing the issue there
326 result = run_pydoc('nothing', '-k', PYTHONPATH=TESTFN)
327 self.assertEqual('', result)
328
329
Georg Brandl8632cc22008-05-18 16:32:48 +0000330class TestDescriptions(unittest.TestCase):
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000331
Georg Brandl8632cc22008-05-18 16:32:48 +0000332 def test_module(self):
333 # Check that pydocfodder module can be described
334 from test import pydocfodder
335 doc = pydoc.render_doc(pydocfodder)
Ezio Melottiaa980582010-01-23 23:04:36 +0000336 self.assertIn("pydocfodder", doc)
Georg Brandl8632cc22008-05-18 16:32:48 +0000337
338 def test_classic_class(self):
339 class C: "Classic class"
340 c = C()
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000341 self.assertEqual(pydoc.describe(C), 'class C')
342 self.assertEqual(pydoc.describe(c), 'instance of C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000343 expected = 'instance of C in module %s' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000344 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000345
346 def test_class(self):
347 class C(object): "New-style class"
348 c = C()
349
Benjamin Petersonf5c38da2008-05-18 20:48:07 +0000350 self.assertEqual(pydoc.describe(C), 'class C')
351 self.assertEqual(pydoc.describe(c), 'C')
Benjamin Peterson3a7305e2008-05-22 23:09:26 +0000352 expected = 'C in module %s object' % __name__
Ezio Melottiaa980582010-01-23 23:04:36 +0000353 self.assertIn(expected, pydoc.render_doc(c))
Georg Brandl8632cc22008-05-18 16:32:48 +0000354
Raymond Hettinger9aa5a342011-03-25 16:00:13 -0700355 def test_namedtuple_public_underscore(self):
356 NT = namedtuple('NT', ['abc', 'def'], rename=True)
357 with captured_stdout() as help_io:
358 help(NT)
359 helptext = help_io.getvalue()
360 self.assertIn('_1', helptext)
361 self.assertIn('_replace', helptext)
362 self.assertIn('_asdict', helptext)
363
Georg Brandl8632cc22008-05-18 16:32:48 +0000364
Ezio Melottibdfa2e62011-04-28 07:59:33 +0300365class TestHelper(unittest.TestCase):
366 def test_keywords(self):
367 self.assertEqual(sorted(pydoc.Helper.keywords),
368 sorted(keyword.kwlist))
369
Éric Araujo9a528302011-07-29 17:34:35 +0200370 def test_builtin(self):
371 for name in ('str', 'str.translate', '__builtin__.str',
372 '__builtin__.str.translate'):
373 # test low-level function
374 self.assertIsNotNone(pydoc.locate(name))
375 # test high-level function
376 try:
377 pydoc.render_doc(name)
378 except ImportError:
379 self.fail('finding the doc of {!r} failed'.format(o))
380
381 for name in ('not__builtin__', 'strrr', 'strr.translate',
382 'str.trrrranslate', '__builtin__.strrr',
383 '__builtin__.str.trrranslate'):
384 self.assertIsNone(pydoc.locate(name))
385 self.assertRaises(ImportError, pydoc.render_doc, name)
386
Ezio Melottibdfa2e62011-04-28 07:59:33 +0300387
Georg Brandl8632cc22008-05-18 16:32:48 +0000388def test_main():
Ned Deily1a96f8d2011-10-06 14:17:34 -0700389 try:
390 test.test_support.run_unittest(PyDocDocTest,
391 PydocImportTest,
392 TestDescriptions,
393 TestHelper)
394 finally:
395 reap_children()
Georg Brandl8632cc22008-05-18 16:32:48 +0000396
397if __name__ == "__main__":
398 test_main()