Skip to content

Commit 8868d48

Browse files
authored
bpo-44967: pydoc: return non-zero exit code when query is not found (GH-27868)
1 parent 21fa854 commit 8868d48

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

Lib/pydoc.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -1775,24 +1775,18 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
17751775
def doc(thing, title='Python Library Documentation: %s', forceload=0,
17761776
output=None):
17771777
"""Display text documentation, given an object or a path to an object."""
1778-
try:
1779-
if output is None:
1780-
pager(render_doc(thing, title, forceload))
1781-
else:
1782-
output.write(render_doc(thing, title, forceload, plaintext))
1783-
except (ImportError, ErrorDuringImport) as value:
1784-
print(value)
1778+
if output is None:
1779+
pager(render_doc(thing, title, forceload))
1780+
else:
1781+
output.write(render_doc(thing, title, forceload, plaintext))
17851782

17861783
def writedoc(thing, forceload=0):
17871784
"""Write HTML documentation to a file in the current directory."""
1788-
try:
1789-
object, name = resolve(thing, forceload)
1790-
page = html.page(describe(object), html.document(object, name))
1791-
with open(name + '.html', 'w', encoding='utf-8') as file:
1792-
file.write(page)
1793-
print('wrote', name + '.html')
1794-
except (ImportError, ErrorDuringImport) as value:
1795-
print(value)
1785+
object, name = resolve(thing, forceload)
1786+
page = html.page(describe(object), html.document(object, name))
1787+
with open(name + '.html', 'w', encoding='utf-8') as file:
1788+
file.write(page)
1789+
print('wrote', name + '.html')
17961790

17971791
def writedocs(dir, pkgpath='', done=None):
17981792
"""Write out HTML documentation for all modules in a directory tree."""
@@ -2786,7 +2780,7 @@ class BadUsage(Exception): pass
27862780
for arg in args:
27872781
if ispath(arg) and not os.path.exists(arg):
27882782
print('file %r does not exist' % arg)
2789-
break
2783+
sys.exit(1)
27902784
try:
27912785
if ispath(arg) and os.path.isfile(arg):
27922786
arg = importfile(arg)
@@ -2797,8 +2791,9 @@ class BadUsage(Exception): pass
27972791
writedoc(arg)
27982792
else:
27992793
help.help(arg)
2800-
except ErrorDuringImport as value:
2794+
except (ImportError, ErrorDuringImport) as value:
28012795
print(value)
2796+
sys.exit(1)
28022797

28032798
except (getopt.error, BadUsage):
28042799
cmd = os.path.splitext(os.path.basename(sys.argv[0]))[0]

Lib/test/test_pydoc.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from io import StringIO
2525
from collections import namedtuple
2626
from test.support import os_helper
27-
from test.support.script_helper import assert_python_ok
27+
from test.support.script_helper import assert_python_ok, assert_python_failure
2828
from test.support import threading_helper
2929
from test.support import (reap_children, captured_output, captured_stdout,
3030
captured_stderr, requires_docstrings)
@@ -345,6 +345,14 @@ def run_pydoc(module_name, *args, **env):
345345
rc, out, err = assert_python_ok('-B', pydoc.__file__, *args, **env)
346346
return out.strip()
347347

348+
def run_pydoc_fail(module_name, *args, **env):
349+
"""
350+
Runs pydoc on the specified module expecting a failure.
351+
"""
352+
args = args + (module_name,)
353+
rc, out, err = assert_python_failure('-B', pydoc.__file__, *args, **env)
354+
return out.strip()
355+
348356
def get_pydoc_html(module):
349357
"Returns pydoc generated output as html"
350358
doc = pydoc.HTMLDoc()
@@ -487,7 +495,7 @@ class B:
487495

488496
def test_not_here(self):
489497
missing_module = "test.i_am_not_here"
490-
result = str(run_pydoc(missing_module), 'ascii')
498+
result = str(run_pydoc_fail(missing_module), 'ascii')
491499
expected = missing_pattern % missing_module
492500
self.assertEqual(expected, result,
493501
"documentation for missing module found")
@@ -501,7 +509,7 @@ def test_not_ascii(self):
501509

502510
def test_input_strip(self):
503511
missing_module = " test.i_am_not_here "
504-
result = str(run_pydoc(missing_module), 'ascii')
512+
result = str(run_pydoc_fail(missing_module), 'ascii')
505513
expected = missing_pattern % missing_module.strip()
506514
self.assertEqual(expected, result)
507515

@@ -902,7 +910,7 @@ def test_badimport(self):
902910
for importstring, expectedinmsg in testpairs:
903911
with open(sourcefn, 'w') as f:
904912
f.write("import {}\n".format(importstring))
905-
result = run_pydoc(modname, PYTHONPATH=TESTFN).decode("ascii")
913+
result = run_pydoc_fail(modname, PYTHONPATH=TESTFN).decode("ascii")
906914
expected = badimport_pattern % (modname, expectedinmsg)
907915
self.assertEqual(expected, result)
908916

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pydoc now returns a non-zero status code when a module cannot be found.

0 commit comments

Comments
 (0)