From 948e8a28003d910e8ff4bf18f7839356fe893cec Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Sat, 6 Sep 2025 08:29:36 +0800 Subject: [PATCH 1/2] fix: make it the same behavior in repl help mode Signed-off-by: yihong0618 --- Lib/pydoc.py | 3 +- Lib/test/test_pydoc/test_pydoc.py | 37 +++++++++++++++++++ ...-09-06-08-29-08.gh-issue-138568.iZlalC.rst | 2 + 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-09-06-08-29-08.gh-issue-138568.iZlalC.rst diff --git a/Lib/pydoc.py b/Lib/pydoc.py index d508fb70ea429e..21d556bedbd87f 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -2059,9 +2059,10 @@ def interact(self): while True: try: request = self.getline('help> ') - if not request: break except (KeyboardInterrupt, EOFError): break + if not request or request.isspace(): + continue # back to the prompt request = request.strip() # Make sure significant trailing quoting marks of literals don't diff --git a/Lib/test/test_pydoc/test_pydoc.py b/Lib/test/test_pydoc/test_pydoc.py index 3b50ead00bdd31..2e933a1f9a4bd4 100644 --- a/Lib/test/test_pydoc/test_pydoc.py +++ b/Lib/test/test_pydoc/test_pydoc.py @@ -2161,10 +2161,47 @@ def test_url_requests(self): class TestHelper(unittest.TestCase): + def mock_interactive_session(self, inputs): + """ + Given a list of inputs, run an interactive help session. Returns a string + of what would be shown on screen. + """ + input_iter = iter(inputs) + + def mock_getline(prompt): + output.write(prompt) + next_input = next(input_iter) + output.write(next_input + os.linesep) + return next_input + + with captured_stdout() as output: + helper = pydoc.Helper(output=output) + with unittest.mock.patch.object(helper, "getline", mock_getline): + helper.interact() + + # handle different line endings across platforms consistently + return output.getvalue().strip().splitlines(keepends=False) + def test_keywords(self): self.assertEqual(sorted(pydoc.Helper.keywords), sorted(keyword.kwlist)) + def test_interact_empty_line_continues(self): + # gh-138568: test pressing Enter without input should continue in help session + self.assertEqual( + self.mock_interactive_session(["", " ", "quit"]), + ["help> ", "help> ", "help> quit"], + ) + + def test_interact_quit_commands_exit(self): + quit_commands = ["quit", "q", "exit"] + for quit_cmd in quit_commands: + with self.subTest(quit_command=quit_cmd): + self.assertEqual( + self.mock_interactive_session([quit_cmd]), + [f"help> {quit_cmd}"], + ) + class PydocWithMetaClasses(unittest.TestCase): def tearDown(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-06-08-29-08.gh-issue-138568.iZlalC.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-06-08-29-08.gh-issue-138568.iZlalC.rst new file mode 100644 index 00000000000000..8a916310259c78 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-06-08-29-08.gh-issue-138568.iZlalC.rst @@ -0,0 +1,2 @@ +Adjusted the built-in :func:`help` function so that empty inputs are ignored in +interactive mode. From b0168181695d1966c04d60e110bc5817a635f8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 7 Jan 2026 12:15:32 +0100 Subject: [PATCH 2/2] Apply adam j hartz's suggestion --- Lib/pydoc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index f51c6f5cb42f16..69c83e085113c9 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -2006,9 +2006,9 @@ def interact(self): request = self.getline('help> ') except (KeyboardInterrupt, EOFError): break - if not request or request.isspace(): - continue # back to the prompt request = request.strip() + if not request: + continue # back to the prompt # Make sure significant trailing quoting marks of literals don't # get deleted while cleaning input