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 |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 14 | import test.support |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 15 | import time |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 16 | import types |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 17 | import typing |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 18 | import unittest |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 19 | import urllib.parse |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 20 | import xml.etree |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 21 | import xml.etree.ElementTree |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 22 | import textwrap |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 23 | import threading |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 24 | from io import StringIO |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 25 | from collections import namedtuple |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 26 | from test.support.script_helper import assert_python_ok |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 27 | from test.support import ( |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 28 | TESTFN, rmtree, |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 29 | reap_children, reap_threads, captured_output, captured_stdout, |
Stefan Krah | 5de3278 | 2014-01-18 23:18:39 +0100 | [diff] [blame] | 30 | captured_stderr, unlink, requires_docstrings |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 31 | ) |
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 |
| 84 | | __annotations__ = {'NO_MEANING': <class '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 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 197 | <dl><dt><strong>__annotations__</strong> = {'NO_MEANING': <class 'str'>}</dl> |
| 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" |
| 359 | dirname = os.path.dirname |
xdegaye | ebfaa71 | 2017-11-18 18:20:21 +0100 | [diff] [blame] | 360 | basedir = dirname(dirname(__file__)) |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 361 | doc = pydoc.TextDoc() |
| 362 | loc = doc.getdocloc(module, basedir=basedir) |
| 363 | return loc |
| 364 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 365 | def get_pydoc_text(module): |
| 366 | "Returns pydoc generated output as text" |
| 367 | doc = pydoc.TextDoc() |
| 368 | loc = doc.getdocloc(pydoc_mod) or "" |
| 369 | if loc: |
| 370 | loc = "\nMODULE DOCS\n " + loc + "\n" |
| 371 | |
| 372 | output = doc.docmodule(module) |
| 373 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 374 | # clean up the extra text formatting that pydoc performs |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 375 | patt = re.compile('\b.') |
| 376 | output = patt.sub('', output) |
| 377 | return output.strip(), loc |
| 378 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 379 | def get_html_title(text): |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 380 | # Bit of hack, but good enough for test purposes |
| 381 | header, _, _ = text.partition("</head>") |
| 382 | _, _, title = header.partition("<title>") |
| 383 | title, _, _ = title.partition("</title>") |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 384 | return title |
| 385 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 386 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 387 | class PydocBaseTest(unittest.TestCase): |
| 388 | |
| 389 | def _restricted_walk_packages(self, walk_packages, path=None): |
| 390 | """ |
| 391 | A version of pkgutil.walk_packages() that will restrict itself to |
| 392 | a given path. |
| 393 | """ |
| 394 | default_path = path or [os.path.dirname(__file__)] |
| 395 | def wrapper(path=None, prefix='', onerror=None): |
| 396 | return walk_packages(path or default_path, prefix, onerror) |
| 397 | return wrapper |
| 398 | |
| 399 | @contextlib.contextmanager |
| 400 | def restrict_walk_packages(self, path=None): |
| 401 | walk_packages = pkgutil.walk_packages |
| 402 | pkgutil.walk_packages = self._restricted_walk_packages(walk_packages, |
| 403 | path) |
| 404 | try: |
| 405 | yield |
| 406 | finally: |
| 407 | pkgutil.walk_packages = walk_packages |
| 408 | |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 409 | def call_url_handler(self, url, expected_title): |
| 410 | text = pydoc._url_handler(url, "text/html") |
| 411 | result = get_html_title(text) |
| 412 | # Check the title to ensure an unexpected error page was not returned |
| 413 | self.assertEqual(result, expected_title, text) |
| 414 | return text |
| 415 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 416 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 417 | class PydocDocTest(unittest.TestCase): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 418 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 419 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 420 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 421 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 422 | 'trace function introduces __locals__ unexpectedly') |
Charles-François Natali | 57398c3 | 2014-06-20 22:59:12 +0100 | [diff] [blame] | 423 | @requires_docstrings |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 424 | def test_html_doc(self): |
| 425 | result, doc_loc = get_pydoc_html(pydoc_mod) |
| 426 | mod_file = inspect.getabsfile(pydoc_mod) |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 427 | mod_url = urllib.parse.quote(mod_file) |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 428 | expected_html = expected_html_pattern % ( |
| 429 | (mod_url, mod_file, doc_loc) + |
| 430 | expected_html_data_docstrings) |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 431 | self.assertEqual(result, expected_html) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 432 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 433 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 434 | "Docstrings are omitted with -O2 and above") |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 435 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 436 | 'trace function introduces __locals__ unexpectedly') |
Charles-François Natali | 57398c3 | 2014-06-20 22:59:12 +0100 | [diff] [blame] | 437 | @requires_docstrings |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 438 | def test_text_doc(self): |
| 439 | result, doc_loc = get_pydoc_text(pydoc_mod) |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 440 | expected_text = expected_text_pattern % ( |
| 441 | (doc_loc,) + |
| 442 | expected_text_data_docstrings + |
| 443 | (inspect.getabsfile(pydoc_mod),)) |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 444 | self.assertEqual(expected_text, result) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 445 | |
Serhiy Storchaka | 056eb02 | 2014-02-19 23:05:12 +0200 | [diff] [blame] | 446 | def test_text_enum_member_with_value_zero(self): |
| 447 | # Test issue #20654 to ensure enum member with value 0 can be |
| 448 | # displayed. It used to throw KeyError: 'zero'. |
| 449 | import enum |
| 450 | class BinaryInteger(enum.IntEnum): |
| 451 | zero = 0 |
| 452 | one = 1 |
| 453 | doc = pydoc.render_doc(BinaryInteger) |
| 454 | self.assertIn('<BinaryInteger.zero: 0>', doc) |
| 455 | |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 456 | def test_mixed_case_module_names_are_lower_cased(self): |
| 457 | # issue16484 |
| 458 | doc_link = get_pydoc_link(xml.etree.ElementTree) |
| 459 | self.assertIn('xml.etree.elementtree', doc_link) |
| 460 | |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 461 | def test_issue8225(self): |
| 462 | # Test issue8225 to ensure no doc link appears for xml.etree |
| 463 | result, doc_loc = get_pydoc_text(xml.etree) |
| 464 | self.assertEqual(doc_loc, "", "MODULE DOCS incorrectly includes a link") |
| 465 | |
Benjamin Peterson | 159824e | 2014-06-07 20:14:26 -0700 | [diff] [blame] | 466 | def test_getpager_with_stdin_none(self): |
| 467 | previous_stdin = sys.stdin |
| 468 | try: |
| 469 | sys.stdin = None |
| 470 | pydoc.getpager() # Shouldn't fail. |
| 471 | finally: |
| 472 | sys.stdin = previous_stdin |
| 473 | |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 474 | def test_non_str_name(self): |
| 475 | # issue14638 |
| 476 | # Treat illegal (non-str) name like no name |
| 477 | class A: |
| 478 | __name__ = 42 |
| 479 | class B: |
| 480 | pass |
| 481 | adoc = pydoc.render_doc(A()) |
| 482 | bdoc = pydoc.render_doc(B()) |
Eric Snow | 4f29e75 | 2016-09-08 15:11:11 -0700 | [diff] [blame] | 483 | self.assertEqual(adoc.replace("A", "B"), bdoc) |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 484 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 485 | def test_not_here(self): |
| 486 | missing_module = "test.i_am_not_here" |
| 487 | result = str(run_pydoc(missing_module), 'ascii') |
| 488 | expected = missing_pattern % missing_module |
| 489 | self.assertEqual(expected, result, |
| 490 | "documentation for missing module found") |
| 491 | |
Serhiy Storchaka | 4c094e5 | 2015-03-01 15:31:36 +0200 | [diff] [blame] | 492 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 493 | 'Docstrings are omitted with -OO and above') |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 494 | def test_not_ascii(self): |
| 495 | result = run_pydoc('test.test_pydoc.nonascii', PYTHONIOENCODING='ascii') |
| 496 | encoded = nonascii.__doc__.encode('ascii', 'backslashreplace') |
| 497 | self.assertIn(encoded, result) |
| 498 | |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 499 | def test_input_strip(self): |
| 500 | missing_module = " test.i_am_not_here " |
| 501 | result = str(run_pydoc(missing_module), 'ascii') |
| 502 | expected = missing_pattern % missing_module.strip() |
| 503 | self.assertEqual(expected, result) |
| 504 | |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 505 | def test_stripid(self): |
| 506 | # test with strings, other implementations might have different repr() |
| 507 | stripid = pydoc.stripid |
| 508 | # strip the id |
| 509 | self.assertEqual(stripid('<function stripid at 0x88dcee4>'), |
| 510 | '<function stripid>') |
| 511 | self.assertEqual(stripid('<function stripid at 0x01F65390>'), |
| 512 | '<function stripid>') |
| 513 | # nothing to strip, return the same text |
| 514 | self.assertEqual(stripid('42'), '42') |
| 515 | self.assertEqual(stripid("<type 'exceptions.Exception'>"), |
| 516 | "<type 'exceptions.Exception'>") |
| 517 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 518 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 519 | 'Docstrings are omitted with -O2 and above') |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 520 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 521 | 'trace function introduces __locals__ unexpectedly') |
Charles-François Natali | 57398c3 | 2014-06-20 22:59:12 +0100 | [diff] [blame] | 522 | @requires_docstrings |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 523 | def test_help_output_redirect(self): |
| 524 | # issue 940286, if output is set in Helper, then all output from |
| 525 | # Helper.help should be redirected |
| 526 | old_pattern = expected_text_pattern |
| 527 | getpager_old = pydoc.getpager |
| 528 | getpager_new = lambda: (lambda x: x) |
| 529 | self.maxDiff = None |
| 530 | |
| 531 | buf = StringIO() |
| 532 | helper = pydoc.Helper(output=buf) |
| 533 | unused, doc_loc = get_pydoc_text(pydoc_mod) |
| 534 | module = "test.pydoc_mod" |
| 535 | help_header = """ |
| 536 | Help on module test.pydoc_mod in test: |
| 537 | |
| 538 | """.lstrip() |
| 539 | help_header = textwrap.dedent(help_header) |
| 540 | expected_help_pattern = help_header + expected_text_pattern |
| 541 | |
| 542 | pydoc.getpager = getpager_new |
| 543 | try: |
| 544 | with captured_output('stdout') as output, \ |
| 545 | captured_output('stderr') as err: |
| 546 | helper.help(module) |
| 547 | result = buf.getvalue().strip() |
Serhiy Storchaka | 9d0add0 | 2013-01-27 19:47:45 +0200 | [diff] [blame] | 548 | expected_text = expected_help_pattern % ( |
| 549 | (doc_loc,) + |
| 550 | expected_text_data_docstrings + |
| 551 | (inspect.getabsfile(pydoc_mod),)) |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 552 | self.assertEqual('', output.getvalue()) |
| 553 | self.assertEqual('', err.getvalue()) |
| 554 | self.assertEqual(expected_text, result) |
| 555 | finally: |
| 556 | pydoc.getpager = getpager_old |
| 557 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 558 | def test_namedtuple_public_underscore(self): |
| 559 | NT = namedtuple('NT', ['abc', 'def'], rename=True) |
| 560 | with captured_stdout() as help_io: |
Terry Jan Reedy | 5c81164 | 2013-11-04 21:43:26 -0500 | [diff] [blame] | 561 | pydoc.help(NT) |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 562 | helptext = help_io.getvalue() |
| 563 | self.assertIn('_1', helptext) |
| 564 | self.assertIn('_replace', helptext) |
| 565 | self.assertIn('_asdict', helptext) |
| 566 | |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 567 | def test_synopsis(self): |
| 568 | self.addCleanup(unlink, TESTFN) |
| 569 | for encoding in ('ISO-8859-1', 'UTF-8'): |
| 570 | with open(TESTFN, 'w', encoding=encoding) as script: |
| 571 | if encoding != 'UTF-8': |
| 572 | print('#coding: {}'.format(encoding), file=script) |
| 573 | print('"""line 1: h\xe9', file=script) |
| 574 | print('line 2: hi"""', file=script) |
| 575 | synopsis = pydoc.synopsis(TESTFN, {}) |
| 576 | self.assertEqual(synopsis, 'line 1: h\xe9') |
| 577 | |
Serhiy Storchaka | 4c094e5 | 2015-03-01 15:31:36 +0200 | [diff] [blame] | 578 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 579 | 'Docstrings are omitted with -OO and above') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 580 | def test_synopsis_sourceless(self): |
| 581 | expected = os.__doc__.splitlines()[0] |
| 582 | filename = os.__cached__ |
| 583 | synopsis = pydoc.synopsis(filename) |
| 584 | |
| 585 | self.assertEqual(synopsis, expected) |
| 586 | |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 587 | def test_synopsis_sourceless_empty_doc(self): |
| 588 | with test.support.temp_cwd() as test_dir: |
| 589 | init_path = os.path.join(test_dir, 'foomod42.py') |
| 590 | cached_path = importlib.util.cache_from_source(init_path) |
| 591 | with open(init_path, 'w') as fobj: |
| 592 | fobj.write("foo = 1") |
| 593 | py_compile.compile(init_path) |
| 594 | synopsis = pydoc.synopsis(init_path, {}) |
| 595 | self.assertIsNone(synopsis) |
| 596 | synopsis_cached = pydoc.synopsis(cached_path, {}) |
| 597 | self.assertIsNone(synopsis_cached) |
| 598 | |
R David Murray | 455f296 | 2013-03-19 00:00:33 -0400 | [diff] [blame] | 599 | def test_splitdoc_with_description(self): |
| 600 | example_string = "I Am A Doc\n\n\nHere is my description" |
| 601 | self.assertEqual(pydoc.splitdoc(example_string), |
| 602 | ('I Am A Doc', '\nHere is my description')) |
| 603 | |
| 604 | def test_is_object_or_method(self): |
| 605 | doc = pydoc.Doc() |
| 606 | # Bound Method |
| 607 | self.assertTrue(pydoc._is_some_method(doc.fail)) |
| 608 | # Method Descriptor |
| 609 | self.assertTrue(pydoc._is_some_method(int.__add__)) |
| 610 | # String |
| 611 | self.assertFalse(pydoc._is_some_method("I am not a method")) |
| 612 | |
| 613 | def test_is_package_when_not_package(self): |
| 614 | with test.support.temp_cwd() as test_dir: |
| 615 | self.assertFalse(pydoc.ispackage(test_dir)) |
| 616 | |
| 617 | def test_is_package_when_is_package(self): |
| 618 | with test.support.temp_cwd() as test_dir: |
| 619 | init_path = os.path.join(test_dir, '__init__.py') |
| 620 | open(init_path, 'w').close() |
| 621 | self.assertTrue(pydoc.ispackage(test_dir)) |
| 622 | os.remove(init_path) |
| 623 | |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 624 | def test_allmethods(self): |
| 625 | # issue 17476: allmethods was no longer returning unbound methods. |
| 626 | # This test is a bit fragile in the face of changes to object and type, |
| 627 | # but I can't think of a better way to do it without duplicating the |
| 628 | # logic of the function under test. |
| 629 | |
| 630 | class TestClass(object): |
| 631 | def method_returning_true(self): |
| 632 | return True |
| 633 | |
| 634 | # What we expect to get back: everything on object... |
| 635 | expected = dict(vars(object)) |
| 636 | # ...plus our unbound method... |
| 637 | expected['method_returning_true'] = TestClass.method_returning_true |
| 638 | # ...but not the non-methods on object. |
| 639 | del expected['__doc__'] |
| 640 | del expected['__class__'] |
| 641 | # inspect resolves descriptors on type into methods, but vars doesn't, |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 642 | # so we need to update __subclasshook__ and __init_subclass__. |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 643 | expected['__subclasshook__'] = TestClass.__subclasshook__ |
Nick Coghlan | d78448e | 2016-07-30 16:26:03 +1000 | [diff] [blame] | 644 | expected['__init_subclass__'] = TestClass.__init_subclass__ |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 645 | |
| 646 | methods = pydoc.allmethods(TestClass) |
| 647 | self.assertDictEqual(methods, expected) |
| 648 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 650 | class PydocImportTest(PydocBaseTest): |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 651 | |
| 652 | def setUp(self): |
| 653 | self.test_dir = os.mkdir(TESTFN) |
| 654 | self.addCleanup(rmtree, TESTFN) |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 655 | importlib.invalidate_caches() |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 656 | |
| 657 | def test_badimport(self): |
| 658 | # This tests the fix for issue 5230, where if pydoc found the module |
| 659 | # but the module had an internal import error pydoc would report no doc |
| 660 | # found. |
| 661 | modname = 'testmod_xyzzy' |
| 662 | testpairs = ( |
| 663 | ('i_am_not_here', 'i_am_not_here'), |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 664 | ('test.i_am_not_here_either', 'test.i_am_not_here_either'), |
| 665 | ('test.i_am_not_here.neither_am_i', 'test.i_am_not_here'), |
| 666 | ('i_am_not_here.{}'.format(modname), 'i_am_not_here'), |
| 667 | ('test.{}'.format(modname), 'test.{}'.format(modname)), |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 668 | ) |
| 669 | |
| 670 | sourcefn = os.path.join(TESTFN, modname) + os.extsep + "py" |
| 671 | for importstring, expectedinmsg in testpairs: |
| 672 | with open(sourcefn, 'w') as f: |
| 673 | f.write("import {}\n".format(importstring)) |
| 674 | result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii") |
| 675 | expected = badimport_pattern % (modname, expectedinmsg) |
| 676 | self.assertEqual(expected, result) |
| 677 | |
| 678 | def test_apropos_with_bad_package(self): |
| 679 | # Issue 7425 - pydoc -k failed when bad package on path |
| 680 | pkgdir = os.path.join(TESTFN, "syntaxerr") |
| 681 | os.mkdir(pkgdir) |
| 682 | badsyntax = os.path.join(pkgdir, "__init__") + os.extsep + "py" |
| 683 | with open(badsyntax, 'w') as f: |
| 684 | f.write("invalid python syntax = $1\n") |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 685 | with self.restrict_walk_packages(path=[TESTFN]): |
| 686 | with captured_stdout() as out: |
| 687 | with captured_stderr() as err: |
| 688 | pydoc.apropos('xyzzy') |
| 689 | # No result, no error |
| 690 | self.assertEqual(out.getvalue(), '') |
| 691 | self.assertEqual(err.getvalue(), '') |
| 692 | # The package name is still matched |
| 693 | with captured_stdout() as out: |
| 694 | with captured_stderr() as err: |
| 695 | pydoc.apropos('syntaxerr') |
| 696 | self.assertEqual(out.getvalue().strip(), 'syntaxerr') |
| 697 | self.assertEqual(err.getvalue(), '') |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 698 | |
| 699 | def test_apropos_with_unreadable_dir(self): |
| 700 | # Issue 7367 - pydoc -k failed when unreadable dir on path |
| 701 | self.unreadable_dir = os.path.join(TESTFN, "unreadable") |
| 702 | os.mkdir(self.unreadable_dir, 0) |
| 703 | self.addCleanup(os.rmdir, self.unreadable_dir) |
| 704 | # Note, on Windows the directory appears to be still |
| 705 | # readable so this is not really testing the issue there |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 706 | with self.restrict_walk_packages(path=[TESTFN]): |
| 707 | with captured_stdout() as out: |
| 708 | with captured_stderr() as err: |
| 709 | pydoc.apropos('SOMEKEY') |
| 710 | # No result, no error |
| 711 | self.assertEqual(out.getvalue(), '') |
| 712 | self.assertEqual(err.getvalue(), '') |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 713 | |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 714 | def test_apropos_empty_doc(self): |
| 715 | pkgdir = os.path.join(TESTFN, 'walkpkg') |
| 716 | os.mkdir(pkgdir) |
| 717 | self.addCleanup(rmtree, pkgdir) |
| 718 | init_path = os.path.join(pkgdir, '__init__.py') |
| 719 | with open(init_path, 'w') as fobj: |
| 720 | fobj.write("foo = 1") |
| 721 | current_mode = stat.S_IMODE(os.stat(pkgdir).st_mode) |
| 722 | try: |
| 723 | os.chmod(pkgdir, current_mode & ~stat.S_IEXEC) |
| 724 | with self.restrict_walk_packages(path=[TESTFN]), captured_stdout() as stdout: |
| 725 | pydoc.apropos('') |
| 726 | self.assertIn('walkpkg', stdout.getvalue()) |
| 727 | finally: |
| 728 | os.chmod(pkgdir, current_mode) |
| 729 | |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 730 | def test_url_search_package_error(self): |
| 731 | # URL handler search should cope with packages that raise exceptions |
| 732 | pkgdir = os.path.join(TESTFN, "test_error_package") |
| 733 | os.mkdir(pkgdir) |
| 734 | init = os.path.join(pkgdir, "__init__.py") |
| 735 | with open(init, "wt", encoding="ascii") as f: |
| 736 | f.write("""raise ValueError("ouch")\n""") |
| 737 | with self.restrict_walk_packages(path=[TESTFN]): |
| 738 | # Package has to be importable for the error to have any effect |
| 739 | saved_paths = tuple(sys.path) |
| 740 | sys.path.insert(0, TESTFN) |
| 741 | try: |
| 742 | with self.assertRaisesRegex(ValueError, "ouch"): |
| 743 | import test_error_package # Sanity check |
| 744 | |
| 745 | text = self.call_url_handler("search?key=test_error_package", |
| 746 | "Pydoc: Search Results") |
| 747 | found = ('<a href="test_error_package.html">' |
| 748 | 'test_error_package</a>') |
| 749 | self.assertIn(found, text) |
| 750 | finally: |
| 751 | sys.path[:] = saved_paths |
| 752 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 753 | @unittest.skip('causes undesirable side-effects (#20128)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 754 | def test_modules(self): |
| 755 | # See Helper.listmodules(). |
| 756 | num_header_lines = 2 |
| 757 | num_module_lines_min = 5 # Playing it safe. |
| 758 | num_footer_lines = 3 |
| 759 | expected = num_header_lines + num_module_lines_min + num_footer_lines |
| 760 | |
| 761 | output = StringIO() |
| 762 | helper = pydoc.Helper(output=output) |
| 763 | helper('modules') |
| 764 | result = output.getvalue().strip() |
| 765 | num_lines = len(result.splitlines()) |
| 766 | |
| 767 | self.assertGreaterEqual(num_lines, expected) |
| 768 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 769 | @unittest.skip('causes undesirable side-effects (#20128)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 770 | def test_modules_search(self): |
| 771 | # See Helper.listmodules(). |
| 772 | expected = 'pydoc - ' |
| 773 | |
| 774 | output = StringIO() |
| 775 | helper = pydoc.Helper(output=output) |
| 776 | with captured_stdout() as help_io: |
| 777 | helper('modules pydoc') |
| 778 | result = help_io.getvalue() |
| 779 | |
| 780 | self.assertIn(expected, result) |
| 781 | |
Eric Snow | a46ef70 | 2014-02-22 13:57:08 -0700 | [diff] [blame] | 782 | @unittest.skip('some buildbots are not cooperating (#20128)') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 783 | def test_modules_search_builtin(self): |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 784 | expected = 'gc - ' |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 785 | |
| 786 | output = StringIO() |
| 787 | helper = pydoc.Helper(output=output) |
| 788 | with captured_stdout() as help_io: |
Eric Snow | 5ea9750 | 2014-01-04 23:04:27 -0700 | [diff] [blame] | 789 | helper('modules garbage') |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 790 | result = help_io.getvalue() |
| 791 | |
| 792 | self.assertTrue(result.startswith(expected)) |
| 793 | |
| 794 | def test_importfile(self): |
| 795 | loaded_pydoc = pydoc.importfile(pydoc.__file__) |
| 796 | |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 797 | self.assertIsNot(loaded_pydoc, pydoc) |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 798 | self.assertEqual(loaded_pydoc.__name__, 'pydoc') |
| 799 | self.assertEqual(loaded_pydoc.__file__, pydoc.__file__) |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 800 | self.assertEqual(loaded_pydoc.__spec__, pydoc.__spec__) |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 801 | |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 802 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 803 | class TestDescriptions(unittest.TestCase): |
| 804 | |
| 805 | def test_module(self): |
| 806 | # Check that pydocfodder module can be described |
| 807 | from test import pydocfodder |
| 808 | doc = pydoc.render_doc(pydocfodder) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 809 | self.assertIn("pydocfodder", doc) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 810 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 811 | def test_class(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 812 | class C: "New-style class" |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 813 | c = C() |
| 814 | |
| 815 | self.assertEqual(pydoc.describe(C), 'class C') |
| 816 | self.assertEqual(pydoc.describe(c), 'C') |
| 817 | expected = 'C in module %s object' % __name__ |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 818 | self.assertIn(expected, pydoc.render_doc(c)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 820 | def test_typing_pydoc(self): |
| 821 | def foo(data: typing.List[typing.Any], |
| 822 | x: int) -> typing.Iterator[typing.Tuple[int, typing.Any]]: |
| 823 | ... |
| 824 | T = typing.TypeVar('T') |
| 825 | class C(typing.Generic[T], typing.Mapping[int, str]): ... |
| 826 | self.assertEqual(pydoc.render_doc(foo).splitlines()[-1], |
Dong-hee Na | 762b957 | 2017-11-16 03:30:59 +0900 | [diff] [blame] | 827 | 'f\x08fo\x08oo\x08o(data: List[Any], x: int)' |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 828 | ' -> Iterator[Tuple[int, Any]]') |
| 829 | self.assertEqual(pydoc.render_doc(C).splitlines()[2], |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 830 | 'class C\x08C(collections.abc.Mapping, typing.Generic)') |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 831 | |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 832 | def test_builtin(self): |
| 833 | for name in ('str', 'str.translate', 'builtins.str', |
| 834 | 'builtins.str.translate'): |
| 835 | # test low-level function |
| 836 | self.assertIsNotNone(pydoc.locate(name)) |
| 837 | # test high-level function |
| 838 | try: |
| 839 | pydoc.render_doc(name) |
| 840 | except ImportError: |
Terry Jan Reedy | fe928de | 2014-06-20 14:59:11 -0400 | [diff] [blame] | 841 | self.fail('finding the doc of {!r} failed'.format(name)) |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 842 | |
| 843 | for name in ('notbuiltins', 'strrr', 'strr.translate', |
| 844 | 'str.trrrranslate', 'builtins.strrr', |
| 845 | 'builtins.str.trrranslate'): |
| 846 | self.assertIsNone(pydoc.locate(name)) |
| 847 | self.assertRaises(ImportError, pydoc.render_doc, name) |
| 848 | |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 849 | @staticmethod |
| 850 | def _get_summary_line(o): |
| 851 | text = pydoc.plain(pydoc.render_doc(o)) |
| 852 | lines = text.split('\n') |
| 853 | assert len(lines) >= 2 |
| 854 | return lines[2] |
| 855 | |
| 856 | # these should include "self" |
| 857 | def test_unbound_python_method(self): |
| 858 | self.assertEqual(self._get_summary_line(textwrap.TextWrapper.wrap), |
| 859 | "wrap(self, text)") |
| 860 | |
Stefan Krah | 5de3278 | 2014-01-18 23:18:39 +0100 | [diff] [blame] | 861 | @requires_docstrings |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 862 | def test_unbound_builtin_method(self): |
| 863 | self.assertEqual(self._get_summary_line(_pickle.Pickler.dump), |
| 864 | "dump(self, obj, /)") |
| 865 | |
| 866 | # these no longer include "self" |
| 867 | def test_bound_python_method(self): |
| 868 | t = textwrap.TextWrapper() |
| 869 | self.assertEqual(self._get_summary_line(t.wrap), |
| 870 | "wrap(text) method of textwrap.TextWrapper instance") |
| 871 | |
Raymond Hettinger | 95801bb | 2015-08-18 22:25:16 -0700 | [diff] [blame] | 872 | def test_field_order_for_named_tuples(self): |
| 873 | Person = namedtuple('Person', ['nickname', 'firstname', 'agegroup']) |
| 874 | s = pydoc.render_doc(Person) |
| 875 | self.assertLess(s.index('nickname'), s.index('firstname')) |
| 876 | self.assertLess(s.index('firstname'), s.index('agegroup')) |
| 877 | |
| 878 | class NonIterableFields: |
| 879 | _fields = None |
| 880 | |
| 881 | class NonHashableFields: |
| 882 | _fields = [[]] |
| 883 | |
| 884 | # Make sure these doesn't fail |
| 885 | pydoc.render_doc(NonIterableFields) |
| 886 | pydoc.render_doc(NonHashableFields) |
| 887 | |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 888 | @requires_docstrings |
| 889 | def test_bound_builtin_method(self): |
| 890 | s = StringIO() |
| 891 | p = _pickle.Pickler(s) |
| 892 | self.assertEqual(self._get_summary_line(p.dump), |
| 893 | "dump(obj, /) method of _pickle.Pickler instance") |
| 894 | |
| 895 | # this should *never* include self! |
| 896 | @requires_docstrings |
| 897 | def test_module_level_callable(self): |
| 898 | self.assertEqual(self._get_summary_line(os.stat), |
| 899 | "stat(path, *, dir_fd=None, follow_symlinks=True)") |
Larry Hastings | 1abd708 | 2014-01-16 14:15:03 -0800 | [diff] [blame] | 900 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 901 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 902 | class PydocServerTest(unittest.TestCase): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 903 | """Tests for pydoc._start_server""" |
| 904 | |
| 905 | def test_server(self): |
| 906 | |
| 907 | # Minimal test that starts the server, then stops it. |
| 908 | def my_url_handler(url, content_type): |
| 909 | text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 910 | return text |
| 911 | |
Feanil Patel | 6a396c9 | 2017-09-14 17:54:09 -0400 | [diff] [blame] | 912 | serverthread = pydoc._start_server(my_url_handler, hostname='0.0.0.0', port=0) |
| 913 | self.assertIn('0.0.0.0', serverthread.docserver.address) |
Senthil Kumaran | 2a42a0b | 2014-09-17 13:17:58 +0800 | [diff] [blame] | 914 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 915 | starttime = time.time() |
| 916 | timeout = 1 #seconds |
| 917 | |
| 918 | while serverthread.serving: |
| 919 | time.sleep(.01) |
| 920 | if serverthread.serving and time.time() - starttime > timeout: |
| 921 | serverthread.stop() |
| 922 | break |
| 923 | |
| 924 | self.assertEqual(serverthread.error, None) |
| 925 | |
| 926 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 927 | class PydocUrlHandlerTest(PydocBaseTest): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 928 | """Tests for pydoc._url_handler""" |
| 929 | |
| 930 | def test_content_type_err(self): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 931 | f = pydoc._url_handler |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 932 | self.assertRaises(TypeError, f, 'A', '') |
| 933 | self.assertRaises(TypeError, f, 'B', 'foobar') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 934 | |
| 935 | def test_url_requests(self): |
| 936 | # Test for the correct title in the html pages returned. |
| 937 | # This tests the different parts of the URL handler without |
| 938 | # getting too picky about the exact html. |
| 939 | requests = [ |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 940 | ("", "Pydoc: Index of Modules"), |
| 941 | ("get?key=", "Pydoc: Index of Modules"), |
| 942 | ("index", "Pydoc: Index of Modules"), |
| 943 | ("topics", "Pydoc: Topics"), |
| 944 | ("keywords", "Pydoc: Keywords"), |
| 945 | ("pydoc", "Pydoc: module pydoc"), |
| 946 | ("get?key=pydoc", "Pydoc: module pydoc"), |
| 947 | ("search?key=pydoc", "Pydoc: Search Results"), |
| 948 | ("topic?key=def", "Pydoc: KEYWORD def"), |
| 949 | ("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"), |
| 950 | ("foobar", "Pydoc: Error - foobar"), |
| 951 | ("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"), |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 952 | ] |
| 953 | |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 954 | with self.restrict_walk_packages(): |
| 955 | for url, title in requests: |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 956 | self.call_url_handler(url, title) |
Antoine Pitrou | 916fc7b | 2013-05-19 15:44:54 +0200 | [diff] [blame] | 957 | |
| 958 | path = string.__file__ |
| 959 | title = "Pydoc: getfile " + path |
| 960 | url = "getfile?key=" + path |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 961 | self.call_url_handler(url, title) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 962 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 963 | |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 964 | class TestHelper(unittest.TestCase): |
| 965 | def test_keywords(self): |
| 966 | self.assertEqual(sorted(pydoc.Helper.keywords), |
| 967 | sorted(keyword.kwlist)) |
| 968 | |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 969 | class PydocWithMetaClasses(unittest.TestCase): |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 970 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 971 | "Docstrings are omitted with -O2 and above") |
| 972 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 973 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 974 | def test_DynamicClassAttribute(self): |
| 975 | class Meta(type): |
| 976 | def __getattr__(self, name): |
| 977 | if name == 'ham': |
| 978 | return 'spam' |
| 979 | return super().__getattr__(name) |
| 980 | class DA(metaclass=Meta): |
| 981 | @types.DynamicClassAttribute |
| 982 | def ham(self): |
| 983 | return 'eggs' |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 984 | expected_text_data_docstrings = tuple('\n | ' + s if s else '' |
| 985 | for s in expected_data_docstrings) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 986 | output = StringIO() |
| 987 | helper = pydoc.Helper(output=output) |
| 988 | helper(DA) |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 989 | expected_text = expected_dynamicattribute_pattern % ( |
| 990 | (__name__,) + expected_text_data_docstrings[:2]) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 991 | result = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 992 | self.assertEqual(expected_text, result) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 993 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 994 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 995 | "Docstrings are omitted with -O2 and above") |
| 996 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 997 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 998 | def test_virtualClassAttributeWithOneMeta(self): |
| 999 | class Meta(type): |
| 1000 | def __dir__(cls): |
| 1001 | return ['__class__', '__module__', '__name__', 'LIFE'] |
| 1002 | def __getattr__(self, name): |
| 1003 | if name =='LIFE': |
| 1004 | return 42 |
| 1005 | return super().__getattr(name) |
| 1006 | class Class(metaclass=Meta): |
| 1007 | pass |
| 1008 | output = StringIO() |
| 1009 | helper = pydoc.Helper(output=output) |
| 1010 | helper(Class) |
| 1011 | expected_text = expected_virtualattribute_pattern1 % __name__ |
| 1012 | result = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1013 | self.assertEqual(expected_text, result) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1014 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1015 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 1016 | "Docstrings are omitted with -O2 and above") |
| 1017 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 1018 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1019 | def test_virtualClassAttributeWithTwoMeta(self): |
| 1020 | class Meta1(type): |
| 1021 | def __dir__(cls): |
| 1022 | return ['__class__', '__module__', '__name__', 'one'] |
| 1023 | def __getattr__(self, name): |
| 1024 | if name =='one': |
| 1025 | return 1 |
| 1026 | return super().__getattr__(name) |
| 1027 | class Meta2(type): |
| 1028 | def __dir__(cls): |
| 1029 | return ['__class__', '__module__', '__name__', 'two'] |
| 1030 | def __getattr__(self, name): |
| 1031 | if name =='two': |
| 1032 | return 2 |
| 1033 | return super().__getattr__(name) |
| 1034 | class Meta3(Meta1, Meta2): |
| 1035 | def __dir__(cls): |
| 1036 | return list(sorted(set( |
| 1037 | ['__class__', '__module__', '__name__', 'three'] + |
| 1038 | Meta1.__dir__(cls) + Meta2.__dir__(cls)))) |
| 1039 | def __getattr__(self, name): |
| 1040 | if name =='three': |
| 1041 | return 3 |
| 1042 | return super().__getattr__(name) |
| 1043 | class Class1(metaclass=Meta1): |
| 1044 | pass |
| 1045 | class Class2(Class1, metaclass=Meta3): |
| 1046 | pass |
| 1047 | fail1 = fail2 = False |
| 1048 | output = StringIO() |
| 1049 | helper = pydoc.Helper(output=output) |
| 1050 | helper(Class1) |
| 1051 | expected_text1 = expected_virtualattribute_pattern2 % __name__ |
| 1052 | result1 = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1053 | self.assertEqual(expected_text1, result1) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1054 | output = StringIO() |
| 1055 | helper = pydoc.Helper(output=output) |
| 1056 | helper(Class2) |
| 1057 | expected_text2 = expected_virtualattribute_pattern3 % __name__ |
| 1058 | result2 = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1059 | self.assertEqual(expected_text2, result2) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1060 | |
Ethan Furman | 3f2f192 | 2013-10-22 07:30:24 -0700 | [diff] [blame] | 1061 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 1062 | "Docstrings are omitted with -O2 and above") |
| 1063 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 1064 | 'trace function introduces __locals__ unexpectedly') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1065 | def test_buggy_dir(self): |
| 1066 | class M(type): |
| 1067 | def __dir__(cls): |
| 1068 | return ['__class__', '__name__', 'missing', 'here'] |
| 1069 | class C(metaclass=M): |
| 1070 | here = 'present!' |
| 1071 | output = StringIO() |
| 1072 | helper = pydoc.Helper(output=output) |
| 1073 | helper(C) |
| 1074 | expected_text = expected_missingattribute_pattern % __name__ |
| 1075 | result = output.getvalue().strip() |
Raymond Hettinger | bb91c1d | 2014-06-21 12:08:22 -0700 | [diff] [blame] | 1076 | self.assertEqual(expected_text, result) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1077 | |
Serhiy Storchaka | b6076fb | 2015-04-21 21:09:48 +0300 | [diff] [blame] | 1078 | def test_resolve_false(self): |
| 1079 | # Issue #23008: pydoc enum.{,Int}Enum failed |
| 1080 | # because bool(enum.Enum) is False. |
| 1081 | with captured_stdout() as help_io: |
| 1082 | pydoc.help('enum.Enum') |
| 1083 | helptext = help_io.getvalue() |
| 1084 | self.assertIn('class Enum', helptext) |
| 1085 | |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 1086 | |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1087 | @reap_threads |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1088 | def test_main(): |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1089 | try: |
| 1090 | test.support.run_unittest(PydocDocTest, |
Ned Deily | 92a81a1 | 2011-10-06 14:19:03 -0700 | [diff] [blame] | 1091 | PydocImportTest, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1092 | TestDescriptions, |
| 1093 | PydocServerTest, |
| 1094 | PydocUrlHandlerTest, |
| 1095 | TestHelper, |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1096 | PydocWithMetaClasses, |
Antoine Pitrou | a6e81a2 | 2011-07-15 22:32:25 +0200 | [diff] [blame] | 1097 | ) |
| 1098 | finally: |
| 1099 | reap_children() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1100 | |
| 1101 | if __name__ == "__main__": |
| 1102 | test_main() |