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