8000 [3.11] gh-102541: Fix Helper.help("mod") for non-existent mod (GH-105… · python/cpython@9e33586 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 9e33586

Browse files
miss-islingtonEclips4terryjreedy
authored
[3.11] gh-102541: Fix Helper.help("mod") for non-existent mod (GH-105934) (#106323)
gh-102541: Fix Helper.help("mod") for non-existent mod (GH-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. --------- (cherry picked from commit 0530f4f) Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru> Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
1 parent f5e29f4 commit 9e33586

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
@@ -1788,7 +1788,11 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0,
17881788
raise
17891789
print(exc)
17901790
else:
1791-
output.write(render_doc(thing, title, forceload, plaintext))
1791+
try:
1792+
s = render_doc(thing, title, forceload, plaintext)
1793+
except ImportError as exc:
1794+
s = str(exc)
1795+
output.write(s)
17921796

17931797
def writedoc(thing, forceload=0):
17941798
"""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)
0