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