Skip to content

Commit ad99027

Browse files
ambvyihong0618
andauthored
[3.13] gh-138568: Make help mode in PyREPL not exit on empty line input (GH-143512) (GH-143520)
(cherry picked from commit b3e4a34) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Co-authored-by: yihong0618 <zouzou0208@gmail.com>
1 parent 1de1d3d commit ad99027

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
@@ -2028,10 +2028,11 @@ def interact(self):
20282028
while True:
20292029
try:
20302030
request = self.getline('help> ')
2031-
if not request: break
20322031
except (KeyboardInterrupt, EOFError):
20332032
break
20342033
request = request.strip()
2034+
if not request:
2035+
continue # back to the prompt
20352036

20362037
# Make sure significant trailing quoting marks of literals don't
20372038
# 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
@@ -2175,10 +2175,47 @@ def test_url_requests(self):
21752175

21762176

21772177
class TestHelper(unittest.TestCase):
2178+
def mock_interactive_session(self, inputs):
2179+
"""
2180+
Given a list of inputs, run an interactive help session. Returns a string
2181+
of what would be shown on screen.
2182+
"""
2183+
input_iter = iter(inputs)
2184+
2185+
def mock_getline(prompt):
2186+
output.write(prompt)
2187+
next_input = next(input_iter)
2188+
output.write(next_input + os.linesep)
2189+
return next_input
2190+
2191+
with captured_stdout() as output:
2192+
helper = pydoc.Helper(output=output)
2193+
with unittest.mock.patch.object(helper, "getline", mock_getline):
2194+
helper.interact()
2195+
2196+
# handle different line endings across platforms consistently
2197+
return output.getvalue().strip().splitlines(keepends=False)
2198+
21782199
def test_keywords(self):
21792200
self.assertEqual(sorted(pydoc.Helper.keywords),
21802201
sorted(keyword.kwlist))
21812202

2203+
def test_interact_empty_line_continues(self):
2204+
# gh-138568: test pressing Enter without input should continue in help session
2205+
self.assertEqual(
2206+
self.mock_interactive_session(["", " ", "quit"]),
2207+
["help> ", "help> ", "help> quit"],
2208+
)
2209+
2210+
def test_interact_quit_commands_exit(self):
2211+
quit_commands = ["quit", "q", "exit"]
2212+
for quit_cmd in quit_commands:
2213+
with self.subTest(quit_command=quit_cmd):
2214+
self.assertEqual(
2215+
self.mock_interactive_session([quit_cmd]),
2216+
[f"help> {quit_cmd}"],
2217+
)
2218+
21822219

21832220
class PydocWithMetaClasses(unittest.TestCase):
21842221
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)