]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-102541: Fix Helper.help("mod") for non-existent mod (GH-105934) (#106323)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 1 Jul 2023 23:15:44 +0000 (16:15 -0700)
committerGitHub <noreply@github.com>
Sat, 1 Jul 2023 23:15:44 +0000 (23:15 +0000)
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 0530f4f64629ff97f3feb7524da0833b9535e8b6)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Lib/pydoc.py
Lib/test/test_pydoc.py
Misc/NEWS.d/next/Library/2023-07-01-16-40-54.gh-issue-102541.C1ahtk.rst [new file with mode: 0644]

index 151c7e877ba5a7699e0929346bfe27ade9504d60..5e191bc1c4b2a683d4a98c662423e94e03eae24b 100755 (executable)
@@ -1788,7 +1788,11 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0,
                 raise
             print(exc)
     else:
-        output.write(render_doc(thing, title, forceload, plaintext))
+        try:
+            s = render_doc(thing, title, forceload, plaintext)
+        except ImportError as exc:
+            s = str(exc)
+        output.write(s)
 
 def writedoc(thing, forceload=0):
     """Write HTML documentation to a file in the current directory."""
index 89faf0d9141f9dead608ec86fa0b2093d4a65ba9..71be6d510adddc75c16625d1cbcd40478b25cc82 100644 (file)
@@ -631,6 +631,13 @@ class PydocDocTest(unittest.TestCase):
         # Testing that the subclasses section does not appear
         self.assertNotIn('Built-in subclasses', text)
 
+    def test_fail_help_output_redirect(self):
+        with StringIO() as buf:
+            helper = pydoc.Helper(output=buf)
+            helper.help("abd")
+            expected = missing_pattern % "abd"
+            self.assertEqual(expected, buf.getvalue().strip().replace('\n', os.linesep))
+
     @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
                      'trace function introduces __locals__ unexpectedly')
     @requires_docstrings
diff --git a/Misc/NEWS.d/next/Library/2023-07-01-16-40-54.gh-issue-102541.C1ahtk.rst b/Misc/NEWS.d/next/Library/2023-07-01-16-40-54.gh-issue-102541.C1ahtk.rst
new file mode 100644 (file)
index 0000000..efaf5db
--- /dev/null
@@ -0,0 +1 @@
+Make pydoc.doc catch bad module ImportError when output stream is not None.