From e6fb90b43120c46cd634d4caa8cf2ff30dfc9a2e Mon Sep 17 00:00:00 2001 From: Eclips4 Date: Tue, 20 Jun 2023 11:02:19 +0300 Subject: [PATCH 1/7] gh-102541: Fix Helper.help when output is non default --- Lib/pydoc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index d69369d4196eaa..245ed695dfc670 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1790,7 +1790,10 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0, raise print(exc) else: - output.write(render_doc(thing, title, forceload, plaintext)) + try: + output.write(render_doc(thing, title, forceload, plaintext)) + except ImportError as exc: + output.write(str(exc)) def writedoc(thing, forceload=0): """Write HTML documentation to a file in the current directory.""" From c25776c0c7fcee5fb6072e66cbb74f54dc0cd876 Mon Sep 17 00:00:00 2001 From: Eclips4 Date: Tue, 20 Jun 2023 11:23:51 +0300 Subject: [PATCH 2/7] Add test --- Lib/test/test_pydoc.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index cefc71cb5a7f54..5e7122a598f3c5 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -631,6 +631,14 @@ def test_builtin_on_metaclasses(self): # Testing that the subclasses section does not appear self.assertNotIn('Built-in subclasses', text) + def test_fail_help_output_redirect(self): + buf = StringIO() + helper = pydoc.Helper(output=buf) + helper.help("abd") + expected = missing_pattern % "abd" + self.assertEqual(expected, buf.getvalue().strip().replace('\n', os.linesep)) + buf.close() + @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), 'trace function introduces __locals__ unexpectedly') @requires_docstrings From 296c0baf04b4cd02df0aad6fc7a30a5c9e957f45 Mon Sep 17 00:00:00 2001 From: Eclips4 Date: Tue, 20 Jun 2023 13:50:25 +0300 Subject: [PATCH 3/7] Rewrite test using the `with` keyword --- Lib/test/test_pydoc.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index 5e7122a598f3c5..115ffd3c29d4d6 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -632,12 +632,11 @@ def test_builtin_on_metaclasses(self): self.assertNotIn('Built-in subclasses', text) def test_fail_help_output_redirect(self): - buf = StringIO() - helper = pydoc.Helper(output=buf) - helper.help("abd") - expected = missing_pattern % "abd" - self.assertEqual(expected, buf.getvalue().strip().replace('\n', os.linesep)) - buf.close() + 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') From c597019217b31fab0022d8a5e8c264a41cba1442 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 1 Jul 2023 16:34:54 -0400 Subject: [PATCH 4/7] Update Lib/pydoc.py --- Lib/pydoc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 245ed695dfc670..1d1638c0e20caf 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1791,9 +1791,10 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0, print(exc) else: try: - output.write(render_doc(thing, title, forceload, plaintext)) + s = render_doc(thing, title, forceload, plaintext)) except ImportError as exc: - output.write(str(exc)) + s = str(exc)) + output.write(s) def writedoc(thing, forceload=0): """Write HTML documentation to a file in the current directory.""" From 7b117ac3ed0face47630d08af27b4a2ace61df2a Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 1 Jul 2023 16:41:36 -0400 Subject: [PATCH 5/7] blurb --- .../next/Library/2023-07-01-16-40-54.gh-issue-102541.C1ahtk.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-01-16-40-54.gh-issue-102541.C1ahtk.rst 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 index 00000000000000..efaf5db10f3e1c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-01-16-40-54.gh-issue-102541.C1ahtk.rst @@ -0,0 +1 @@ +Make pydoc.doc catch bad module ImportError when output stream is not None. From f4eb39d0abe97a46f84f71fa1f41247bb8fed2d2 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 1 Jul 2023 17:05:59 -0400 Subject: [PATCH 6/7] Update pydoc.py --- Lib/pydoc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 1d1638c0e20caf..f93931662af780 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1791,7 +1791,7 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0, print(exc) else: try: - s = render_doc(thing, title, forceload, plaintext)) + s = render_doc(thing, title, forceload, plaintext) except ImportError as exc: s = str(exc)) output.write(s) From 8868e954fd20168d20c702039ca5b55ddbc48b5e Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 1 Jul 2023 18:11:38 -0400 Subject: [PATCH 7/7] Update pydoc.py --- Lib/pydoc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index f93931662af780..185f09e603df2e 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1793,7 +1793,7 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0, try: s = render_doc(thing, title, forceload, plaintext) except ImportError as exc: - s = str(exc)) + s = str(exc) output.write(s) def writedoc(thing, forceload=0):