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