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