Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | import os |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2 | import sys |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 3 | import builtins |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 4 | import difflib |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 5 | import inspect |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 6 | import pydoc |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 7 | import keyword |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 8 | import re |
| 9 | import string |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 10 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 11 | import time |
| 12 | import unittest |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 13 | import xml.etree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 14 | import textwrap |
| 15 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 16 | from collections import namedtuple |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 17 | from test.script_helper import assert_python_ok |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 18 | from test.support import ( |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 19 | TESTFN, rmtree, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 20 | reap_children, reap_threads, captured_output, captured_stdout, unlink |
| 21 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 22 | from test import pydoc_mod |
| 23 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 24 | try: |
| 25 | import threading |
| 26 | except ImportError: |
| 27 | threading = None |
| 28 | |
Florent Xicluna | 085a656 | 2010-03-06 14:04:16 +0000 | [diff] [blame] | 29 | # Just in case sys.modules["test"] has the optional attribute __loader__. |
| 30 | if hasattr(pydoc_mod, "__loader__"): |
| 31 | del pydoc_mod.__loader__ |
| 32 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 33 | if test.support.HAVE_DOCSTRINGS: |
| 34 | expected_data_docstrings = ( |
| 35 | 'dictionary for instance variables (if defined)', |
| 36 | 'list of weak references to the object (if defined)', |
| 37 | ) * 2 |
| 38 | else: |
| 39 | expected_data_docstrings = ('', '', '', '') |
| 40 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 41 | expected_text_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 42 | NAME |
| 43 | test.pydoc_mod - This is a test module for test_pydoc |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 44 | %s |
| 45 | CLASSES |
| 46 | builtins.object |
| 47 | A |
| 48 | B |
| 49 | \x20\x20\x20\x20 |
| 50 | class A(builtins.object) |
| 51 | | Hello and goodbye |
| 52 | |\x20\x20 |
| 53 | | Methods defined here: |
| 54 | |\x20\x20 |
| 55 | | __init__() |
| 56 | | Wow, I have no function! |
| 57 | |\x20\x20 |
| 58 | | ---------------------------------------------------------------------- |
| 59 | | Data descriptors defined here: |
| 60 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 61 | | __dict__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 62 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 63 | | __weakref__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 64 | \x20\x20\x20\x20 |
| 65 | class B(builtins.object) |
| 66 | | Data descriptors defined here: |
| 67 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 68 | | __dict__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 69 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 70 | | __weakref__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 71 | |\x20\x20 |
| 72 | | ---------------------------------------------------------------------- |
| 73 | | Data and other attributes defined here: |
| 74 | |\x20\x20 |
| 75 | | NO_MEANING = 'eggs' |
| 76 | |
| 77 | FUNCTIONS |
| 78 | doc_func() |
| 79 | This function solves all of the world's problems: |
| 80 | hunger |
| 81 | lack of Python |
| 82 | war |
| 83 | \x20\x20\x20\x20 |
| 84 | nodoc_func() |
| 85 | |
| 86 | DATA |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 87 | __xyz__ = 'X, Y and Z' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 88 | |
| 89 | VERSION |
| 90 | 1.2.3.4 |
| 91 | |
| 92 | AUTHOR |
| 93 | Benjamin Peterson |
| 94 | |
| 95 | CREDITS |
| 96 | Nobody |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 97 | |
| 98 | FILE |
| 99 | %s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 100 | """.strip() |
| 101 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 102 | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
| 103 | for s in expected_data_docstrings) |
| 104 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 105 | expected_html_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 106 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 107 | <tr bgcolor="#7799ee"> |
| 108 | <td valign=bottom> <br> |
| 109 | <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 |
| 110 | ><td align=right valign=bottom |
| 111 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
| 112 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 113 | <p> |
| 114 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 115 | <tr bgcolor="#ee77aa"> |
| 116 | <td colspan=3 valign=bottom> <br> |
| 117 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 118 | \x20\x20\x20\x20 |
| 119 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 120 | <td width="100%%"><dl> |
| 121 | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
| 122 | </font></dt><dd> |
| 123 | <dl> |
| 124 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 125 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 126 | </font></dt></dl> |
| 127 | </dd> |
| 128 | </dl> |
| 129 | <p> |
| 130 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 131 | <tr bgcolor="#ffc8d8"> |
| 132 | <td colspan=3 valign=bottom> <br> |
| 133 | <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> |
| 134 | \x20\x20\x20\x20 |
| 135 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 136 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 137 | <tr><td> </td> |
| 138 | <td width="100%%">Methods defined here:<br> |
| 139 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 140 | |
| 141 | <hr> |
| 142 | Data descriptors defined here:<br> |
| 143 | <dl><dt><strong>__dict__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 144 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 145 | </dl> |
| 146 | <dl><dt><strong>__weakref__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 147 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 148 | </dl> |
| 149 | </td></tr></table> <p> |
| 150 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 151 | <tr bgcolor="#ffc8d8"> |
| 152 | <td colspan=3 valign=bottom> <br> |
| 153 | <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> |
| 154 | \x20\x20\x20\x20 |
| 155 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 156 | <td width="100%%">Data descriptors defined here:<br> |
| 157 | <dl><dt><strong>__dict__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 158 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 159 | </dl> |
| 160 | <dl><dt><strong>__weakref__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 161 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 162 | </dl> |
| 163 | <hr> |
| 164 | Data and other attributes defined here:<br> |
| 165 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 166 | |
| 167 | </td></tr></table></td></tr></table><p> |
| 168 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 169 | <tr bgcolor="#eeaa77"> |
| 170 | <td colspan=3 valign=bottom> <br> |
| 171 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 172 | \x20\x20\x20\x20 |
| 173 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 174 | <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> |
| 175 | hunger<br> |
| 176 | lack of Python<br> |
| 177 | war</tt></dd></dl> |
| 178 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 179 | </td></tr></table><p> |
| 180 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 181 | <tr bgcolor="#55aa55"> |
| 182 | <td colspan=3 valign=bottom> <br> |
| 183 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 184 | \x20\x20\x20\x20 |
| 185 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 186 | <td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 187 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 188 | <tr bgcolor="#7799ee"> |
| 189 | <td colspan=3 valign=bottom> <br> |
| 190 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 191 | \x20\x20\x20\x20 |
| 192 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 193 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 194 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 195 | <tr bgcolor="#7799ee"> |
| 196 | <td colspan=3 valign=bottom> <br> |
| 197 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 198 | \x20\x20\x20\x20 |
| 199 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 200 | <td width="100%%">Nobody</td></tr></table> |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 201 | """.strip() # ' <- emacs turd |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 202 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 203 | expected_html_data_docstrings = tuple(s.replace(' ', ' ') |
| 204 | for s in expected_data_docstrings) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 205 | |
| 206 | # output pattern for missing module |
| 207 | missing_pattern = "no Python documentation found for '%s'" |
| 208 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 209 | # output pattern for module with bad imports |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 210 | badimport_pattern = "problem in %s - ImportError: No module named %r" |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 212 | def run_pydoc(module_name, *args, **env): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 213 | """ |
| 214 | Runs pydoc on the specified module. Returns the stripped |
| 215 | output of pydoc. |
| 216 | """ |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 217 | args = args + (module_name,) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 218 | # do not write bytecode files to avoid caching errors |
| 219 | rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env) |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 220 | return out.strip() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 221 | |
| 222 | def get_pydoc_html(module): |
| 223 | "Returns pydoc generated output as html" |
| 224 | doc = pydoc.HTMLDoc() |
| 225 | output = doc.docmodule(module) |
| 226 | loc = doc.getdocloc(pydoc_mod) or "" |
| 227 | if loc: |
| 228 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 229 | return output.strip(), loc |
| 230 | |
| 231 | def get_pydoc_text(module): |
| 232 | "Returns pydoc generated output as text" |
| 233 | doc = pydoc.TextDoc() |
| 234 | loc = doc.getdocloc(pydoc_mod) or "" |
| 235 | if loc: |
| 236 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 237 | |
| 238 | output = doc.docmodule(module) |
| 239 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 240 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 241 | patt = re.compile('\b.') |
| 242 | output = patt.sub('', output) |
| 243 | return output.strip(), loc |
| 244 | |
| 245 | def print_diffs(text1, text2): |
| 246 | "Prints unified diffs for two texts" |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 247 | # XXX now obsolete, use unittest built-in support |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 248 | lines1 = text1.splitlines(keepends=True) |
| 249 | lines2 = text2.splitlines(keepends=True) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 250 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 251 | tofile='got') |
| 252 | print('\n' + ''.join(diffs)) |
| 253 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 254 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 255 | # Bit of hack, but good enough for test purposes |
| 256 | header, _, _ = text.partition("</head>") |
| 257 | _, _, title = header.partition("<title>") |
| 258 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 259 | return title |
| 260 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 261 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 262 | class PydocDocTest(unittest.TestCase): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 263 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 264 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 265 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 266 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 267 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 268 | def test_html_doc(self): |
| 269 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 270 | mod_file = inspect.getabsfile(pydoc_mod) |
Benjamin Peterson | c5e9464 | 2008-06-14 23:04:46 +0000 | [diff] [blame] | 271 | if sys.platform == 'win32': |
| 272 | import nturl2path |
| 273 | mod_url = nturl2path.pathname2url(mod_file) |
| 274 | else: |
| 275 | mod_url = mod_file |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 276 | expected_html = expected_html_pattern % ( |
| 277 | (mod_url, mod_file, doc_loc) + |
| 278 | expected_html_data_docstrings) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 279 | if result != expected_html: |
| 280 | print_diffs(expected_html, result) |
| 281 | self.fail("outputs are not equal, see diff above") |
| 282 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 283 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 284 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 285 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 286 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 287 | def test_text_doc(self): |
| 288 | result, doc_loc = get_pydoc_text(pydoc_mod) |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 289 | expected_text = expected_text_pattern % ( |
| 290 | (doc_loc,) + |
| 291 | expected_text_data_docstrings + |
| 292 | (inspect.getabsfile(pydoc_mod),)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 293 | if result != expected_text: |
| 294 | print_diffs(expected_text, result) |
| 295 | self.fail("outputs are not equal, see diff above") |
| 296 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 297 | def test_issue8225(self): |
| 298 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 299 | result, doc_loc = get_pydoc_text(xml.etree) |
| 300 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 301 | |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 302 | def test_non_str_name(self): |
| 303 | # issue14638 |
| 304 | # Treat illegal (non-str) name like no name |
| 305 | class A: |
| 306 | __name__ = 42 |
| 307 | class B: |
| 308 | pass |
| 309 | adoc = pydoc.render_doc(A()) |
| 310 | bdoc = pydoc.render_doc(B()) |
| 311 | self.assertEqual(adoc.replace("A", "B"), bdoc) |
| 312 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 313 | def test_not_here(self): |
| 314 | missing_module = "test.i_am_not_here" |
| 315 | result = str(run_pydoc(missing_module), 'ascii') |
| 316 | expected = missing_pattern % missing_module |
| 317 | self.assertEqual(expected, result, |
| 318 | "documentation for missing module found") |
| 319 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 320 | def test_input_strip(self): |
| 321 | missing_module = " test.i_am_not_here " |
| 322 | result = str(run_pydoc(missing_module), 'ascii') |
| 323 | expected = missing_pattern % missing_module.strip() |
| 324 | self.assertEqual(expected, result) |
| 325 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 326 | def test_stripid(self): |
| 327 | # test with strings, other implementations might have different repr() |
| 328 | stripid = pydoc.stripid |
| 329 | # strip the id |
| 330 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 331 | '<function stripid>') |
| 332 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 333 | '<function stripid>') |
| 334 | # nothing to strip, return the same text |
| 335 | self.assertEqual(stripid('42'), '42') |
| 336 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 337 | "<type 'exceptions.Exception'>") |
| 338 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 339 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 340 | 'Docstrings are omitted with -O2 and above') |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 341 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 342 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 343 | def test_help_output_redirect(self): |
| 344 | # issue 940286, if output is set in Helper, then all output from |
| 345 | # Helper.help should be redirected |
| 346 | old_pattern = expected_text_pattern |
| 347 | getpager_old = pydoc.getpager |
| 348 | getpager_new = lambda: (lambda x: x) |
| 349 | self.maxDiff = None |
| 350 | |
| 351 | buf = StringIO() |
| 352 | helper = pydoc.Helper(output=buf) |
| 353 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 354 | module = "test.pydoc_mod" |
| 355 | help_header = """ |
| 356 | Help on module test.pydoc_mod in test: |
| 357 | |
| 358 | """.lstrip() |
| 359 | help_header = textwrap.dedent(help_header) |
| 360 | expected_help_pattern = help_header + expected_text_pattern |
| 361 | |
| 362 | pydoc.getpager = getpager_new |
| 363 | try: |
| 364 | with captured_output('stdout') as output, \ |
| 365 | captured_output('stderr') as err: |
| 366 | helper.help(module) |
| 367 | result = buf.getvalue().strip() |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 368 | expected_text = expected_help_pattern % ( |
| 369 | (doc_loc,) + |
| 370 | expected_text_data_docstrings + |
| 371 | (inspect.getabsfile(pydoc_mod),)) |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 372 | self.assertEqual('', output.getvalue()) |
| 373 | self.assertEqual('', err.getvalue()) |
| 374 | self.assertEqual(expected_text, result) |
| 375 | finally: |
| 376 | pydoc.getpager = getpager_old |
| 377 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 378 | def test_namedtuple_public_underscore(self): |
| 379 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 380 | with captured_stdout() as help_io: |
| 381 | help(NT) |
| 382 | helptext = help_io.getvalue() |
| 383 | self.assertIn('_1', helptext) |
| 384 | self.assertIn('_replace', helptext) |
| 385 | self.assertIn('_asdict', helptext) |
| 386 | |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 387 | def test_synopsis(self): |
| 388 | self.addCleanup(unlink, TESTFN) |
| 389 | for encoding in ('ISO-8859-1', 'UTF-8'): |
| 390 | with open(TESTFN, 'w', encoding=encoding) as script: |
| 391 | if encoding != 'UTF-8': |
| 392 | print('#coding: {}'.format(encoding), file=script) |
| 393 | print('"""line 1: h\xe9', file=script) |
| 394 | print('line 2: hi"""', file=script) |
| 395 | synopsis = pydoc.synopsis(TESTFN, {}) |
| 396 | self.assertEqual(synopsis, 'line 1: h\xe9') |
| 397 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 398 | |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 399 | class PydocImportTest(unittest.TestCase): |
| 400 | |
| 401 | def setUp(self): |
| 402 | self.test_dir = os.mkdir(TESTFN) |
| 403 | self.addCleanup(rmtree, TESTFN) |
| 404 | |
| 405 | def test_badimport(self): |
| 406 | # This tests the fix for issue 5230, where if pydoc found the module |
| 407 | # but the module had an internal import error pydoc would report no doc |
| 408 | # found. |
| 409 | modname = 'testmod_xyzzy' |
| 410 | testpairs = ( |
| 411 | ('i_am_not_here', 'i_am_not_here'), |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 412 | ('test.i_am_not_here_either', 'test.i_am_not_here_either'), |
| 413 | ('test.i_am_not_here.neither_am_i', 'test.i_am_not_here'), |
| 414 | ('i_am_not_here.{}'.format(modname), 'i_am_not_here'), |
| 415 | ('test.{}'.format(modname), 'test.{}'.format(modname)), |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 416 | ) |
| 417 | |
| 418 | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
| 419 | for importstring, expectedinmsg in testpairs: |
| 420 | with open(sourcefn, 'w') as f: |
| 421 | f.write("import {}\n".format(importstring)) |
| 422 | result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
| 423 | expected = badimport_pattern % (modname, expectedinmsg) |
| 424 | self.assertEqual(expected, result) |
| 425 | |
| 426 | def test_apropos_with_bad_package(self): |
| 427 | # Issue 7425 - pydoc -k failed when bad package on path |
| 428 | pkgdir = os.path.join(TESTFN, "syntaxerr") |
| 429 | os.mkdir(pkgdir) |
| 430 | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
| 431 | with open(badsyntax, 'w') as f: |
| 432 | f.write("invalid python syntax = $1\n") |
Ned Deily | 3429491 | 2012-02-03 23:14:37 +0100 | [diff] [blame] | 433 | result = run_pydoc('zqwykjv', '-k', PYTHONPATH=TESTFN) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 434 | self.assertEqual(b'', result) |
| 435 | |
| 436 | def test_apropos_with_unreadable_dir(self): |
| 437 | # Issue 7367 - pydoc -k failed when unreadable dir on path |
| 438 | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
| 439 | os.mkdir(self.unreadable_dir, 0) |
| 440 | self.addCleanup(os.rmdir, self.unreadable_dir) |
| 441 | # Note, on Windows the directory appears to be still |
| 442 | # readable so this is not really testing the issue there |
Ned Deily | 3429491 | 2012-02-03 23:14:37 +0100 | [diff] [blame] | 443 | result = run_pydoc('zqwykjv', '-k', PYTHONPATH=TESTFN) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 444 | self.assertEqual(b'', result) |
| 445 | |
| 446 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 447 | class TestDescriptions(unittest.TestCase): |
| 448 | |
| 449 | def test_module(self): |
| 450 | # Check that pydocfodder module can be described |
| 451 | from test import pydocfodder |
| 452 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 453 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 454 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 455 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 456 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 457 | c = C() |
| 458 | |
| 459 | self.assertEqual(pydoc.describe(C), 'class C') |
| 460 | self.assertEqual(pydoc.describe(c), 'C') |
| 461 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 462 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 463 | |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 464 | def test_builtin(self): |
| 465 | for name in ('str', 'str.translate', 'builtins.str', |
| 466 | 'builtins.str.translate'): |
| 467 | # test low-level function |
| 468 | self.assertIsNotNone(pydoc.locate(name)) |
| 469 | # test high-level function |
| 470 | try: |
| 471 | pydoc.render_doc(name) |
| 472 | except ImportError: |
| 473 | self.fail('finding the doc of {!r} failed'.format(o)) |
| 474 | |
| 475 | for name in ('notbuiltins', 'strrr', 'strr.translate', |
| 476 | 'str.trrrranslate', 'builtins.strrr', |
| 477 | 'builtins.str.trrranslate'): |
| 478 | self.assertIsNone(pydoc.locate(name)) |
| 479 | self.assertRaises(ImportError, pydoc.render_doc, name) |
| 480 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 481 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 482 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 483 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 484 | """Tests for pydoc._start_server""" |
| 485 | |
| 486 | def test_server(self): |
| 487 | |
| 488 | # Minimal test that starts the server, then stops it. |
| 489 | def my_url_handler(url, content_type): |
| 490 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 491 | return text |
| 492 | |
| 493 | serverthread = pydoc._start_server(my_url_handler, port=0) |
| 494 | starttime = time.time() |
| 495 | timeout = 1 #seconds |
| 496 | |
| 497 | while serverthread.serving: |
| 498 | time.sleep(.01) |
| 499 | if serverthread.serving and time.time() - starttime > timeout: |
| 500 | serverthread.stop() |
| 501 | break |
| 502 | |
| 503 | self.assertEqual(serverthread.error, None) |
| 504 | |
| 505 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 506 | class PydocUrlHandlerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 507 | """Tests for pydoc._url_handler""" |
| 508 | |
| 509 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 510 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 511 | self.assertRaises(TypeError, f, 'A', '') |
| 512 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 513 | |
| 514 | def test_url_requests(self): |
| 515 | # Test for the correct title in the html pages returned. |
| 516 | # This tests the different parts of the URL handler without |
| 517 | # getting too picky about the exact html. |
| 518 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 519 | ("", "Pydoc: Index of Modules"), |
| 520 | ("get?key=", "Pydoc: Index of Modules"), |
| 521 | ("index", "Pydoc: Index of Modules"), |
| 522 | ("topics", "Pydoc: Topics"), |
| 523 | ("keywords", "Pydoc: Keywords"), |
| 524 | ("pydoc", "Pydoc: module pydoc"), |
| 525 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 526 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 527 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 528 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 529 | ("foobar", "Pydoc: Error - foobar"), |
| 530 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 531 | ] |
| 532 | |
| 533 | for url, title in requests: |
| 534 | text = pydoc._url_handler(url, "text/html") |
| 535 | result = get_html_title(text) |
| 536 | self.assertEqual(result, title) |
| 537 | |
| 538 | path = string.__file__ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 539 | title = "Pydoc: getfile " + path |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 540 | url = "getfile?key=" + path |
| 541 | text = pydoc._url_handler(url, "text/html") |
| 542 | result = get_html_title(text) |
| 543 | self.assertEqual(result, title) |
| 544 | |
| 545 | |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 546 | class TestHelper(unittest.TestCase): |
| 547 | def test_keywords(self): |
| 548 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 549 | sorted(keyword.kwlist)) |
| 550 | |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 551 | @reap_threads |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 552 | def test_main(): |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 553 | try: |
| 554 | test.support.run_unittest(PydocDocTest, |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 555 | PydocImportTest, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 556 | TestDescriptions, |
| 557 | PydocServerTest, |
| 558 | PydocUrlHandlerTest, |
| 559 | TestHelper, |
| 560 | ) |
| 561 | finally: |
| 562 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 563 | |
| 564 | if __name__ == "__main__": |
| 565 | test_main() |