Skip to content

Commit 0530f4f

Browse files
Eclips4terryjreedy
andauthored
gh-102541: Fix Helper.help("mod") for non-existent mod (#105934)
If the output arg to Helper() is a stream rather than the default None, which means 'page to stdout', the ImportError from pydoc.resolve is currently not caught in pydoc.doc. The same error is caught when output is None. --------- Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent 46d7761 commit 0530f4f

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/pydoc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,11 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0,
17901790
raise
17911791
print(exc)
17921792
else:
1793-
output.write(render_doc(thing, title, forceload, plaintext))
1793+
try:
1794+
s = render_doc(thing, title, forceload, plaintext)
1795+
except ImportError as exc:
1796+
s = str(exc)
1797+
output.write(s)
17941798

17951799
def writedoc(thing, forceload=0):
17961800
"""Write HTML documentation to a file in the current directory."""

Lib/test/test_pydoc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ def test_builtin_on_metaclasses(self):
631631
# Testing that the subclasses section does not appear
632632
self.assertNotIn('Built-in subclasses', text)
633633

634+
def test_fail_help_output_redirect(self):
635+
with StringIO() as buf:
636+
helper = pydoc.Helper(output=buf)
637+
helper.help("abd")
638+
expected = missing_pattern % "abd"
639+
self.assertEqual(expected, buf.getvalue().strip().replace('\n', os.linesep))
640+
634641
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
635642
'trace function introduces __locals__ unexpectedly')
636643
@requires_docstrings
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make pydoc.doc catch bad module ImportError when output stream is not None.

0 commit comments

Comments
 (0)