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 |
Larry Hastings | 13da6a1 | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 9 | import _pickle |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 10 | import pkgutil |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 11 | import re |
| 12 | import string |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 13 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 14 | import time |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 15 | import types |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 16 | import unittest |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 17 | import xml.etree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 18 | import textwrap |
| 19 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 20 | from collections import namedtuple |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 21 | from test.script_helper import assert_python_ok |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 22 | from test.support import ( |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 23 | TESTFN, rmtree, |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 24 | reap_children, reap_threads, captured_output, captured_stdout, |
Stefan Krah | 5de3278 | 2014-01-18 23:18:39 +0100 | [diff] [blame] | 25 | captured_stderr, unlink, requires_docstrings |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 26 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 27 | from test import pydoc_mod |
| 28 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 29 | try: |
| 30 | import threading |
| 31 | except ImportError: |
| 32 | threading = None |
| 33 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 34 | if test.support.HAVE_DOCSTRINGS: |
| 35 | expected_data_docstrings = ( |
| 36 | 'dictionary for instance variables (if defined)', |
| 37 | 'list of weak references to the object (if defined)', |
| 38 | ) * 2 |
| 39 | else: |
| 40 | expected_data_docstrings = ('', '', '', '') |
| 41 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 42 | expected_text_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 43 | NAME |
| 44 | test.pydoc_mod - This is a test module for test_pydoc |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 45 | %s |
| 46 | CLASSES |
| 47 | builtins.object |
| 48 | A |
| 49 | B |
| 50 | \x20\x20\x20\x20 |
| 51 | class A(builtins.object) |
| 52 | | Hello and goodbye |
| 53 | |\x20\x20 |
| 54 | | Methods defined here: |
| 55 | |\x20\x20 |
| 56 | | __init__() |
| 57 | | Wow, I have no function! |
| 58 | |\x20\x20 |
| 59 | | ---------------------------------------------------------------------- |
| 60 | | Data descriptors defined here: |
| 61 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 62 | | __dict__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 63 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 64 | | __weakref__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 65 | \x20\x20\x20\x20 |
| 66 | class B(builtins.object) |
| 67 | | Data descriptors defined here: |
| 68 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 69 | | __dict__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 70 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 71 | | __weakref__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 72 | |\x20\x20 |
| 73 | | ---------------------------------------------------------------------- |
| 74 | | Data and other attributes defined here: |
| 75 | |\x20\x20 |
| 76 | | NO_MEANING = 'eggs' |
| 77 | |
| 78 | FUNCTIONS |
| 79 | doc_func() |
| 80 | This function solves all of the world's problems: |
| 81 | hunger |
| 82 | lack of Python |
| 83 | war |
| 84 | \x20\x20\x20\x20 |
| 85 | nodoc_func() |
| 86 | |
| 87 | DATA |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 88 | __xyz__ = 'X, Y and Z' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 89 | |
| 90 | VERSION |
| 91 | 1.2.3.4 |
| 92 | |
| 93 | AUTHOR |
| 94 | Benjamin Peterson |
| 95 | |
| 96 | CREDITS |
| 97 | Nobody |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 98 | |
| 99 | FILE |
| 100 | %s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 101 | """.strip() |
| 102 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 103 | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
| 104 | for s in expected_data_docstrings) |
| 105 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 106 | expected_html_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 107 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 108 | <tr bgcolor="#7799ee"> |
| 109 | <td valign=bottom> <br> |
| 110 | <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 |
| 111 | ><td align=right valign=bottom |
| 112 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
| 113 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 114 | <p> |
| 115 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 116 | <tr bgcolor="#ee77aa"> |
| 117 | <td colspan=3 valign=bottom> <br> |
| 118 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 119 | \x20\x20\x20\x20 |
| 120 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 121 | <td width="100%%"><dl> |
| 122 | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
| 123 | </font></dt><dd> |
| 124 | <dl> |
| 125 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 126 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
| 127 | </font></dt></dl> |
| 128 | </dd> |
| 129 | </dl> |
| 130 | <p> |
| 131 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 132 | <tr bgcolor="#ffc8d8"> |
| 133 | <td colspan=3 valign=bottom> <br> |
| 134 | <font color="#000000" face="helvetica, arial"><a name="A">class <strong>A</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 135 | \x20\x20\x20\x20 |
| 136 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 137 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 138 | <tr><td> </td> |
| 139 | <td width="100%%">Methods defined here:<br> |
| 140 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 141 | |
| 142 | <hr> |
| 143 | Data descriptors defined here:<br> |
| 144 | <dl><dt><strong>__dict__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 145 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 146 | </dl> |
| 147 | <dl><dt><strong>__weakref__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 148 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 149 | </dl> |
| 150 | </td></tr></table> <p> |
| 151 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 152 | <tr bgcolor="#ffc8d8"> |
| 153 | <td colspan=3 valign=bottom> <br> |
| 154 | <font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 155 | \x20\x20\x20\x20 |
| 156 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 157 | <td width="100%%">Data descriptors defined here:<br> |
| 158 | <dl><dt><strong>__dict__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 159 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 160 | </dl> |
| 161 | <dl><dt><strong>__weakref__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 162 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 163 | </dl> |
| 164 | <hr> |
| 165 | Data and other attributes defined here:<br> |
| 166 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 167 | |
| 168 | </td></tr></table></td></tr></table><p> |
| 169 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 170 | <tr bgcolor="#eeaa77"> |
| 171 | <td colspan=3 valign=bottom> <br> |
| 172 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 173 | \x20\x20\x20\x20 |
| 174 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 175 | <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> |
| 176 | hunger<br> |
| 177 | lack of Python<br> |
| 178 | war</tt></dd></dl> |
| 179 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 180 | </td></tr></table><p> |
| 181 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 182 | <tr bgcolor="#55aa55"> |
| 183 | <td colspan=3 valign=bottom> <br> |
| 184 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 185 | \x20\x20\x20\x20 |
| 186 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 187 | <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] | 188 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 189 | <tr bgcolor="#7799ee"> |
| 190 | <td colspan=3 valign=bottom> <br> |
| 191 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 192 | \x20\x20\x20\x20 |
| 193 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 194 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 195 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 196 | <tr bgcolor="#7799ee"> |
| 197 | <td colspan=3 valign=bottom> <br> |
| 198 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 199 | \x20\x20\x20\x20 |
| 200 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 201 | <td width="100%%">Nobody</td></tr></table> |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 202 | """.strip() # ' <- emacs turd |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 203 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 204 | expected_html_data_docstrings = tuple(s.replace(' ', ' ') |
| 205 | for s in expected_data_docstrings) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 206 | |
| 207 | # output pattern for missing module |
| 208 | missing_pattern = "no Python documentation found for '%s'" |
| 209 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 210 | # output pattern for module with bad imports |
Brett Cannon | 679ecb5 | 2013-07-04 17:51:50 -0400 | [diff] [blame] | 211 | badimport_pattern = "problem in %s - ImportError: No module named %r" |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 212 | |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 213 | expected_dynamicattribute_pattern = """ |
| 214 | Help on class DA in module %s: |
| 215 | |
| 216 | class DA(builtins.object) |
| 217 | | Data descriptors defined here: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 218 | |\x20\x20 |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 219 | | __dict__%s |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 220 | |\x20\x20 |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 221 | | __weakref__%s |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 222 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 223 | | ham |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 224 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 225 | | ---------------------------------------------------------------------- |
| 226 | | Data and other attributes inherited from Meta: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 227 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 228 | | ham = 'spam' |
| 229 | """.strip() |
| 230 | |
| 231 | expected_virtualattribute_pattern1 = """ |
| 232 | Help on class Class in module %s: |
| 233 | |
| 234 | class Class(builtins.object) |
| 235 | | Data and other attributes inherited from Meta: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 236 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 237 | | LIFE = 42 |
| 238 | """.strip() |
| 239 | |
| 240 | expected_virtualattribute_pattern2 = """ |
| 241 | Help on class Class1 in module %s: |
| 242 | |
| 243 | class Class1(builtins.object) |
| 244 | | Data and other attributes inherited from Meta1: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 245 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 246 | | one = 1 |
| 247 | """.strip() |
| 248 | |
| 249 | expected_virtualattribute_pattern3 = """ |
| 250 | Help on class Class2 in module %s: |
| 251 | |
| 252 | class Class2(Class1) |
| 253 | | Method resolution order: |
| 254 | | Class2 |
| 255 | | Class1 |
| 256 | | builtins.object |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 257 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 258 | | Data and other attributes inherited from Meta1: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 259 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 260 | | one = 1 |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 261 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 262 | | ---------------------------------------------------------------------- |
| 263 | | Data and other attributes inherited from Meta3: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 264 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 265 | | three = 3 |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 266 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 267 | | ---------------------------------------------------------------------- |
| 268 | | Data and other attributes inherited from Meta2: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 269 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 270 | | two = 2 |
| 271 | """.strip() |
| 272 | |
| 273 | expected_missingattribute_pattern = """ |
| 274 | Help on class C in module %s: |
| 275 | |
| 276 | class C(builtins.object) |
| 277 | | Data and other attributes defined here: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 278 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 279 | | here = 'present!' |
| 280 | """.strip() |
| 281 | |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 282 | def run_pydoc(module_name, *args, **env): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 283 | """ |
| 284 | Runs pydoc on the specified module. Returns the stripped |
| 285 | output of pydoc. |
| 286 | """ |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 287 | args = args + (module_name,) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 288 | # do not write bytecode files to avoid caching errors |
| 289 | rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env) |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 290 | return out.strip() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 291 | |
| 292 | def get_pydoc_html(module): |
| 293 | "Returns pydoc generated output as html" |
| 294 | doc = pydoc.HTMLDoc() |
| 295 | output = doc.docmodule(module) |
| 296 | loc = doc.getdocloc(pydoc_mod) or "" |
| 297 | if loc: |
| 298 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 299 | return output.strip(), loc |
| 300 | |
| 301 | def get_pydoc_text(module): |
| 302 | "Returns pydoc generated output as text" |
| 303 | doc = pydoc.TextDoc() |
| 304 | loc = doc.getdocloc(pydoc_mod) or "" |
| 305 | if loc: |
| 306 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 307 | |
| 308 | output = doc.docmodule(module) |
| 309 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 310 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 311 | patt = re.compile('\b.') |
| 312 | output = patt.sub('', output) |
| 313 | return output.strip(), loc |
| 314 | |
| 315 | def print_diffs(text1, text2): |
| 316 | "Prints unified diffs for two texts" |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 317 | # XXX now obsolete, use unittest built-in support |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 318 | lines1 = text1.splitlines(keepends=True) |
| 319 | lines2 = text2.splitlines(keepends=True) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 320 | diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected', |
| 321 | tofile='got') |
| 322 | print('\n' + ''.join(diffs)) |
| 323 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 324 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 325 | # Bit of hack, but good enough for test purposes |
| 326 | header, _, _ = text.partition("</head>") |
| 327 | _, _, title = header.partition("<title>") |
| 328 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 329 | return title |
| 330 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 332 | class PydocBaseTest(unittest.TestCase): |
| 333 | |
| 334 | def _restricted_walk_packages(self, walk_packages, path=None): |
| 335 | """ |
| 336 | A version of pkgutil.walk_packages() that will restrict itself to |
| 337 | a given path. |
| 338 | """ |
| 339 | default_path = path or [os.path.dirname(__file__)] |
| 340 | def wrapper(path=None, prefix='', onerror=None): |
| 341 | return walk_packages(path or default_path, prefix, onerror) |
| 342 | return wrapper |
| 343 | |
| 344 | @contextlib.contextmanager |
| 345 | def restrict_walk_packages(self, path=None): |
| 346 | walk_packages = pkgutil.walk_packages |
| 347 | pkgutil.walk_packages = self._restricted_walk_packages(walk_packages, |
| 348 | path) |
| 349 | try: |
| 350 | yield |
| 351 | finally: |
| 352 | pkgutil.walk_packages = walk_packages |
| 353 | |
| 354 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 355 | class PydocDocTest(unittest.TestCase): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 356 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 357 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 358 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 359 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 360 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 361 | def test_html_doc(self): |
| 362 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 363 | mod_file = inspect.getabsfile(pydoc_mod) |
Benjamin Peterson | c5e9464 | 2008-06-14 23:04:46 +0000 | [diff] [blame] | 364 | if sys.platform == 'win32': |
| 365 | import nturl2path |
| 366 | mod_url = nturl2path.pathname2url(mod_file) |
| 367 | else: |
| 368 | mod_url = mod_file |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 369 | expected_html = expected_html_pattern % ( |
| 370 | (mod_url, mod_file, doc_loc) + |
| 371 | expected_html_data_docstrings) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 372 | if result != expected_html: |
| 373 | print_diffs(expected_html, result) |
| 374 | self.fail("outputs are not equal, see diff above") |
| 375 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 376 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 377 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 378 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 379 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 380 | def test_text_doc(self): |
| 381 | result, doc_loc = get_pydoc_text(pydoc_mod) |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 382 | expected_text = expected_text_pattern % ( |
| 383 | (doc_loc,) + |
| 384 | expected_text_data_docstrings + |
| 385 | (inspect.getabsfile(pydoc_mod),)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 386 | if result != expected_text: |
| 387 | print_diffs(expected_text, result) |
| 388 | self.fail("outputs are not equal, see diff above") |
| 389 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 390 | def test_issue8225(self): |
| 391 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 392 | result, doc_loc = get_pydoc_text(xml.etree) |
| 393 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 394 | |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 395 | def test_non_str_name(self): |
| 396 | # issue14638 |
| 397 | # Treat illegal (non-str) name like no name |
| 398 | class A: |
| 399 | __name__ = 42 |
| 400 | class B: |
| 401 | pass |
| 402 | adoc = pydoc.render_doc(A()) |
| 403 | bdoc = pydoc.render_doc(B()) |
| 404 | self.assertEqual(adoc.replace("A", "B"), bdoc) |
| 405 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 406 | def test_not_here(self): |
| 407 | missing_module = "test.i_am_not_here" |
| 408 | result = str(run_pydoc(missing_module), 'ascii') |
| 409 | expected = missing_pattern % missing_module |
| 410 | self.assertEqual(expected, result, |
| 411 | "documentation for missing module found") |
| 412 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 413 | def test_input_strip(self): |
| 414 | missing_module = " test.i_am_not_here " |
| 415 | result = str(run_pydoc(missing_module), 'ascii') |
| 416 | expected = missing_pattern % missing_module.strip() |
| 417 | self.assertEqual(expected, result) |
| 418 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 419 | def test_stripid(self): |
| 420 | # test with strings, other implementations might have different repr() |
| 421 | stripid = pydoc.stripid |
| 422 | # strip the id |
| 423 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 424 | '<function stripid>') |
| 425 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 426 | '<function stripid>') |
| 427 | # nothing to strip, return the same text |
| 428 | self.assertEqual(stripid('42'), '42') |
| 429 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 430 | "<type 'exceptions.Exception'>") |
| 431 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 432 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 433 | 'Docstrings are omitted with -O2 and above') |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 434 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 435 | 'trace function introduces __locals__ unexpectedly') |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 436 | def test_help_output_redirect(self): |
| 437 | # issue 940286, if output is set in Helper, then all output from |
| 438 | # Helper.help should be redirected |
| 439 | old_pattern = expected_text_pattern |
| 440 | getpager_old = pydoc.getpager |
| 441 | getpager_new = lambda: (lambda x: x) |
| 442 | self.maxDiff = None |
| 443 | |
| 444 | buf = StringIO() |
| 445 | helper = pydoc.Helper(output=buf) |
| 446 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 447 | module = "test.pydoc_mod" |
| 448 | help_header = """ |
| 449 | Help on module test.pydoc_mod in test: |
| 450 | |
| 451 | """.lstrip() |
| 452 | help_header = textwrap.dedent(help_header) |
| 453 | expected_help_pattern = help_header + expected_text_pattern |
| 454 | |
| 455 | pydoc.getpager = getpager_new |
| 456 | try: |
| 457 | with captured_output('stdout') as output, \ |
| 458 | captured_output('stderr') as err: |
| 459 | helper.help(module) |
| 460 | result = buf.getvalue().strip() |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 461 | expected_text = expected_help_pattern % ( |
| 462 | (doc_loc,) + |
| 463 | expected_text_data_docstrings + |
| 464 | (inspect.getabsfile(pydoc_mod),)) |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 465 | self.assertEqual('', output.getvalue()) |
| 466 | self.assertEqual('', err.getvalue()) |
| 467 | self.assertEqual(expected_text, result) |
| 468 | finally: |
| 469 | pydoc.getpager = getpager_old |
| 470 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 471 | def test_namedtuple_public_underscore(self): |
| 472 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 473 | with captured_stdout() as help_io: |
Terry Jan Reedy | 5c81164 | 2013-11-04 21:43:26 -0500 | [diff] [blame] | 474 | pydoc.help(NT) |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 475 | helptext = help_io.getvalue() |
| 476 | self.assertIn('_1', helptext) |
| 477 | self.assertIn('_replace', helptext) |
| 478 | self.assertIn('_asdict', helptext) |
| 479 | |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 480 | def test_synopsis(self): |
| 481 | self.addCleanup(unlink, TESTFN) |
| 482 | for encoding in ('ISO-8859-1', 'UTF-8'): |
| 483 | with open(TESTFN, 'w', encoding=encoding) as script: |
| 484 | if encoding != 'UTF-8': |
| 485 | print('#coding: {}'.format(encoding), file=script) |
| 486 | print('"""line 1: h\xe9', file=script) |
| 487 | print('line 2: hi"""', file=script) |
| 488 | synopsis = pydoc.synopsis(TESTFN, {}) |
| 489 | self.assertEqual(synopsis, 'line 1: h\xe9') |
| 490 | |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 491 | def test_synopsis_sourceless(self): |
| 492 | expected = os.__doc__.splitlines()[0] |
| 493 | filename = os.__cached__ |
| 494 | synopsis = pydoc.synopsis(filename) |
| 495 | |
| 496 | self.assertEqual(synopsis, expected) |
| 497 | |
R David Murray | 455f296 | 2013-03-19 00:00:33 -0400 | [diff] [blame] | 498 | def test_splitdoc_with_description(self): |
| 499 | example_string = "I Am A Doc\n\n\nHere is my description" |
| 500 | self.assertEqual(pydoc.splitdoc(example_string), |
| 501 | ('I Am A Doc', '\nHere is my description')) |
| 502 | |
| 503 | def test_is_object_or_method(self): |
| 504 | doc = pydoc.Doc() |
| 505 | # Bound Method |
| 506 | self.assertTrue(pydoc._is_some_method(doc.fail)) |
| 507 | # Method Descriptor |
| 508 | self.assertTrue(pydoc._is_some_method(int.__add__)) |
| 509 | # String |
| 510 | self.assertFalse(pydoc._is_some_method("I am not a method")) |
| 511 | |
| 512 | def test_is_package_when_not_package(self): |
| 513 | with test.support.temp_cwd() as test_dir: |
| 514 | self.assertFalse(pydoc.ispackage(test_dir)) |
| 515 | |
| 516 | def test_is_package_when_is_package(self): |
| 517 | with test.support.temp_cwd() as test_dir: |
| 518 | init_path = os.path.join(test_dir, '__init__.py') |
| 519 | open(init_path, 'w').close() |
| 520 | self.assertTrue(pydoc.ispackage(test_dir)) |
| 521 | os.remove(init_path) |
| 522 | |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 523 | def test_allmethods(self): |
| 524 | # issue 17476: allmethods was no longer returning unbound methods. |
| 525 | # This test is a bit fragile in the face of changes to object and type, |
| 526 | # but I can't think of a better way to do it without duplicating the |
| 527 | # logic of the function under test. |
| 528 | |
| 529 | class TestClass(object): |
| 530 | def method_returning_true(self): |
| 531 | return True |
| 532 | |
| 533 | # What we expect to get back: everything on object... |
| 534 | expected = dict(vars(object)) |
| 535 | # ...plus our unbound method... |
| 536 | expected['method_returning_true'] = TestClass.method_returning_true |
| 537 | # ...but not the non-methods on object. |
| 538 | del expected['__doc__'] |
| 539 | del expected['__class__'] |
| 540 | # inspect resolves descriptors on type into methods, but vars doesn't, |
| 541 | # so we need to update __subclasshook__. |
| 542 | expected['__subclasshook__'] = TestClass.__subclasshook__ |
| 543 | |
| 544 | methods = pydoc.allmethods(TestClass) |
| 545 | self.assertDictEqual(methods, expected) |
| 546 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 548 | class PydocImportTest(PydocBaseTest): |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 549 | |
| 550 | def setUp(self): |
| 551 | self.test_dir = os.mkdir(TESTFN) |
| 552 | self.addCleanup(rmtree, TESTFN) |
| 553 | |
| 554 | def test_badimport(self): |
| 555 | # This tests the fix for issue 5230, where if pydoc found the module |
| 556 | # but the module had an internal import error pydoc would report no doc |
| 557 | # found. |
| 558 | modname = 'testmod_xyzzy' |
| 559 | testpairs = ( |
| 560 | ('i_am_not_here', 'i_am_not_here'), |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 561 | ('test.i_am_not_here_either', 'test.i_am_not_here_either'), |
| 562 | ('test.i_am_not_here.neither_am_i', 'test.i_am_not_here'), |
| 563 | ('i_am_not_here.{}'.format(modname), 'i_am_not_here'), |
| 564 | ('test.{}'.format(modname), 'test.{}'.format(modname)), |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 565 | ) |
| 566 | |
| 567 | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
| 568 | for importstring, expectedinmsg in testpairs: |
| 569 | with open(sourcefn, 'w') as f: |
| 570 | f.write("import {}\n".format(importstring)) |
| 571 | result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
| 572 | expected = badimport_pattern % (modname, expectedinmsg) |
| 573 | self.assertEqual(expected, result) |
| 574 | |
| 575 | def test_apropos_with_bad_package(self): |
| 576 | # Issue 7425 - pydoc -k failed when bad package on path |
| 577 | pkgdir = os.path.join(TESTFN, "syntaxerr") |
| 578 | os.mkdir(pkgdir) |
| 579 | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
| 580 | with open(badsyntax, 'w') as f: |
| 581 | f.write("invalid python syntax = $1\n") |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 582 | with self.restrict_walk_packages(path=[TESTFN]): |
| 583 | with captured_stdout() as out: |
| 584 | with captured_stderr() as err: |
| 585 | pydoc.apropos('xyzzy') |
| 586 | # No result, no error |
| 587 | self.assertEqual(out.getvalue(), '') |
| 588 | self.assertEqual(err.getvalue(), '') |
| 589 | # The package name is still matched |
| 590 | with captured_stdout() as out: |
| 591 | with captured_stderr() as err: |
| 592 | pydoc.apropos('syntaxerr') |
| 593 | self.assertEqual(out.getvalue().strip(), 'syntaxerr') |
| 594 | self.assertEqual(err.getvalue(), '') |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 595 | |
| 596 | def test_apropos_with_unreadable_dir(self): |
| 597 | # Issue 7367 - pydoc -k failed when unreadable dir on path |
| 598 | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
| 599 | os.mkdir(self.unreadable_dir, 0) |
| 600 | self.addCleanup(os.rmdir, self.unreadable_dir) |
| 601 | # Note, on Windows the directory appears to be still |
| 602 | # readable so this is not really testing the issue there |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 603 | with self.restrict_walk_packages(path=[TESTFN]): |
| 604 | with captured_stdout() as out: |
| 605 | with captured_stderr() as err: |
| 606 | pydoc.apropos('SOMEKEY') |
| 607 | # No result, no error |
| 608 | self.assertEqual(out.getvalue(), '') |
| 609 | self.assertEqual(err.getvalue(), '') |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 610 | |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 611 | def test_modules(self): |
| 612 | # See Helper.listmodules(). |
| 613 | num_header_lines = 2 |
| 614 | num_module_lines_min = 5 # Playing it safe. |
| 615 | num_footer_lines = 3 |
| 616 | expected = num_header_lines + num_module_lines_min + num_footer_lines |
| 617 | |
| 618 | output = StringIO() |
| 619 | helper = pydoc.Helper(output=output) |
| 620 | helper('modules') |
| 621 | result = output.getvalue().strip() |
| 622 | num_lines = len(result.splitlines()) |
| 623 | |
| 624 | self.assertGreaterEqual(num_lines, expected) |
| 625 | |
| 626 | def test_modules_search(self): |
| 627 | # See Helper.listmodules(). |
| 628 | expected = 'pydoc - ' |
| 629 | |
| 630 | output = StringIO() |
| 631 | helper = pydoc.Helper(output=output) |
| 632 | with captured_stdout() as help_io: |
| 633 | helper('modules pydoc') |
| 634 | result = help_io.getvalue() |
| 635 | |
| 636 | self.assertIn(expected, result) |
| 637 | |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 638 | @unittest.skip('some buildbots are not cooperating (#20123)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 639 | def test_modules_search_builtin(self): |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 640 | expected = 'gc - ' |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 641 | |
| 642 | output = StringIO() |
| 643 | helper = pydoc.Helper(output=output) |
| 644 | with captured_stdout() as help_io: |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 645 | helper('modules garbage') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 646 | result = help_io.getvalue() |
| 647 | |
| 648 | self.assertTrue(result.startswith(expected)) |
| 649 | |
| 650 | def test_importfile(self): |
| 651 | loaded_pydoc = pydoc.importfile(pydoc.__file__) |
| 652 | |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 653 | self.assertIsNot(loaded_pydoc, pydoc) |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 654 | self.assertEqual(loaded_pydoc.__name__, 'pydoc') |
| 655 | self.assertEqual(loaded_pydoc.__file__, pydoc.__file__) |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 656 | self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__) |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 657 | |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 658 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 659 | class TestDescriptions(unittest.TestCase): |
| 660 | |
| 661 | def test_module(self): |
| 662 | # Check that pydocfodder module can be described |
| 663 | from test import pydocfodder |
| 664 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 665 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 666 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 667 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 668 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 669 | c = C() |
| 670 | |
| 671 | self.assertEqual(pydoc.describe(C), 'class C') |
| 672 | self.assertEqual(pydoc.describe(c), 'C') |
| 673 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 674 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 675 | |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 676 | def test_builtin(self): |
| 677 | for name in ('str', 'str.translate', 'builtins.str', |
| 678 | 'builtins.str.translate'): |
| 679 | # test low-level function |
| 680 | self.assertIsNotNone(pydoc.locate(name)) |
| 681 | # test high-level function |
| 682 | try: |
| 683 | pydoc.render_doc(name) |
| 684 | except ImportError: |
| 685 | self.fail('finding the doc of {!r} failed'.format(o)) |
| 686 | |
| 687 | for name in ('notbuiltins', 'strrr', 'strr.translate', |
| 688 | 'str.trrrranslate', 'builtins.strrr', |
| 689 | 'builtins.str.trrranslate'): |
| 690 | self.assertIsNone(pydoc.locate(name)) |
| 691 | self.assertRaises(ImportError, pydoc.render_doc, name) |
| 692 | |
Larry Hastings | 13da6a1 | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 693 | @staticmethod |
| 694 | def _get_summary_line(o): |
| 695 | text = pydoc.plain(pydoc.render_doc(o)) |
| 696 | lines = text.split('\n') |
| 697 | assert len(lines) >= 2 |
| 698 | return lines[2] |
| 699 | |
| 700 | # these should include "self" |
| 701 | def test_unbound_python_method(self): |
| 702 | self.assertEqual(self._get_summary_line(textwrap.TextWrapper.wrap), |
| 703 | "wrap(self, text)") |
| 704 | |
Stefan Krah | 5de3278 | 2014-01-18 23:18:39 +0100 | [diff] [blame] | 705 | @requires_docstrings |
Larry Hastings | 13da6a1 | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 706 | def test_unbound_builtin_method(self): |
| 707 | self.assertEqual(self._get_summary_line(_pickle.Pickler.dump), |
| 708 | "dump(self, obj, /)") |
| 709 | |
| 710 | # these no longer include "self" |
| 711 | def test_bound_python_method(self): |
| 712 | t = textwrap.TextWrapper() |
| 713 | self.assertEqual(self._get_summary_line(t.wrap), |
| 714 | "wrap(text) method of textwrap.TextWrapper instance") |
| 715 | |
| 716 | @requires_docstrings |
| 717 | def test_bound_builtin_method(self): |
| 718 | s = StringIO() |
| 719 | p = _pickle.Pickler(s) |
| 720 | self.assertEqual(self._get_summary_line(p.dump), |
| 721 | "dump(obj, /) method of _pickle.Pickler instance") |
| 722 | |
| 723 | # this should *never* include self! |
| 724 | @requires_docstrings |
| 725 | def test_module_level_callable(self): |
| 726 | self.assertEqual(self._get_summary_line(os.stat), |
| 727 | "stat(path, *, dir_fd=None, follow_symlinks=True)") |
Larry Hastings | 1abd708 | 2014-01-16 14:15:03 -0800 | [diff] [blame] | 728 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 729 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 730 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 731 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 732 | """Tests for pydoc._start_server""" |
| 733 | |
| 734 | def test_server(self): |
| 735 | |
| 736 | # Minimal test that starts the server, then stops it. |
| 737 | def my_url_handler(url, content_type): |
| 738 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 739 | return text |
| 740 | |
| 741 | serverthread = pydoc._start_server(my_url_handler, port=0) |
| 742 | starttime = time.time() |
| 743 | timeout = 1 #seconds |
| 744 | |
| 745 | while serverthread.serving: |
| 746 | time.sleep(.01) |
| 747 | if serverthread.serving and time.time() - starttime > timeout: |
| 748 | serverthread.stop() |
| 749 | break |
| 750 | |
| 751 | self.assertEqual(serverthread.error, None) |
| 752 | |
| 753 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 754 | class PydocUrlHandlerTest(PydocBaseTest): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 755 | """Tests for pydoc._url_handler""" |
| 756 | |
| 757 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 758 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 759 | self.assertRaises(TypeError, f, 'A', '') |
| 760 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 761 | |
| 762 | def test_url_requests(self): |
| 763 | # Test for the correct title in the html pages returned. |
| 764 | # This tests the different parts of the URL handler without |
| 765 | # getting too picky about the exact html. |
| 766 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 767 | ("", "Pydoc: Index of Modules"), |
| 768 | ("get?key=", "Pydoc: Index of Modules"), |
| 769 | ("index", "Pydoc: Index of Modules"), |
| 770 | ("topics", "Pydoc: Topics"), |
| 771 | ("keywords", "Pydoc: Keywords"), |
| 772 | ("pydoc", "Pydoc: module pydoc"), |
| 773 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 774 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 775 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 776 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 777 | ("foobar", "Pydoc: Error - foobar"), |
| 778 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 779 | ] |
| 780 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 781 | with self.restrict_walk_packages(): |
| 782 | for url, title in requests: |
| 783 | text = pydoc._url_handler(url, "text/html") |
| 784 | result = get_html_title(text) |
| 785 | self.assertEqual(result, title, text) |
| 786 | |
| 787 | path = string.__file__ |
| 788 | title = "Pydoc: getfile " + path |
| 789 | url = "getfile?key=" + path |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 790 | text = pydoc._url_handler(url, "text/html") |
| 791 | result = get_html_title(text) |
| 792 | self.assertEqual(result, title) |
| 793 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 794 | |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 795 | class TestHelper(unittest.TestCase): |
| 796 | def test_keywords(self): |
| 797 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 798 | sorted(keyword.kwlist)) |
| 799 | |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 800 | class PydocWithMetaClasses(unittest.TestCase): |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 801 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 802 | "Docstrings are omitted with -O2 and above") |
| 803 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 804 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 805 | def test_DynamicClassAttribute(self): |
| 806 | class Meta(type): |
| 807 | def __getattr__(self, name): |
| 808 | if name == 'ham': |
| 809 | return 'spam' |
| 810 | return super().__getattr__(name) |
| 811 | class DA(metaclass=Meta): |
| 812 | @types.DynamicClassAttribute |
| 813 | def ham(self): |
| 814 | return 'eggs' |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 815 | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
| 816 | for s in expected_data_docstrings) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 817 | output = StringIO() |
| 818 | helper = pydoc.Helper(output=output) |
| 819 | helper(DA) |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 820 | expected_text = expected_dynamicattribute_pattern % ( |
| 821 | (__name__,) + expected_text_data_docstrings[:2]) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 822 | result = output.getvalue().strip() |
| 823 | if result != expected_text: |
| 824 | print_diffs(expected_text, result) |
| 825 | self.fail("outputs are not equal, see diff above") |
| 826 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 827 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 828 | "Docstrings are omitted with -O2 and above") |
| 829 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 830 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 831 | def test_virtualClassAttributeWithOneMeta(self): |
| 832 | class Meta(type): |
| 833 | def __dir__(cls): |
| 834 | return ['__class__', '__module__', '__name__', 'LIFE'] |
| 835 | def __getattr__(self, name): |
| 836 | if name =='LIFE': |
| 837 | return 42 |
| 838 | return super().__getattr(name) |
| 839 | class Class(metaclass=Meta): |
| 840 | pass |
| 841 | output = StringIO() |
| 842 | helper = pydoc.Helper(output=output) |
| 843 | helper(Class) |
| 844 | expected_text = expected_virtualattribute_pattern1 % __name__ |
| 845 | result = output.getvalue().strip() |
| 846 | if result != expected_text: |
| 847 | print_diffs(expected_text, result) |
| 848 | self.fail("outputs are not equal, see diff above") |
| 849 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 850 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 851 | "Docstrings are omitted with -O2 and above") |
| 852 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 853 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 854 | def test_virtualClassAttributeWithTwoMeta(self): |
| 855 | class Meta1(type): |
| 856 | def __dir__(cls): |
| 857 | return ['__class__', '__module__', '__name__', 'one'] |
| 858 | def __getattr__(self, name): |
| 859 | if name =='one': |
| 860 | return 1 |
| 861 | return super().__getattr__(name) |
| 862 | class Meta2(type): |
| 863 | def __dir__(cls): |
| 864 | return ['__class__', '__module__', '__name__', 'two'] |
| 865 | def __getattr__(self, name): |
| 866 | if name =='two': |
| 867 | return 2 |
| 868 | return super().__getattr__(name) |
| 869 | class Meta3(Meta1, Meta2): |
| 870 | def __dir__(cls): |
| 871 | return list(sorted(set( |
| 872 | ['__class__', '__module__', '__name__', 'three'] + |
| 873 | Meta1.__dir__(cls) + Meta2.__dir__(cls)))) |
| 874 | def __getattr__(self, name): |
| 875 | if name =='three': |
| 876 | return 3 |
| 877 | return super().__getattr__(name) |
| 878 | class Class1(metaclass=Meta1): |
| 879 | pass |
| 880 | class Class2(Class1, metaclass=Meta3): |
| 881 | pass |
| 882 | fail1 = fail2 = False |
| 883 | output = StringIO() |
| 884 | helper = pydoc.Helper(output=output) |
| 885 | helper(Class1) |
| 886 | expected_text1 = expected_virtualattribute_pattern2 % __name__ |
| 887 | result1 = output.getvalue().strip() |
| 888 | if result1 != expected_text1: |
| 889 | print_diffs(expected_text1, result1) |
| 890 | fail1 = True |
| 891 | output = StringIO() |
| 892 | helper = pydoc.Helper(output=output) |
| 893 | helper(Class2) |
| 894 | expected_text2 = expected_virtualattribute_pattern3 % __name__ |
| 895 | result2 = output.getvalue().strip() |
| 896 | if result2 != expected_text2: |
| 897 | print_diffs(expected_text2, result2) |
| 898 | fail2 = True |
| 899 | if fail1 or fail2: |
| 900 | self.fail("outputs are not equal, see diff above") |
| 901 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 902 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 903 | "Docstrings are omitted with -O2 and above") |
| 904 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 905 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 906 | def test_buggy_dir(self): |
| 907 | class M(type): |
| 908 | def __dir__(cls): |
| 909 | return ['__class__', '__name__', 'missing', 'here'] |
| 910 | class C(metaclass=M): |
| 911 | here = 'present!' |
| 912 | output = StringIO() |
| 913 | helper = pydoc.Helper(output=output) |
| 914 | helper(C) |
| 915 | expected_text = expected_missingattribute_pattern % __name__ |
| 916 | result = output.getvalue().strip() |
| 917 | if result != expected_text: |
| 918 | print_diffs(expected_text, result) |
| 919 | self.fail("outputs are not equal, see diff above") |
| 920 | |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 921 | |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 922 | @reap_threads |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 923 | def test_main(): |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 924 | try: |
| 925 | test.support.run_unittest(PydocDocTest, |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 926 | PydocImportTest, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 927 | TestDescriptions, |
| 928 | PydocServerTest, |
| 929 | PydocUrlHandlerTest, |
| 930 | TestHelper, |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 931 | PydocWithMetaClasses, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 932 | ) |
| 933 | finally: |
| 934 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 935 | |
| 936 | if __name__ == "__main__": |
| 937 | test_main() |