test_pydoc.py 56 KB

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