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 |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 3 | import contextlib |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 4 | import importlib.util |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 5 | import inspect |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 6 | import pydoc |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 7 | import py_compile |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 8 | import keyword |
Larry Hastings | 24a882b | 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 |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 12 | import stat |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 13 | import string |
Nick Coghlan | 82a9481 | 2018-04-15 21:52:57 +1000 | [diff] [blame] | 14 | import tempfile |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 15 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 16 | import time |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 17 | import types |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 18 | import typing |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 19 | import unittest |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 20 | import urllib.parse |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 21 | import xml.etree |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 22 | import xml.etree.ElementTree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 23 | import textwrap |
| 24 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 25 | from collections import namedtuple |
Hai Shi | bb0424b | 2020-08-04 00:47:42 +0800 | [diff] [blame] | 26 | from test.support import os_helper |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 27 | from test.support.script_helper import assert_python_ok |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 28 | from test.support import threading_helper |
Hai Shi | bb0424b | 2020-08-04 00:47:42 +0800 | [diff] [blame] | 29 | from test.support import (reap_children, captured_output, captured_stdout, |
| 30 | captured_stderr, requires_docstrings) |
| 31 | from test.support.os_helper import (TESTFN, rmtree, unlink) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 32 | from test import pydoc_mod |
| 33 | |
Victor Stinner | 62a68f2 | 2011-05-20 02:29:13 +0200 | [diff] [blame] | 34 | |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 35 | class nonascii: |
| 36 | 'Це не латиниця' |
| 37 | pass |
| 38 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 39 | if test.support.HAVE_DOCSTRINGS: |
| 40 | expected_data_docstrings = ( |
| 41 | 'dictionary for instance variables (if defined)', |
| 42 | 'list of weak references to the object (if defined)', |
| 43 | ) * 2 |
| 44 | else: |
| 45 | expected_data_docstrings = ('', '', '', '') |
| 46 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 47 | expected_text_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 48 | NAME |
| 49 | test.pydoc_mod - This is a test module for test_pydoc |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 50 | %s |
| 51 | CLASSES |
| 52 | builtins.object |
| 53 | A |
| 54 | B |
Benjamin Peterson | ed1160b | 2014-06-07 16:44:00 -0700 | [diff] [blame] | 55 | C |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 56 | \x20\x20\x20\x20 |
| 57 | class A(builtins.object) |
| 58 | | Hello and goodbye |
| 59 | |\x20\x20 |
| 60 | | Methods defined here: |
| 61 | |\x20\x20 |
| 62 | | __init__() |
| 63 | | Wow, I have no function! |
| 64 | |\x20\x20 |
| 65 | | ---------------------------------------------------------------------- |
| 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\x20\x20 |
| 72 | class B(builtins.object) |
| 73 | | Data descriptors defined here: |
| 74 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 75 | | __dict__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 76 | |\x20\x20 |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 77 | | __weakref__%s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 78 | |\x20\x20 |
| 79 | | ---------------------------------------------------------------------- |
| 80 | | Data and other attributes defined here: |
| 81 | |\x20\x20 |
| 82 | | NO_MEANING = 'eggs' |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 83 | |\x20\x20 |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 84 | | __annotations__ = {'NO_MEANING': 'str'} |
Benjamin Peterson | ed1160b | 2014-06-07 16:44:00 -0700 | [diff] [blame] | 85 | \x20\x20\x20\x20 |
| 86 | class C(builtins.object) |
| 87 | | Methods defined here: |
| 88 | |\x20\x20 |
| 89 | | get_answer(self) |
| 90 | | Return say_no() |
| 91 | |\x20\x20 |
| 92 | | is_it_true(self) |
| 93 | | Return self.get_answer() |
| 94 | |\x20\x20 |
| 95 | | say_no(self) |
| 96 | |\x20\x20 |
| 97 | | ---------------------------------------------------------------------- |
| 98 | | Data descriptors defined here: |
| 99 | |\x20\x20 |
| 100 | | __dict__ |
| 101 | | dictionary for instance variables (if defined) |
| 102 | |\x20\x20 |
| 103 | | __weakref__ |
| 104 | | list of weak references to the object (if defined) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 105 | |
| 106 | FUNCTIONS |
| 107 | doc_func() |
| 108 | This function solves all of the world's problems: |
| 109 | hunger |
| 110 | lack of Python |
| 111 | war |
| 112 | \x20\x20\x20\x20 |
| 113 | nodoc_func() |
| 114 | |
| 115 | DATA |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 116 | __xyz__ = 'X, Y and Z' |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 117 | |
| 118 | VERSION |
| 119 | 1.2.3.4 |
| 120 | |
| 121 | AUTHOR |
| 122 | Benjamin Peterson |
| 123 | |
| 124 | CREDITS |
| 125 | Nobody |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 126 | |
| 127 | FILE |
| 128 | %s |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 129 | """.strip() |
| 130 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 131 | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
| 132 | for s in expected_data_docstrings) |
| 133 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 134 | expected_html_pattern = """ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 135 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 136 | <tr bgcolor="#7799ee"> |
| 137 | <td valign=bottom> <br> |
| 138 | <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 |
| 139 | ><td align=right valign=bottom |
| 140 | ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:%s">%s</a>%s</font></td></tr></table> |
| 141 | <p><tt>This is a test module for test_pydoc</tt></p> |
| 142 | <p> |
| 143 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 144 | <tr bgcolor="#ee77aa"> |
| 145 | <td colspan=3 valign=bottom> <br> |
| 146 | <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> |
| 147 | \x20\x20\x20\x20 |
| 148 | <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> |
| 149 | <td width="100%%"><dl> |
| 150 | <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> |
| 151 | </font></dt><dd> |
| 152 | <dl> |
| 153 | <dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#A">A</a> |
| 154 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#B">B</a> |
Benjamin Peterson | ed1160b | 2014-06-07 16:44:00 -0700 | [diff] [blame] | 155 | </font></dt><dt><font face="helvetica, arial"><a href="test.pydoc_mod.html#C">C</a> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 156 | </font></dt></dl> |
| 157 | </dd> |
| 158 | </dl> |
| 159 | <p> |
| 160 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 161 | <tr bgcolor="#ffc8d8"> |
| 162 | <td colspan=3 valign=bottom> <br> |
| 163 | <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> |
| 164 | \x20\x20\x20\x20 |
| 165 | <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> |
| 166 | <td colspan=2><tt>Hello and goodbye<br> </tt></td></tr> |
| 167 | <tr><td> </td> |
| 168 | <td width="100%%">Methods defined here:<br> |
| 169 | <dl><dt><a name="A-__init__"><strong>__init__</strong></a>()</dt><dd><tt>Wow, I have no function!</tt></dd></dl> |
| 170 | |
| 171 | <hr> |
| 172 | Data descriptors defined here:<br> |
| 173 | <dl><dt><strong>__dict__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 174 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 175 | </dl> |
| 176 | <dl><dt><strong>__weakref__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 177 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 178 | </dl> |
| 179 | </td></tr></table> <p> |
| 180 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 181 | <tr bgcolor="#ffc8d8"> |
| 182 | <td colspan=3 valign=bottom> <br> |
| 183 | <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> |
| 184 | \x20\x20\x20\x20 |
| 185 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 186 | <td width="100%%">Data descriptors defined here:<br> |
| 187 | <dl><dt><strong>__dict__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 188 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 189 | </dl> |
| 190 | <dl><dt><strong>__weakref__</strong></dt> |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 191 | <dd><tt>%s</tt></dd> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 192 | </dl> |
| 193 | <hr> |
| 194 | Data and other attributes defined here:<br> |
| 195 | <dl><dt><strong>NO_MEANING</strong> = 'eggs'</dl> |
| 196 | |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 197 | <dl><dt><strong>__annotations__</strong> = {'NO_MEANING': 'str'}</dl> |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 198 | |
Benjamin Peterson | ed1160b | 2014-06-07 16:44:00 -0700 | [diff] [blame] | 199 | </td></tr></table> <p> |
| 200 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 201 | <tr bgcolor="#ffc8d8"> |
| 202 | <td colspan=3 valign=bottom> <br> |
| 203 | <font color="#000000" face="helvetica, arial"><a name="C">class <strong>C</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> |
| 204 | \x20\x20\x20\x20 |
| 205 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 206 | <td width="100%%">Methods defined here:<br> |
| 207 | <dl><dt><a name="C-get_answer"><strong>get_answer</strong></a>(self)</dt><dd><tt>Return <a href="#C-say_no">say_no</a>()</tt></dd></dl> |
| 208 | |
| 209 | <dl><dt><a name="C-is_it_true"><strong>is_it_true</strong></a>(self)</dt><dd><tt>Return self.<a href="#C-get_answer">get_answer</a>()</tt></dd></dl> |
| 210 | |
| 211 | <dl><dt><a name="C-say_no"><strong>say_no</strong></a>(self)</dt></dl> |
| 212 | |
| 213 | <hr> |
| 214 | Data descriptors defined here:<br> |
| 215 | <dl><dt><strong>__dict__</strong></dt> |
| 216 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 217 | </dl> |
| 218 | <dl><dt><strong>__weakref__</strong></dt> |
| 219 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 220 | </dl> |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 221 | </td></tr></table></td></tr></table><p> |
| 222 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 223 | <tr bgcolor="#eeaa77"> |
| 224 | <td colspan=3 valign=bottom> <br> |
| 225 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 226 | \x20\x20\x20\x20 |
| 227 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 228 | <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> |
| 229 | hunger<br> |
| 230 | lack of Python<br> |
| 231 | war</tt></dd></dl> |
| 232 | <dl><dt><a name="-nodoc_func"><strong>nodoc_func</strong></a>()</dt></dl> |
| 233 | </td></tr></table><p> |
| 234 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 235 | <tr bgcolor="#55aa55"> |
| 236 | <td colspan=3 valign=bottom> <br> |
| 237 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 238 | \x20\x20\x20\x20 |
| 239 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 240 | <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] | 241 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 242 | <tr bgcolor="#7799ee"> |
| 243 | <td colspan=3 valign=bottom> <br> |
| 244 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 245 | \x20\x20\x20\x20 |
| 246 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 247 | <td width="100%%">Benjamin Peterson</td></tr></table><p> |
| 248 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 249 | <tr bgcolor="#7799ee"> |
| 250 | <td colspan=3 valign=bottom> <br> |
| 251 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 252 | \x20\x20\x20\x20 |
| 253 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 254 | <td width="100%%">Nobody</td></tr></table> |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 255 | """.strip() # ' <- emacs turd |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 256 | |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 257 | expected_html_data_docstrings = tuple(s.replace(' ', ' ') |
| 258 | for s in expected_data_docstrings) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 259 | |
| 260 | # output pattern for missing module |
Serhiy Storchaka | 1c20551 | 2015-03-01 00:42:54 +0200 | [diff] [blame] | 261 | missing_pattern = '''\ |
| 262 | No Python documentation found for %r. |
| 263 | Use help() to get the interactive help utility. |
| 264 | Use help(str) for help on the str class.'''.replace('\n', os.linesep) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 265 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 266 | # output pattern for module with bad imports |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 267 | badimport_pattern = "problem in %s - ModuleNotFoundError: No module named %r" |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 268 | |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 269 | expected_dynamicattribute_pattern = """ |
| 270 | Help on class DA in module %s: |
| 271 | |
| 272 | class DA(builtins.object) |
| 273 | | Data descriptors defined here: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 274 | |\x20\x20 |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 275 | | __dict__%s |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 276 | |\x20\x20 |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 277 | | __weakref__%s |
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 | | ham |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 280 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 281 | | ---------------------------------------------------------------------- |
| 282 | | Data and other attributes inherited from Meta: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 283 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 284 | | ham = 'spam' |
| 285 | """.strip() |
| 286 | |
| 287 | expected_virtualattribute_pattern1 = """ |
| 288 | Help on class Class in module %s: |
| 289 | |
| 290 | class Class(builtins.object) |
| 291 | | Data and other attributes inherited from Meta: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 292 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 293 | | LIFE = 42 |
| 294 | """.strip() |
| 295 | |
| 296 | expected_virtualattribute_pattern2 = """ |
| 297 | Help on class Class1 in module %s: |
| 298 | |
| 299 | class Class1(builtins.object) |
| 300 | | Data and other attributes inherited from Meta1: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 301 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 302 | | one = 1 |
| 303 | """.strip() |
| 304 | |
| 305 | expected_virtualattribute_pattern3 = """ |
| 306 | Help on class Class2 in module %s: |
| 307 | |
| 308 | class Class2(Class1) |
| 309 | | Method resolution order: |
| 310 | | Class2 |
| 311 | | Class1 |
| 312 | | builtins.object |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 313 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 314 | | Data and other attributes inherited from Meta1: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 315 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 316 | | one = 1 |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 317 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 318 | | ---------------------------------------------------------------------- |
| 319 | | Data and other attributes inherited from Meta3: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 320 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 321 | | three = 3 |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 322 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 323 | | ---------------------------------------------------------------------- |
| 324 | | Data and other attributes inherited from Meta2: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 325 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 326 | | two = 2 |
| 327 | """.strip() |
| 328 | |
| 329 | expected_missingattribute_pattern = """ |
| 330 | Help on class C in module %s: |
| 331 | |
| 332 | class C(builtins.object) |
| 333 | | Data and other attributes defined here: |
Charles-François Natali | 1a82f7e | 2013-10-21 14:46:34 +0200 | [diff] [blame] | 334 | |\x20\x20 |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 335 | | here = 'present!' |
| 336 | """.strip() |
| 337 | |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 338 | def run_pydoc(module_name, *args, **env): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 339 | """ |
| 340 | Runs pydoc on the specified module. Returns the stripped |
| 341 | output of pydoc. |
| 342 | """ |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 343 | args = args + (module_name,) |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 344 | # do not write bytecode files to avoid caching errors |
| 345 | rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env) |
Antoine Pitrou | f7f5475 | 2011-07-15 22:42:12 +0200 | [diff] [blame] | 346 | return out.strip() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 347 | |
| 348 | def get_pydoc_html(module): |
| 349 | "Returns pydoc generated output as html" |
| 350 | doc = pydoc.HTMLDoc() |
| 351 | output = doc.docmodule(module) |
| 352 | loc = doc.getdocloc(pydoc_mod) or "" |
| 353 | if loc: |
| 354 | loc = "<br><a href=\"" + loc + "\">Module Docs</a>" |
| 355 | return output.strip(), loc |
| 356 | |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 357 | def get_pydoc_link(module): |
| 358 | "Returns a documentation web link of a module" |
Bo Bayles | 4e11c46 | 2018-07-29 14:15:14 -0500 | [diff] [blame] | 359 | abspath = os.path.abspath |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 360 | dirname = os.path.dirname |
Bo Bayles | 4e11c46 | 2018-07-29 14:15:14 -0500 | [diff] [blame] | 361 | basedir = dirname(dirname(abspath(__file__))) |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 362 | doc = pydoc.TextDoc() |
| 363 | loc = doc.getdocloc(module, basedir=basedir) |
| 364 | return loc |
| 365 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 366 | def get_pydoc_text(module): |
| 367 | "Returns pydoc generated output as text" |
| 368 | doc = pydoc.TextDoc() |
| 369 | loc = doc.getdocloc(pydoc_mod) or "" |
| 370 | if loc: |
| 371 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 372 | |
| 373 | output = doc.docmodule(module) |
| 374 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 375 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 376 | patt = re.compile('\b.') |
| 377 | output = patt.sub('', output) |
| 378 | return output.strip(), loc |
| 379 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 380 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 381 | # Bit of hack, but good enough for test purposes |
| 382 | header, _, _ = text.partition("</head>") |
| 383 | _, _, title = header.partition("<title>") |
| 384 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 385 | return title |
| 386 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 388 | class PydocBaseTest(unittest.TestCase): |
| 389 | |
| 390 | def _restricted_walk_packages(self, walk_packages, path=None): |
| 391 | """ |
| 392 | A version of pkgutil.walk_packages() that will restrict itself to |
| 393 | a given path. |
| 394 | """ |
| 395 | default_path = path or [os.path.dirname(__file__)] |
| 396 | def wrapper(path=None, prefix='', onerror=None): |
| 397 | return walk_packages(path or default_path, prefix, onerror) |
| 398 | return wrapper |
| 399 | |
| 400 | @contextlib.contextmanager |
| 401 | def restrict_walk_packages(self, path=None): |
| 402 | walk_packages = pkgutil.walk_packages |
| 403 | pkgutil.walk_packages = self._restricted_walk_packages(walk_packages, |
| 404 | path) |
| 405 | try: |
| 406 | yield |
| 407 | finally: |
| 408 | pkgutil.walk_packages = walk_packages |
| 409 | |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 410 | def call_url_handler(self, url, expected_title): |
| 411 | text = pydoc._url_handler(url, "text/html") |
| 412 | result = get_html_title(text) |
| 413 | # Check the title to ensure an unexpected error page was not returned |
| 414 | self.assertEqual(result, expected_title, text) |
| 415 | return text |
| 416 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 417 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 418 | class PydocDocTest(unittest.TestCase): |
Serhiy Storchaka | a44d34e | 2018-11-08 08:48:11 +0200 | [diff] [blame] | 419 | maxDiff = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 420 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 421 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 422 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 423 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 424 | 'trace function introduces __locals__ unexpectedly') |
Charles-François Natali | 57398c3 | 2014-06-20 22:59:12 +0100 | [diff] [blame] | 425 | @requires_docstrings |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 426 | def test_html_doc(self): |
| 427 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 428 | mod_file = inspect.getabsfile(pydoc_mod) |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 429 | mod_url = urllib.parse.quote(mod_file) |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 430 | expected_html = expected_html_pattern % ( |
| 431 | (mod_url, mod_file, doc_loc) + |
| 432 | expected_html_data_docstrings) |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 433 | self.assertEqual(result, expected_html) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 434 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 435 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 436 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 437 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 438 | 'trace function introduces __locals__ unexpectedly') |
Charles-François Natali | 57398c3 | 2014-06-20 22:59:12 +0100 | [diff] [blame] | 439 | @requires_docstrings |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 440 | def test_text_doc(self): |
| 441 | result, doc_loc = get_pydoc_text(pydoc_mod) |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 442 | expected_text = expected_text_pattern % ( |
| 443 | (doc_loc,) + |
| 444 | expected_text_data_docstrings + |
| 445 | (inspect.getabsfile(pydoc_mod),)) |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 446 | self.assertEqual(expected_text, result) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 447 | |
Serhiy Storchaka | 056eb02 | 2014-02-19 23:05:12 +0200 | [diff] [blame] | 448 | def test_text_enum_member_with_value_zero(self): |
| 449 | # Test issue #20654 to ensure enum member with value 0 can be |
| 450 | # displayed. It used to throw KeyError: 'zero'. |
| 451 | import enum |
| 452 | class BinaryInteger(enum.IntEnum): |
| 453 | zero = 0 |
| 454 | one = 1 |
| 455 | doc = pydoc.render_doc(BinaryInteger) |
| 456 | self.assertIn('<BinaryInteger.zero: 0>', doc) |
| 457 | |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 458 | def test_mixed_case_module_names_are_lower_cased(self): |
| 459 | # issue16484 |
| 460 | doc_link = get_pydoc_link(xml.etree.ElementTree) |
| 461 | self.assertIn('xml.etree.elementtree', doc_link) |
| 462 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 463 | def test_issue8225(self): |
| 464 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 465 | result, doc_loc = get_pydoc_text(xml.etree) |
| 466 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 467 | |
Benjamin Peterson | 159824e | 2014-06-07 20:14:26 -0700 | [diff] [blame] | 468 | def test_getpager_with_stdin_none(self): |
| 469 | previous_stdin = sys.stdin |
| 470 | try: |
| 471 | sys.stdin = None |
| 472 | pydoc.getpager() # Shouldn't fail. |
| 473 | finally: |
| 474 | sys.stdin = previous_stdin |
| 475 | |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 476 | def test_non_str_name(self): |
| 477 | # issue14638 |
| 478 | # Treat illegal (non-str) name like no name |
Karthikeyan Singaravelan | 696136b | 2020-04-18 21:49:32 +0530 | [diff] [blame] | 479 | |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 480 | class A: |
| 481 | __name__ = 42 |
| 482 | class B: |
| 483 | pass |
| 484 | adoc = pydoc.render_doc(A()) |
| 485 | bdoc = pydoc.render_doc(B()) |
Eric Snow | 4f29e75 | 2016-09-08 15:11:11 -0700 | [diff] [blame] | 486 | self.assertEqual(adoc.replace("A", "B"), bdoc) |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 487 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 488 | def test_not_here(self): |
| 489 | missing_module = "test.i_am_not_here" |
| 490 | result = str(run_pydoc(missing_module), 'ascii') |
| 491 | expected = missing_pattern % missing_module |
| 492 | self.assertEqual(expected, result, |
| 493 | "documentation for missing module found") |
| 494 | |
Serhiy Storchaka | 4c094e5 | 2015-03-01 15:31:36 +0200 | [diff] [blame] | 495 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 496 | 'Docstrings are omitted with -OO and above') |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 497 | def test_not_ascii(self): |
| 498 | result = run_pydoc('test.test_pydoc.nonascii', PYTHONIOENCODING='ascii') |
| 499 | encoded = nonascii.__doc__.encode('ascii', 'backslashreplace') |
| 500 | self.assertIn(encoded, result) |
| 501 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 502 | def test_input_strip(self): |
| 503 | missing_module = " test.i_am_not_here " |
| 504 | result = str(run_pydoc(missing_module), 'ascii') |
| 505 | expected = missing_pattern % missing_module.strip() |
| 506 | self.assertEqual(expected, result) |
| 507 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 508 | def test_stripid(self): |
| 509 | # test with strings, other implementations might have different repr() |
| 510 | stripid = pydoc.stripid |
| 511 | # strip the id |
| 512 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 513 | '<function stripid>') |
| 514 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 515 | '<function stripid>') |
| 516 | # nothing to strip, return the same text |
| 517 | self.assertEqual(stripid('42'), '42') |
| 518 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 519 | "<type 'exceptions.Exception'>") |
| 520 | |
Sanyam Khurana | a323cdc | 2018-10-21 00:22:02 -0700 | [diff] [blame] | 521 | def test_builtin_with_more_than_four_children(self): |
| 522 | """Tests help on builtin object which have more than four child classes. |
| 523 | |
| 524 | When running help() on a builtin class which has child classes, it |
| 525 | should contain a "Built-in subclasses" section and only 4 classes |
| 526 | should be displayed with a hint on how many more subclasses are present. |
| 527 | For example: |
| 528 | |
| 529 | >>> help(object) |
| 530 | Help on class object in module builtins: |
| 531 | |
| 532 | class object |
| 533 | | The most base type |
| 534 | | |
| 535 | | Built-in subclasses: |
| 536 | | async_generator |
| 537 | | BaseException |
| 538 | | builtin_function_or_method |
| 539 | | bytearray |
| 540 | | ... and 82 other subclasses |
| 541 | """ |
| 542 | doc = pydoc.TextDoc() |
| 543 | text = doc.docclass(object) |
| 544 | snip = (" | Built-in subclasses:\n" |
| 545 | " | async_generator\n" |
| 546 | " | BaseException\n" |
| 547 | " | builtin_function_or_method\n" |
| 548 | " | bytearray\n" |
| 549 | " | ... and \\d+ other subclasses") |
| 550 | self.assertRegex(text, snip) |
| 551 | |
| 552 | def test_builtin_with_child(self): |
| 553 | """Tests help on builtin object which have only child classes. |
| 554 | |
| 555 | When running help() on a builtin class which has child classes, it |
| 556 | should contain a "Built-in subclasses" section. For example: |
| 557 | |
| 558 | >>> help(ArithmeticError) |
| 559 | Help on class ArithmeticError in module builtins: |
| 560 | |
| 561 | class ArithmeticError(Exception) |
| 562 | | Base class for arithmetic errors. |
| 563 | | |
| 564 | ... |
| 565 | | |
| 566 | | Built-in subclasses: |
| 567 | | FloatingPointError |
| 568 | | OverflowError |
| 569 | | ZeroDivisionError |
| 570 | """ |
| 571 | doc = pydoc.TextDoc() |
| 572 | text = doc.docclass(ArithmeticError) |
| 573 | snip = (" | Built-in subclasses:\n" |
| 574 | " | FloatingPointError\n" |
| 575 | " | OverflowError\n" |
| 576 | " | ZeroDivisionError") |
| 577 | self.assertIn(snip, text) |
| 578 | |
| 579 | def test_builtin_with_grandchild(self): |
| 580 | """Tests help on builtin classes which have grandchild classes. |
| 581 | |
| 582 | When running help() on a builtin class which has child classes, it |
| 583 | should contain a "Built-in subclasses" section. However, if it also has |
| 584 | grandchildren, these should not show up on the subclasses section. |
| 585 | For example: |
| 586 | |
| 587 | >>> help(Exception) |
| 588 | Help on class Exception in module builtins: |
| 589 | |
| 590 | class Exception(BaseException) |
| 591 | | Common base class for all non-exit exceptions. |
| 592 | | |
| 593 | ... |
| 594 | | |
| 595 | | Built-in subclasses: |
| 596 | | ArithmeticError |
| 597 | | AssertionError |
| 598 | | AttributeError |
| 599 | ... |
| 600 | """ |
| 601 | doc = pydoc.TextDoc() |
| 602 | text = doc.docclass(Exception) |
| 603 | snip = (" | Built-in subclasses:\n" |
| 604 | " | ArithmeticError\n" |
| 605 | " | AssertionError\n" |
| 606 | " | AttributeError") |
| 607 | self.assertIn(snip, text) |
| 608 | # Testing that the grandchild ZeroDivisionError does not show up |
| 609 | self.assertNotIn('ZeroDivisionError', text) |
| 610 | |
| 611 | def test_builtin_no_child(self): |
| 612 | """Tests help on builtin object which have no child classes. |
| 613 | |
| 614 | When running help() on a builtin class which has no child classes, it |
| 615 | should not contain any "Built-in subclasses" section. For example: |
| 616 | |
| 617 | >>> help(ZeroDivisionError) |
| 618 | |
| 619 | Help on class ZeroDivisionError in module builtins: |
| 620 | |
| 621 | class ZeroDivisionError(ArithmeticError) |
| 622 | | Second argument to a division or modulo operation was zero. |
| 623 | | |
| 624 | | Method resolution order: |
| 625 | | ZeroDivisionError |
| 626 | | ArithmeticError |
| 627 | | Exception |
| 628 | | BaseException |
| 629 | | object |
| 630 | | |
| 631 | | Methods defined here: |
| 632 | ... |
| 633 | """ |
| 634 | doc = pydoc.TextDoc() |
| 635 | text = doc.docclass(ZeroDivisionError) |
| 636 | # Testing that the subclasses section does not appear |
| 637 | self.assertNotIn('Built-in subclasses', text) |
| 638 | |
Sanyam Khurana | b539cef | 2018-12-31 10:44:47 +0530 | [diff] [blame] | 639 | def test_builtin_on_metaclasses(self): |
| 640 | """Tests help on metaclasses. |
| 641 | |
| 642 | When running help() on a metaclasses such as type, it |
| 643 | should not contain any "Built-in subclasses" section. |
| 644 | """ |
| 645 | doc = pydoc.TextDoc() |
| 646 | text = doc.docclass(type) |
| 647 | # Testing that the subclasses section does not appear |
| 648 | self.assertNotIn('Built-in subclasses', text) |
| 649 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 650 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 651 | 'Docstrings are omitted with -O2 and above') |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 652 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 653 | 'trace function introduces __locals__ unexpectedly') |
Charles-François Natali | 57398c3 | 2014-06-20 22:59:12 +0100 | [diff] [blame] | 654 | @requires_docstrings |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 655 | def test_help_output_redirect(self): |
| 656 | # issue 940286, if output is set in Helper, then all output from |
| 657 | # Helper.help should be redirected |
| 658 | old_pattern = expected_text_pattern |
| 659 | getpager_old = pydoc.getpager |
| 660 | getpager_new = lambda: (lambda x: x) |
| 661 | self.maxDiff = None |
| 662 | |
| 663 | buf = StringIO() |
| 664 | helper = pydoc.Helper(output=buf) |
| 665 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 666 | module = "test.pydoc_mod" |
| 667 | help_header = """ |
| 668 | Help on module test.pydoc_mod in test: |
| 669 | |
| 670 | """.lstrip() |
| 671 | help_header = textwrap.dedent(help_header) |
| 672 | expected_help_pattern = help_header + expected_text_pattern |
| 673 | |
| 674 | pydoc.getpager = getpager_new |
| 675 | try: |
| 676 | with captured_output('stdout') as output, \ |
| 677 | captured_output('stderr') as err: |
| 678 | helper.help(module) |
| 679 | result = buf.getvalue().strip() |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 680 | expected_text = expected_help_pattern % ( |
| 681 | (doc_loc,) + |
| 682 | expected_text_data_docstrings + |
| 683 | (inspect.getabsfile(pydoc_mod),)) |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 684 | self.assertEqual('', output.getvalue()) |
| 685 | self.assertEqual('', err.getvalue()) |
| 686 | self.assertEqual(expected_text, result) |
| 687 | finally: |
| 688 | pydoc.getpager = getpager_old |
| 689 | |
Serhiy Storchaka | 052b2df | 2018-12-31 14:15:16 +0200 | [diff] [blame] | 690 | def test_namedtuple_fields(self): |
| 691 | Person = namedtuple('Person', ['nickname', 'firstname']) |
| 692 | with captured_stdout() as help_io: |
| 693 | pydoc.help(Person) |
| 694 | helptext = help_io.getvalue() |
| 695 | self.assertIn("nickname", helptext) |
| 696 | self.assertIn("firstname", helptext) |
| 697 | self.assertIn("Alias for field number 0", helptext) |
| 698 | self.assertIn("Alias for field number 1", helptext) |
| 699 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 700 | def test_namedtuple_public_underscore(self): |
| 701 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 702 | with captured_stdout() as help_io: |
Terry Jan Reedy | 5c81164 | 2013-11-04 21:43:26 -0500 | [diff] [blame] | 703 | pydoc.help(NT) |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 704 | helptext = help_io.getvalue() |
| 705 | self.assertIn('_1', helptext) |
| 706 | self.assertIn('_replace', helptext) |
| 707 | self.assertIn('_asdict', helptext) |
| 708 | |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 709 | def test_synopsis(self): |
| 710 | self.addCleanup(unlink, TESTFN) |
| 711 | for encoding in ('ISO-8859-1', 'UTF-8'): |
| 712 | with open(TESTFN, 'w', encoding=encoding) as script: |
| 713 | if encoding != 'UTF-8': |
| 714 | print('#coding: {}'.format(encoding), file=script) |
| 715 | print('"""line 1: h\xe9', file=script) |
| 716 | print('line 2: hi"""', file=script) |
| 717 | synopsis = pydoc.synopsis(TESTFN, {}) |
| 718 | self.assertEqual(synopsis, 'line 1: h\xe9') |
| 719 | |
Serhiy Storchaka | 4c094e5 | 2015-03-01 15:31:36 +0200 | [diff] [blame] | 720 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 721 | 'Docstrings are omitted with -OO and above') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 722 | def test_synopsis_sourceless(self): |
| 723 | expected = os.__doc__.splitlines()[0] |
| 724 | filename = os.__cached__ |
| 725 | synopsis = pydoc.synopsis(filename) |
| 726 | |
| 727 | self.assertEqual(synopsis, expected) |
| 728 | |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 729 | def test_synopsis_sourceless_empty_doc(self): |
Hai Shi | bb0424b | 2020-08-04 00:47:42 +0800 | [diff] [blame] | 730 | with os_helper.temp_cwd() as test_dir: |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 731 | init_path = os.path.join(test_dir, 'foomod42.py') |
| 732 | cached_path = importlib.util.cache_from_source(init_path) |
| 733 | with open(init_path, 'w') as fobj: |
| 734 | fobj.write("foo = 1") |
| 735 | py_compile.compile(init_path) |
| 736 | synopsis = pydoc.synopsis(init_path, {}) |
| 737 | self.assertIsNone(synopsis) |
| 738 | synopsis_cached = pydoc.synopsis(cached_path, {}) |
| 739 | self.assertIsNone(synopsis_cached) |
| 740 | |
R David Murray | 455f296 | 2013-03-19 00:00:33 -0400 | [diff] [blame] | 741 | def test_splitdoc_with_description(self): |
| 742 | example_string = "I Am A Doc\n\n\nHere is my description" |
| 743 | self.assertEqual(pydoc.splitdoc(example_string), |
| 744 | ('I Am A Doc', '\nHere is my description')) |
| 745 | |
R David Murray | 455f296 | 2013-03-19 00:00:33 -0400 | [diff] [blame] | 746 | def test_is_package_when_not_package(self): |
Hai Shi | bb0424b | 2020-08-04 00:47:42 +0800 | [diff] [blame] | 747 | with os_helper.temp_cwd() as test_dir: |
R David Murray | 455f296 | 2013-03-19 00:00:33 -0400 | [diff] [blame] | 748 | self.assertFalse(pydoc.ispackage(test_dir)) |
| 749 | |
| 750 | def test_is_package_when_is_package(self): |
Hai Shi | bb0424b | 2020-08-04 00:47:42 +0800 | [diff] [blame] | 751 | with os_helper.temp_cwd() as test_dir: |
R David Murray | 455f296 | 2013-03-19 00:00:33 -0400 | [diff] [blame] | 752 | init_path = os.path.join(test_dir, '__init__.py') |
| 753 | open(init_path, 'w').close() |
| 754 | self.assertTrue(pydoc.ispackage(test_dir)) |
| 755 | os.remove(init_path) |
| 756 | |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 757 | def test_allmethods(self): |
| 758 | # issue 17476: allmethods was no longer returning unbound methods. |
| 759 | # This test is a bit fragile in the face of changes to object and type, |
| 760 | # but I can't think of a better way to do it without duplicating the |
| 761 | # logic of the function under test. |
| 762 | |
| 763 | class TestClass(object): |
| 764 | def method_returning_true(self): |
| 765 | return True |
| 766 | |
| 767 | # What we expect to get back: everything on object... |
| 768 | expected = dict(vars(object)) |
| 769 | # ...plus our unbound method... |
| 770 | expected['method_returning_true'] = TestClass.method_returning_true |
| 771 | # ...but not the non-methods on object. |
| 772 | del expected['__doc__'] |
| 773 | del expected['__class__'] |
| 774 | # inspect resolves descriptors on type into methods, but vars doesn't, |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 775 | # so we need to update __subclasshook__ and __init_subclass__. |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 776 | expected['__subclasshook__'] = TestClass.__subclasshook__ |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 777 | expected['__init_subclass__'] = TestClass.__init_subclass__ |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 778 | |
| 779 | methods = pydoc.allmethods(TestClass) |
| 780 | self.assertDictEqual(methods, expected) |
| 781 | |
Serhiy Storchaka | a44d34e | 2018-11-08 08:48:11 +0200 | [diff] [blame] | 782 | def test_method_aliases(self): |
| 783 | class A: |
| 784 | def tkraise(self, aboveThis=None): |
| 785 | """Raise this widget in the stacking order.""" |
| 786 | lift = tkraise |
| 787 | def a_size(self): |
| 788 | """Return size""" |
| 789 | class B(A): |
| 790 | def itemconfigure(self, tagOrId, cnf=None, **kw): |
| 791 | """Configure resources of an item TAGORID.""" |
| 792 | itemconfig = itemconfigure |
| 793 | b_size = A.a_size |
| 794 | |
| 795 | doc = pydoc.render_doc(B) |
| 796 | # clean up the extra text formatting that pydoc performs |
| 797 | doc = re.sub('\b.', '', doc) |
| 798 | self.assertEqual(doc, '''\ |
| 799 | Python Library Documentation: class B in module %s |
| 800 | |
| 801 | class B(A) |
| 802 | | Method resolution order: |
| 803 | | B |
| 804 | | A |
| 805 | | builtins.object |
| 806 | |\x20\x20 |
| 807 | | Methods defined here: |
| 808 | |\x20\x20 |
| 809 | | b_size = a_size(self) |
| 810 | |\x20\x20 |
| 811 | | itemconfig = itemconfigure(self, tagOrId, cnf=None, **kw) |
| 812 | |\x20\x20 |
| 813 | | itemconfigure(self, tagOrId, cnf=None, **kw) |
| 814 | | Configure resources of an item TAGORID. |
| 815 | |\x20\x20 |
| 816 | | ---------------------------------------------------------------------- |
| 817 | | Methods inherited from A: |
| 818 | |\x20\x20 |
| 819 | | a_size(self) |
| 820 | | Return size |
| 821 | |\x20\x20 |
| 822 | | lift = tkraise(self, aboveThis=None) |
| 823 | |\x20\x20 |
| 824 | | tkraise(self, aboveThis=None) |
| 825 | | Raise this widget in the stacking order. |
| 826 | |\x20\x20 |
| 827 | | ---------------------------------------------------------------------- |
| 828 | | Data descriptors inherited from A: |
| 829 | |\x20\x20 |
| 830 | | __dict__ |
| 831 | | dictionary for instance variables (if defined) |
| 832 | |\x20\x20 |
| 833 | | __weakref__ |
| 834 | | list of weak references to the object (if defined) |
| 835 | ''' % __name__) |
| 836 | |
| 837 | doc = pydoc.render_doc(B, renderer=pydoc.HTMLDoc()) |
| 838 | self.assertEqual(doc, '''\ |
| 839 | Python Library Documentation: class B in module %s |
| 840 | |
| 841 | <p> |
| 842 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 843 | <tr bgcolor="#ffc8d8"> |
| 844 | <td colspan=3 valign=bottom> <br> |
| 845 | <font color="#000000" face="helvetica, arial"><a name="B">class <strong>B</strong></a>(A)</font></td></tr> |
| 846 | \x20\x20\x20\x20 |
| 847 | <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> |
| 848 | <td width="100%%"><dl><dt>Method resolution order:</dt> |
| 849 | <dd>B</dd> |
| 850 | <dd>A</dd> |
| 851 | <dd><a href="builtins.html#object">builtins.object</a></dd> |
| 852 | </dl> |
| 853 | <hr> |
| 854 | Methods defined here:<br> |
| 855 | <dl><dt><a name="B-b_size"><strong>b_size</strong></a> = <a href="#B-a_size">a_size</a>(self)</dt></dl> |
| 856 | |
| 857 | <dl><dt><a name="B-itemconfig"><strong>itemconfig</strong></a> = <a href="#B-itemconfigure">itemconfigure</a>(self, tagOrId, cnf=None, **kw)</dt></dl> |
| 858 | |
| 859 | <dl><dt><a name="B-itemconfigure"><strong>itemconfigure</strong></a>(self, tagOrId, cnf=None, **kw)</dt><dd><tt>Configure resources of an item TAGORID.</tt></dd></dl> |
| 860 | |
| 861 | <hr> |
| 862 | Methods inherited from A:<br> |
| 863 | <dl><dt><a name="B-a_size"><strong>a_size</strong></a>(self)</dt><dd><tt>Return size</tt></dd></dl> |
| 864 | |
| 865 | <dl><dt><a name="B-lift"><strong>lift</strong></a> = <a href="#B-tkraise">tkraise</a>(self, aboveThis=None)</dt></dl> |
| 866 | |
| 867 | <dl><dt><a name="B-tkraise"><strong>tkraise</strong></a>(self, aboveThis=None)</dt><dd><tt>Raise this widget in the stacking order.</tt></dd></dl> |
| 868 | |
| 869 | <hr> |
| 870 | Data descriptors inherited from A:<br> |
| 871 | <dl><dt><strong>__dict__</strong></dt> |
| 872 | <dd><tt>dictionary for instance variables (if defined)</tt></dd> |
| 873 | </dl> |
| 874 | <dl><dt><strong>__weakref__</strong></dt> |
| 875 | <dd><tt>list of weak references to the object (if defined)</tt></dd> |
| 876 | </dl> |
| 877 | </td></tr></table>\ |
| 878 | ''' % __name__) |
| 879 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 881 | class PydocImportTest(PydocBaseTest): |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 882 | |
| 883 | def setUp(self): |
| 884 | self.test_dir = os.mkdir(TESTFN) |
| 885 | self.addCleanup(rmtree, TESTFN) |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 886 | importlib.invalidate_caches() |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 887 | |
| 888 | def test_badimport(self): |
| 889 | # This tests the fix for issue 5230, where if pydoc found the module |
| 890 | # but the module had an internal import error pydoc would report no doc |
| 891 | # found. |
| 892 | modname = 'testmod_xyzzy' |
| 893 | testpairs = ( |
| 894 | ('i_am_not_here', 'i_am_not_here'), |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 895 | ('test.i_am_not_here_either', 'test.i_am_not_here_either'), |
| 896 | ('test.i_am_not_here.neither_am_i', 'test.i_am_not_here'), |
| 897 | ('i_am_not_here.{}'.format(modname), 'i_am_not_here'), |
| 898 | ('test.{}'.format(modname), 'test.{}'.format(modname)), |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 899 | ) |
| 900 | |
| 901 | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
| 902 | for importstring, expectedinmsg in testpairs: |
| 903 | with open(sourcefn, 'w') as f: |
| 904 | f.write("import {}\n".format(importstring)) |
| 905 | result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
| 906 | expected = badimport_pattern % (modname, expectedinmsg) |
| 907 | self.assertEqual(expected, result) |
| 908 | |
| 909 | def test_apropos_with_bad_package(self): |
| 910 | # Issue 7425 - pydoc -k failed when bad package on path |
| 911 | pkgdir = os.path.join(TESTFN, "syntaxerr") |
| 912 | os.mkdir(pkgdir) |
| 913 | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
| 914 | with open(badsyntax, 'w') as f: |
| 915 | f.write("invalid python syntax = $1\n") |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 916 | with self.restrict_walk_packages(path=[TESTFN]): |
| 917 | with captured_stdout() as out: |
| 918 | with captured_stderr() as err: |
| 919 | pydoc.apropos('xyzzy') |
| 920 | # No result, no error |
| 921 | self.assertEqual(out.getvalue(), '') |
| 922 | self.assertEqual(err.getvalue(), '') |
| 923 | # The package name is still matched |
| 924 | with captured_stdout() as out: |
| 925 | with captured_stderr() as err: |
| 926 | pydoc.apropos('syntaxerr') |
| 927 | self.assertEqual(out.getvalue().strip(), 'syntaxerr') |
| 928 | self.assertEqual(err.getvalue(), '') |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 929 | |
| 930 | def test_apropos_with_unreadable_dir(self): |
| 931 | # Issue 7367 - pydoc -k failed when unreadable dir on path |
| 932 | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
| 933 | os.mkdir(self.unreadable_dir, 0) |
| 934 | self.addCleanup(os.rmdir, self.unreadable_dir) |
| 935 | # Note, on Windows the directory appears to be still |
| 936 | # readable so this is not really testing the issue there |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 937 | with self.restrict_walk_packages(path=[TESTFN]): |
| 938 | with captured_stdout() as out: |
| 939 | with captured_stderr() as err: |
| 940 | pydoc.apropos('SOMEKEY') |
| 941 | # No result, no error |
| 942 | self.assertEqual(out.getvalue(), '') |
| 943 | self.assertEqual(err.getvalue(), '') |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 944 | |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 945 | def test_apropos_empty_doc(self): |
| 946 | pkgdir = os.path.join(TESTFN, 'walkpkg') |
| 947 | os.mkdir(pkgdir) |
| 948 | self.addCleanup(rmtree, pkgdir) |
| 949 | init_path = os.path.join(pkgdir, '__init__.py') |
| 950 | with open(init_path, 'w') as fobj: |
| 951 | fobj.write("foo = 1") |
| 952 | current_mode = stat.S_IMODE(os.stat(pkgdir).st_mode) |
| 953 | try: |
| 954 | os.chmod(pkgdir, current_mode & ~stat.S_IEXEC) |
| 955 | with self.restrict_walk_packages(path=[TESTFN]), captured_stdout() as stdout: |
| 956 | pydoc.apropos('') |
| 957 | self.assertIn('walkpkg', stdout.getvalue()) |
| 958 | finally: |
| 959 | os.chmod(pkgdir, current_mode) |
| 960 | |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 961 | def test_url_search_package_error(self): |
| 962 | # URL handler search should cope with packages that raise exceptions |
| 963 | pkgdir = os.path.join(TESTFN, "test_error_package") |
| 964 | os.mkdir(pkgdir) |
| 965 | init = os.path.join(pkgdir, "__init__.py") |
| 966 | with open(init, "wt", encoding="ascii") as f: |
| 967 | f.write("""raise ValueError("ouch")\n""") |
| 968 | with self.restrict_walk_packages(path=[TESTFN]): |
| 969 | # Package has to be importable for the error to have any effect |
| 970 | saved_paths = tuple(sys.path) |
| 971 | sys.path.insert(0, TESTFN) |
| 972 | try: |
| 973 | with self.assertRaisesRegex(ValueError, "ouch"): |
| 974 | import test_error_package # Sanity check |
| 975 | |
| 976 | text = self.call_url_handler("search?key=test_error_package", |
| 977 | "Pydoc: Search Results") |
| 978 | found = ('<a href="test_error_package.html">' |
| 979 | 'test_error_package</a>') |
| 980 | self.assertIn(found, text) |
| 981 | finally: |
| 982 | sys.path[:] = saved_paths |
| 983 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 984 | @unittest.skip('causes undesirable side-effects (#20128)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 985 | def test_modules(self): |
| 986 | # See Helper.listmodules(). |
| 987 | num_header_lines = 2 |
| 988 | num_module_lines_min = 5 # Playing it safe. |
| 989 | num_footer_lines = 3 |
| 990 | expected = num_header_lines + num_module_lines_min + num_footer_lines |
| 991 | |
| 992 | output = StringIO() |
| 993 | helper = pydoc.Helper(output=output) |
| 994 | helper('modules') |
| 995 | result = output.getvalue().strip() |
| 996 | num_lines = len(result.splitlines()) |
| 997 | |
| 998 | self.assertGreaterEqual(num_lines, expected) |
| 999 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 1000 | @unittest.skip('causes undesirable side-effects (#20128)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1001 | def test_modules_search(self): |
| 1002 | # See Helper.listmodules(). |
| 1003 | expected = 'pydoc - ' |
| 1004 | |
| 1005 | output = StringIO() |
| 1006 | helper = pydoc.Helper(output=output) |
| 1007 | with captured_stdout() as help_io: |
| 1008 | helper('modules pydoc') |
| 1009 | result = help_io.getvalue() |
| 1010 | |
| 1011 | self.assertIn(expected, result) |
| 1012 | |
Eric Snow | a46ef70 | 2014-02-22 13:57:08 -0700 | [diff] [blame] | 1013 | @unittest.skip('some buildbots are not cooperating (#20128)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1014 | def test_modules_search_builtin(self): |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 1015 | expected = 'gc - ' |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1016 | |
| 1017 | output = StringIO() |
| 1018 | helper = pydoc.Helper(output=output) |
| 1019 | with captured_stdout() as help_io: |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 1020 | helper('modules garbage') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1021 | result = help_io.getvalue() |
| 1022 | |
| 1023 | self.assertTrue(result.startswith(expected)) |
| 1024 | |
| 1025 | def test_importfile(self): |
| 1026 | loaded_pydoc = pydoc.importfile(pydoc.__file__) |
| 1027 | |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 1028 | self.assertIsNot(loaded_pydoc, pydoc) |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1029 | self.assertEqual(loaded_pydoc.__name__, 'pydoc') |
| 1030 | self.assertEqual(loaded_pydoc.__file__, pydoc.__file__) |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 1031 | self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__) |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1032 | |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 1033 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1034 | class TestDescriptions(unittest.TestCase): |
| 1035 | |
| 1036 | def test_module(self): |
| 1037 | # Check that pydocfodder module can be described |
| 1038 | from test import pydocfodder |
| 1039 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1040 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1041 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1042 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1043 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1044 | c = C() |
| 1045 | |
| 1046 | self.assertEqual(pydoc.describe(C), 'class C') |
| 1047 | self.assertEqual(pydoc.describe(c), 'C') |
| 1048 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1049 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1050 | |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 1051 | def test_typing_pydoc(self): |
| 1052 | def foo(data: typing.List[typing.Any], |
| 1053 | x: int) -> typing.Iterator[typing.Tuple[int, typing.Any]]: |
| 1054 | ... |
| 1055 | T = typing.TypeVar('T') |
| 1056 | class C(typing.Generic[T], typing.Mapping[int, str]): ... |
| 1057 | self.assertEqual(pydoc.render_doc(foo).splitlines()[-1], |
Dong-hee Na | 762b957 | 2017-11-16 03:30:59 +0900 | [diff] [blame] | 1058 | 'f\x08fo\x08oo\x08o(data: List[Any], x: int)' |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 1059 | ' -> Iterator[Tuple[int, Any]]') |
| 1060 | self.assertEqual(pydoc.render_doc(C).splitlines()[2], |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1061 | 'class C\x08C(collections.abc.Mapping, typing.Generic)') |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 1062 | |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 1063 | def test_builtin(self): |
| 1064 | for name in ('str', 'str.translate', 'builtins.str', |
| 1065 | 'builtins.str.translate'): |
| 1066 | # test low-level function |
| 1067 | self.assertIsNotNone(pydoc.locate(name)) |
| 1068 | # test high-level function |
| 1069 | try: |
| 1070 | pydoc.render_doc(name) |
| 1071 | except ImportError: |
Terry Jan Reedy | fe928de | 2014-06-20 14:59:11 -0400 | [diff] [blame] | 1072 | self.fail('finding the doc of {!r} failed'.format(name)) |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 1073 | |
| 1074 | for name in ('notbuiltins', 'strrr', 'strr.translate', |
| 1075 | 'str.trrrranslate', 'builtins.strrr', |
| 1076 | 'builtins.str.trrranslate'): |
| 1077 | self.assertIsNone(pydoc.locate(name)) |
| 1078 | self.assertRaises(ImportError, pydoc.render_doc, name) |
| 1079 | |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 1080 | @staticmethod |
| 1081 | def _get_summary_line(o): |
| 1082 | text = pydoc.plain(pydoc.render_doc(o)) |
| 1083 | lines = text.split('\n') |
| 1084 | assert len(lines) >= 2 |
| 1085 | return lines[2] |
| 1086 | |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1087 | @staticmethod |
| 1088 | def _get_summary_lines(o): |
| 1089 | text = pydoc.plain(pydoc.render_doc(o)) |
| 1090 | lines = text.split('\n') |
| 1091 | return '\n'.join(lines[2:]) |
| 1092 | |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 1093 | # these should include "self" |
| 1094 | def test_unbound_python_method(self): |
| 1095 | self.assertEqual(self._get_summary_line(textwrap.TextWrapper.wrap), |
| 1096 | "wrap(self, text)") |
| 1097 | |
Stefan Krah | 5de3278 | 2014-01-18 23:18:39 +0100 | [diff] [blame] | 1098 | @requires_docstrings |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 1099 | def test_unbound_builtin_method(self): |
| 1100 | self.assertEqual(self._get_summary_line(_pickle.Pickler.dump), |
| 1101 | "dump(self, obj, /)") |
| 1102 | |
| 1103 | # these no longer include "self" |
| 1104 | def test_bound_python_method(self): |
| 1105 | t = textwrap.TextWrapper() |
| 1106 | self.assertEqual(self._get_summary_line(t.wrap), |
| 1107 | "wrap(text) method of textwrap.TextWrapper instance") |
Raymond Hettinger | 95801bb | 2015-08-18 22:25:16 -0700 | [diff] [blame] | 1108 | def test_field_order_for_named_tuples(self): |
| 1109 | Person = namedtuple('Person', ['nickname', 'firstname', 'agegroup']) |
| 1110 | s = pydoc.render_doc(Person) |
| 1111 | self.assertLess(s.index('nickname'), s.index('firstname')) |
| 1112 | self.assertLess(s.index('firstname'), s.index('agegroup')) |
| 1113 | |
| 1114 | class NonIterableFields: |
| 1115 | _fields = None |
| 1116 | |
| 1117 | class NonHashableFields: |
| 1118 | _fields = [[]] |
| 1119 | |
| 1120 | # Make sure these doesn't fail |
| 1121 | pydoc.render_doc(NonIterableFields) |
| 1122 | pydoc.render_doc(NonHashableFields) |
| 1123 | |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 1124 | @requires_docstrings |
| 1125 | def test_bound_builtin_method(self): |
| 1126 | s = StringIO() |
| 1127 | p = _pickle.Pickler(s) |
| 1128 | self.assertEqual(self._get_summary_line(p.dump), |
| 1129 | "dump(obj, /) method of _pickle.Pickler instance") |
| 1130 | |
| 1131 | # this should *never* include self! |
| 1132 | @requires_docstrings |
| 1133 | def test_module_level_callable(self): |
| 1134 | self.assertEqual(self._get_summary_line(os.stat), |
| 1135 | "stat(path, *, dir_fd=None, follow_symlinks=True)") |
Larry Hastings | 1abd708 | 2014-01-16 14:15:03 -0800 | [diff] [blame] | 1136 | |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1137 | @requires_docstrings |
| 1138 | def test_staticmethod(self): |
| 1139 | class X: |
| 1140 | @staticmethod |
| 1141 | def sm(x, y): |
| 1142 | '''A static method''' |
| 1143 | ... |
| 1144 | self.assertEqual(self._get_summary_lines(X.__dict__['sm']), |
| 1145 | "<staticmethod object>") |
| 1146 | self.assertEqual(self._get_summary_lines(X.sm), """\ |
| 1147 | sm(x, y) |
| 1148 | A static method |
| 1149 | """) |
| 1150 | self.assertIn(""" |
| 1151 | | Static methods defined here: |
| 1152 | |\x20\x20 |
| 1153 | | sm(x, y) |
| 1154 | | A static method |
| 1155 | """, pydoc.plain(pydoc.render_doc(X))) |
| 1156 | |
| 1157 | @requires_docstrings |
| 1158 | def test_classmethod(self): |
| 1159 | class X: |
| 1160 | @classmethod |
| 1161 | def cm(cls, x): |
| 1162 | '''A class method''' |
| 1163 | ... |
| 1164 | self.assertEqual(self._get_summary_lines(X.__dict__['cm']), |
| 1165 | "<classmethod object>") |
| 1166 | self.assertEqual(self._get_summary_lines(X.cm), """\ |
| 1167 | cm(x) method of builtins.type instance |
| 1168 | A class method |
| 1169 | """) |
| 1170 | self.assertIn(""" |
| 1171 | | Class methods defined here: |
| 1172 | |\x20\x20 |
| 1173 | | cm(x) from builtins.type |
| 1174 | | A class method |
| 1175 | """, pydoc.plain(pydoc.render_doc(X))) |
| 1176 | |
| 1177 | @requires_docstrings |
| 1178 | def test_getset_descriptor(self): |
| 1179 | # Currently these attributes are implemented as getset descriptors |
| 1180 | # in CPython. |
| 1181 | self.assertEqual(self._get_summary_line(int.numerator), "numerator") |
| 1182 | self.assertEqual(self._get_summary_line(float.real), "real") |
| 1183 | self.assertEqual(self._get_summary_line(Exception.args), "args") |
| 1184 | self.assertEqual(self._get_summary_line(memoryview.obj), "obj") |
| 1185 | |
| 1186 | @requires_docstrings |
| 1187 | def test_member_descriptor(self): |
| 1188 | # Currently these attributes are implemented as member descriptors |
| 1189 | # in CPython. |
| 1190 | self.assertEqual(self._get_summary_line(complex.real), "real") |
| 1191 | self.assertEqual(self._get_summary_line(range.start), "start") |
| 1192 | self.assertEqual(self._get_summary_line(slice.start), "start") |
| 1193 | self.assertEqual(self._get_summary_line(property.fget), "fget") |
| 1194 | self.assertEqual(self._get_summary_line(StopIteration.value), "value") |
| 1195 | |
| 1196 | @requires_docstrings |
| 1197 | def test_slot_descriptor(self): |
| 1198 | class Point: |
| 1199 | __slots__ = 'x', 'y' |
| 1200 | self.assertEqual(self._get_summary_line(Point.x), "x") |
| 1201 | |
| 1202 | @requires_docstrings |
| 1203 | def test_dict_attr_descriptor(self): |
| 1204 | class NS: |
| 1205 | pass |
| 1206 | self.assertEqual(self._get_summary_line(NS.__dict__['__dict__']), |
| 1207 | "__dict__") |
| 1208 | |
| 1209 | @requires_docstrings |
| 1210 | def test_structseq_member_descriptor(self): |
| 1211 | self.assertEqual(self._get_summary_line(type(sys.hash_info).width), |
| 1212 | "width") |
| 1213 | self.assertEqual(self._get_summary_line(type(sys.flags).debug), |
| 1214 | "debug") |
| 1215 | self.assertEqual(self._get_summary_line(type(sys.version_info).major), |
| 1216 | "major") |
| 1217 | self.assertEqual(self._get_summary_line(type(sys.float_info).max), |
| 1218 | "max") |
| 1219 | |
| 1220 | @requires_docstrings |
| 1221 | def test_namedtuple_field_descriptor(self): |
| 1222 | Box = namedtuple('Box', ('width', 'height')) |
| 1223 | self.assertEqual(self._get_summary_lines(Box.width), """\ |
| 1224 | Alias for field number 0 |
| 1225 | """) |
| 1226 | |
| 1227 | @requires_docstrings |
| 1228 | def test_property(self): |
| 1229 | class Rect: |
| 1230 | @property |
| 1231 | def area(self): |
| 1232 | '''Area of the rect''' |
| 1233 | return self.w * self.h |
| 1234 | |
| 1235 | self.assertEqual(self._get_summary_lines(Rect.area), """\ |
| 1236 | Area of the rect |
| 1237 | """) |
| 1238 | self.assertIn(""" |
| 1239 | | area |
| 1240 | | Area of the rect |
| 1241 | """, pydoc.plain(pydoc.render_doc(Rect))) |
| 1242 | |
| 1243 | @requires_docstrings |
| 1244 | def test_custom_non_data_descriptor(self): |
| 1245 | class Descr: |
| 1246 | def __get__(self, obj, cls): |
| 1247 | if obj is None: |
| 1248 | return self |
| 1249 | return 42 |
| 1250 | class X: |
| 1251 | attr = Descr() |
| 1252 | |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1253 | self.assertEqual(self._get_summary_lines(X.attr), """\ |
| 1254 | <test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>""") |
| 1255 | |
| 1256 | X.attr.__doc__ = 'Custom descriptor' |
| 1257 | self.assertEqual(self._get_summary_lines(X.attr), """\ |
Serhiy Storchaka | fbf2786 | 2020-04-15 23:00:20 +0300 | [diff] [blame] | 1258 | <test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object> |
Serhiy Storchaka | 7e64414 | 2020-04-18 17:13:21 +0300 | [diff] [blame] | 1259 | Custom descriptor |
| 1260 | """) |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1261 | |
| 1262 | X.attr.__name__ = 'foo' |
| 1263 | self.assertEqual(self._get_summary_lines(X.attr), """\ |
| 1264 | foo(...) |
| 1265 | Custom descriptor |
| 1266 | """) |
| 1267 | |
| 1268 | @requires_docstrings |
| 1269 | def test_custom_data_descriptor(self): |
| 1270 | class Descr: |
| 1271 | def __get__(self, obj, cls): |
| 1272 | if obj is None: |
| 1273 | return self |
| 1274 | return 42 |
| 1275 | def __set__(self, obj, cls): |
| 1276 | 1/0 |
| 1277 | class X: |
| 1278 | attr = Descr() |
| 1279 | |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1280 | self.assertEqual(self._get_summary_lines(X.attr), "") |
| 1281 | |
| 1282 | X.attr.__doc__ = 'Custom descriptor' |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1283 | self.assertEqual(self._get_summary_lines(X.attr), """\ |
| 1284 | Custom descriptor |
| 1285 | """) |
| 1286 | |
| 1287 | X.attr.__name__ = 'foo' |
Serhiy Storchaka | efcf82f | 2019-01-15 10:53:18 +0200 | [diff] [blame] | 1288 | self.assertEqual(self._get_summary_lines(X.attr), """\ |
| 1289 | foo |
| 1290 | Custom descriptor |
| 1291 | """) |
| 1292 | |
Dan Rose | 2a37f8f | 2019-05-24 06:38:01 -0500 | [diff] [blame] | 1293 | def test_async_annotation(self): |
| 1294 | async def coro_function(ign) -> int: |
| 1295 | return 1 |
| 1296 | |
| 1297 | text = pydoc.plain(pydoc.plaintext.document(coro_function)) |
| 1298 | self.assertIn('async coro_function', text) |
| 1299 | |
| 1300 | html = pydoc.HTMLDoc().document(coro_function) |
| 1301 | self.assertIn( |
| 1302 | 'async <a name="-coro_function"><strong>coro_function', |
| 1303 | html) |
| 1304 | |
| 1305 | def test_async_generator_annotation(self): |
| 1306 | async def an_async_generator(): |
| 1307 | yield 1 |
| 1308 | |
| 1309 | text = pydoc.plain(pydoc.plaintext.document(an_async_generator)) |
| 1310 | self.assertIn('async an_async_generator', text) |
| 1311 | |
| 1312 | html = pydoc.HTMLDoc().document(an_async_generator) |
| 1313 | self.assertIn( |
| 1314 | 'async <a name="-an_async_generator"><strong>an_async_generator', |
| 1315 | html) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1316 | |
Kirill | 61289d4 | 2019-11-13 19:13:53 +0300 | [diff] [blame] | 1317 | def test_html_for_https_links(self): |
| 1318 | def a_fn_with_https_link(): |
| 1319 | """a link https://localhost/""" |
| 1320 | pass |
| 1321 | |
| 1322 | html = pydoc.HTMLDoc().document(a_fn_with_https_link) |
| 1323 | self.assertIn( |
| 1324 | '<a href="https://localhost/">https://localhost/</a>', |
| 1325 | html |
| 1326 | ) |
| 1327 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 1328 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1329 | """Tests for pydoc._start_server""" |
| 1330 | |
| 1331 | def test_server(self): |
| 1332 | |
| 1333 | # Minimal test that starts the server, then stops it. |
| 1334 | def my_url_handler(url, content_type): |
| 1335 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 1336 | return text |
| 1337 | |
Feanil Patel | 6a396c9 | 2017-09-14 17:54:09 -0400 | [diff] [blame] | 1338 | serverthread = pydoc._start_server(my_url_handler, hostname='0.0.0.0', port=0) |
| 1339 | self.assertIn('0.0.0.0', serverthread.docserver.address) |
Senthil Kumaran | 2a42a0b | 2014-09-17 13:17:58 +0800 | [diff] [blame] | 1340 | |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 1341 | starttime = time.monotonic() |
Victor Stinner | 0d63bac | 2019-12-11 11:30:03 +0100 | [diff] [blame] | 1342 | timeout = test.support.SHORT_TIMEOUT |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1343 | |
| 1344 | while serverthread.serving: |
| 1345 | time.sleep(.01) |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 1346 | if serverthread.serving and time.monotonic() - starttime > timeout: |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1347 | serverthread.stop() |
| 1348 | break |
| 1349 | |
| 1350 | self.assertEqual(serverthread.error, None) |
| 1351 | |
| 1352 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 1353 | class PydocUrlHandlerTest(PydocBaseTest): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1354 | """Tests for pydoc._url_handler""" |
| 1355 | |
| 1356 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1357 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 1358 | self.assertRaises(TypeError, f, 'A', '') |
| 1359 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1360 | |
| 1361 | def test_url_requests(self): |
| 1362 | # Test for the correct title in the html pages returned. |
| 1363 | # This tests the different parts of the URL handler without |
| 1364 | # getting too picky about the exact html. |
| 1365 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 1366 | ("", "Pydoc: Index of Modules"), |
| 1367 | ("get?key=", "Pydoc: Index of Modules"), |
| 1368 | ("index", "Pydoc: Index of Modules"), |
| 1369 | ("topics", "Pydoc: Topics"), |
| 1370 | ("keywords", "Pydoc: Keywords"), |
| 1371 | ("pydoc", "Pydoc: module pydoc"), |
| 1372 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 1373 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 1374 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 1375 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 1376 | ("foobar", "Pydoc: Error - foobar"), |
| 1377 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1378 | ] |
| 1379 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 1380 | with self.restrict_walk_packages(): |
| 1381 | for url, title in requests: |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 1382 | self.call_url_handler(url, title) |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 1383 | |
| 1384 | path = string.__file__ |
| 1385 | title = "Pydoc: getfile " + path |
| 1386 | url = "getfile?key=" + path |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 1387 | self.call_url_handler(url, title) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1388 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1389 | |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 1390 | class TestHelper(unittest.TestCase): |
| 1391 | def test_keywords(self): |
| 1392 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 1393 | sorted(keyword.kwlist)) |
| 1394 | |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1395 | class PydocWithMetaClasses(unittest.TestCase): |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1396 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 1397 | "Docstrings are omitted with -O2 and above") |
| 1398 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 1399 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1400 | def test_DynamicClassAttribute(self): |
| 1401 | class Meta(type): |
| 1402 | def __getattr__(self, name): |
| 1403 | if name == 'ham': |
| 1404 | return 'spam' |
| 1405 | return super().__getattr__(name) |
| 1406 | class DA(metaclass=Meta): |
| 1407 | @types.DynamicClassAttribute |
| 1408 | def ham(self): |
| 1409 | return 'eggs' |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1410 | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
| 1411 | for s in expected_data_docstrings) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1412 | output = StringIO() |
| 1413 | helper = pydoc.Helper(output=output) |
| 1414 | helper(DA) |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1415 | expected_text = expected_dynamicattribute_pattern % ( |
| 1416 | (__name__,) + expected_text_data_docstrings[:2]) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1417 | result = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1418 | self.assertEqual(expected_text, result) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1419 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1420 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 1421 | "Docstrings are omitted with -O2 and above") |
| 1422 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 1423 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1424 | def test_virtualClassAttributeWithOneMeta(self): |
| 1425 | class Meta(type): |
| 1426 | def __dir__(cls): |
| 1427 | return ['__class__', '__module__', '__name__', 'LIFE'] |
| 1428 | def __getattr__(self, name): |
| 1429 | if name =='LIFE': |
| 1430 | return 42 |
| 1431 | return super().__getattr(name) |
| 1432 | class Class(metaclass=Meta): |
| 1433 | pass |
| 1434 | output = StringIO() |
| 1435 | helper = pydoc.Helper(output=output) |
| 1436 | helper(Class) |
| 1437 | expected_text = expected_virtualattribute_pattern1 % __name__ |
| 1438 | result = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1439 | self.assertEqual(expected_text, result) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1440 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1441 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 1442 | "Docstrings are omitted with -O2 and above") |
| 1443 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 1444 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1445 | def test_virtualClassAttributeWithTwoMeta(self): |
| 1446 | class Meta1(type): |
| 1447 | def __dir__(cls): |
| 1448 | return ['__class__', '__module__', '__name__', 'one'] |
| 1449 | def __getattr__(self, name): |
| 1450 | if name =='one': |
| 1451 | return 1 |
| 1452 | return super().__getattr__(name) |
| 1453 | class Meta2(type): |
| 1454 | def __dir__(cls): |
| 1455 | return ['__class__', '__module__', '__name__', 'two'] |
| 1456 | def __getattr__(self, name): |
| 1457 | if name =='two': |
| 1458 | return 2 |
| 1459 | return super().__getattr__(name) |
| 1460 | class Meta3(Meta1, Meta2): |
| 1461 | def __dir__(cls): |
| 1462 | return list(sorted(set( |
| 1463 | ['__class__', '__module__', '__name__', 'three'] + |
| 1464 | Meta1.__dir__(cls) + Meta2.__dir__(cls)))) |
| 1465 | def __getattr__(self, name): |
| 1466 | if name =='three': |
| 1467 | return 3 |
| 1468 | return super().__getattr__(name) |
| 1469 | class Class1(metaclass=Meta1): |
| 1470 | pass |
| 1471 | class Class2(Class1, metaclass=Meta3): |
| 1472 | pass |
| 1473 | fail1 = fail2 = False |
| 1474 | output = StringIO() |
| 1475 | helper = pydoc.Helper(output=output) |
| 1476 | helper(Class1) |
| 1477 | expected_text1 = expected_virtualattribute_pattern2 % __name__ |
| 1478 | result1 = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1479 | self.assertEqual(expected_text1, result1) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1480 | output = StringIO() |
| 1481 | helper = pydoc.Helper(output=output) |
| 1482 | helper(Class2) |
| 1483 | expected_text2 = expected_virtualattribute_pattern3 % __name__ |
| 1484 | result2 = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1485 | self.assertEqual(expected_text2, result2) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1486 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1487 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 1488 | "Docstrings are omitted with -O2 and above") |
| 1489 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 1490 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1491 | def test_buggy_dir(self): |
| 1492 | class M(type): |
| 1493 | def __dir__(cls): |
| 1494 | return ['__class__', '__name__', 'missing', 'here'] |
| 1495 | class C(metaclass=M): |
| 1496 | here = 'present!' |
| 1497 | output = StringIO() |
| 1498 | helper = pydoc.Helper(output=output) |
| 1499 | helper(C) |
| 1500 | expected_text = expected_missingattribute_pattern % __name__ |
| 1501 | result = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1502 | self.assertEqual(expected_text, result) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1503 | |
Serhiy Storchaka | b6076fb | 2015-04-21 21:09:48 +0300 | [diff] [blame] | 1504 | def test_resolve_false(self): |
| 1505 | # Issue #23008: pydoc enum.{,Int}Enum failed |
| 1506 | # because bool(enum.Enum) is False. |
| 1507 | with captured_stdout() as help_io: |
| 1508 | pydoc.help('enum.Enum') |
| 1509 | helptext = help_io.getvalue() |
| 1510 | self.assertIn('class Enum', helptext) |
| 1511 | |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1512 | |
Nick Coghlan | 82a9481 | 2018-04-15 21:52:57 +1000 | [diff] [blame] | 1513 | class TestInternalUtilities(unittest.TestCase): |
| 1514 | |
| 1515 | def setUp(self): |
| 1516 | tmpdir = tempfile.TemporaryDirectory() |
| 1517 | self.argv0dir = tmpdir.name |
| 1518 | self.argv0 = os.path.join(tmpdir.name, "nonexistent") |
| 1519 | self.addCleanup(tmpdir.cleanup) |
| 1520 | self.abs_curdir = abs_curdir = os.getcwd() |
| 1521 | self.curdir_spellings = ["", os.curdir, abs_curdir] |
| 1522 | |
| 1523 | def _get_revised_path(self, given_path, argv0=None): |
| 1524 | # Checking that pydoc.cli() actually calls pydoc._get_revised_path() |
| 1525 | # is handled via code review (at least for now). |
| 1526 | if argv0 is None: |
| 1527 | argv0 = self.argv0 |
| 1528 | return pydoc._get_revised_path(given_path, argv0) |
| 1529 | |
| 1530 | def _get_starting_path(self): |
Nick Coghlan | 1a5c4bd | 2018-04-15 23:32:05 +1000 | [diff] [blame] | 1531 | # Get a copy of sys.path without the current directory. |
Nick Coghlan | 82a9481 | 2018-04-15 21:52:57 +1000 | [diff] [blame] | 1532 | clean_path = sys.path.copy() |
| 1533 | for spelling in self.curdir_spellings: |
| 1534 | for __ in range(clean_path.count(spelling)): |
| 1535 | clean_path.remove(spelling) |
| 1536 | return clean_path |
| 1537 | |
| 1538 | def test_sys_path_adjustment_adds_missing_curdir(self): |
| 1539 | clean_path = self._get_starting_path() |
| 1540 | expected_path = [self.abs_curdir] + clean_path |
| 1541 | self.assertEqual(self._get_revised_path(clean_path), expected_path) |
| 1542 | |
| 1543 | def test_sys_path_adjustment_removes_argv0_dir(self): |
| 1544 | clean_path = self._get_starting_path() |
| 1545 | expected_path = [self.abs_curdir] + clean_path |
| 1546 | leading_argv0dir = [self.argv0dir] + clean_path |
| 1547 | self.assertEqual(self._get_revised_path(leading_argv0dir), expected_path) |
| 1548 | trailing_argv0dir = clean_path + [self.argv0dir] |
| 1549 | self.assertEqual(self._get_revised_path(trailing_argv0dir), expected_path) |
| 1550 | |
| 1551 | |
| 1552 | def test_sys_path_adjustment_protects_pydoc_dir(self): |
| 1553 | def _get_revised_path(given_path): |
| 1554 | return self._get_revised_path(given_path, argv0=pydoc.__file__) |
| 1555 | clean_path = self._get_starting_path() |
| 1556 | leading_argv0dir = [self.argv0dir] + clean_path |
| 1557 | expected_path = [self.abs_curdir] + leading_argv0dir |
| 1558 | self.assertEqual(_get_revised_path(leading_argv0dir), expected_path) |
| 1559 | trailing_argv0dir = clean_path + [self.argv0dir] |
| 1560 | expected_path = [self.abs_curdir] + trailing_argv0dir |
| 1561 | self.assertEqual(_get_revised_path(trailing_argv0dir), expected_path) |
| 1562 | |
| 1563 | def test_sys_path_adjustment_when_curdir_already_included(self): |
| 1564 | clean_path = self._get_starting_path() |
| 1565 | for spelling in self.curdir_spellings: |
| 1566 | with self.subTest(curdir_spelling=spelling): |
| 1567 | # If curdir is already present, no alterations are made at all |
| 1568 | leading_curdir = [spelling] + clean_path |
| 1569 | self.assertIsNone(self._get_revised_path(leading_curdir)) |
| 1570 | trailing_curdir = clean_path + [spelling] |
| 1571 | self.assertIsNone(self._get_revised_path(trailing_curdir)) |
| 1572 | leading_argv0dir = [self.argv0dir] + leading_curdir |
| 1573 | self.assertIsNone(self._get_revised_path(leading_argv0dir)) |
| 1574 | trailing_argv0dir = trailing_curdir + [self.argv0dir] |
| 1575 | self.assertIsNone(self._get_revised_path(trailing_argv0dir)) |
| 1576 | |
| 1577 | |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 1578 | @threading_helper.reap_threads |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1579 | def test_main(): |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1580 | try: |
| 1581 | test.support.run_unittest(PydocDocTest, |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 1582 | PydocImportTest, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1583 | TestDescriptions, |
| 1584 | PydocServerTest, |
| 1585 | PydocUrlHandlerTest, |
| 1586 | TestHelper, |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1587 | PydocWithMetaClasses, |
Nick Coghlan | 82a9481 | 2018-04-15 21:52:57 +1000 | [diff] [blame] | 1588 | TestInternalUtilities, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1589 | ) |
| 1590 | finally: |
| 1591 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1592 | |
| 1593 | if __name__ == "__main__": |
| 1594 | test_main() |