Skip to content

Commit 63f8a86

Browse files
miss-islingtonambvyihong0618
authored
[3.14] gh-138568: Make help mode in PyREPL not exit on empty line input (GH-143512) (GH-143519)
(cherry picked from commit b3e4a34) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: yihong0618 <zouzou0208@gmail.com>
1 parent fcd9500 commit 63f8a86

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Lib/pydoc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,10 +2072,11 @@ def interact(self):
20722072
while True:
20732073
try:
20742074
request = self.getline('help> ')
2075-
if not request: break
20762075
except (KeyboardInterrupt, EOFError):
20772076
break
20782077
request = request.strip()
2078+
if not request:
2079+
continue # back to the prompt
20792080

20802081
# Make sure significant trailing quoting marks of literals don't
20812082
# get deleted while cleaning input

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,10 +2188,47 @@ def test_url_requests(self):
21882188

21892189

21902190
class TestHelper(unittest.TestCase):
2191+
def mock_interactive_session(self, inputs):
2192+
"""
2193+
Given a list of inputs, run an interactive help session. Returns a string
2194+
of what would be shown on screen.
2195+
"""
2196+
input_iter = iter(inputs)
2197+
2198+
def mock_getline(prompt):
2199+
output.write(prompt)
2200+
next_input = next(input_iter)
2201+
output.write(next_input + os.linesep)
2202+
return next_input
2203+
2204+
with captured_stdout() as output:
2205+
helper = pydoc.Helper(output=output)
2206+
with unittest.mock.patch.object(helper, "getline", mock_getline):
2207+
helper.interact()
2208+
2209+
# handle different line endings across platforms consistently
2210+
return output.getvalue().strip().splitlines(keepends=False)
2211+
21912212
def test_keywords(self):
21922213
self.assertEqual(sorted(pydoc.Helper.keywords),
21932214
sorted(keyword.kwlist))
21942215

2216+
def test_interact_empty_line_continues(self):
2217+
# gh-138568: test pressing Enter without input should continue in help session
2218+
self.assertEqual(
2219+
self.mock_interactive_session(["", " ", "quit"]),
2220+
["help> ", "help> ", "help> quit"],
2221+
)
2222+
2223+
def test_interact_quit_commands_exit(self):
2224+
quit_commands = ["quit", "q", "exit"]
2225+
for quit_cmd in quit_commands:
2226+
with self.subTest(quit_command=quit_cmd):
2227+
self.assertEqual(
2228+
self.mock_interactive_session([quit_cmd]),
2229+
[f"help> {quit_cmd}"],
2230+
)
2231+
21952232

21962233
class PydocWithMetaClasses(unittest.TestCase):
21972234
def tearDown(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Adjusted the built-in :func:`help` function so that empty inputs are ignored in
2+
interactive mode.

0 commit comments

Comments
 (0)