Skip to content

Commit b3e4a34

Browse files
ambvyihong0618
andauthored
gh-138568: Make help mode in PyREPL not exit on empty line input (#143512)
Signed-off-by: yihong0618 <zouzou0208@gmail.com> Co-authored-by: yihong0618 <zouzou0208@gmail.com>
1 parent a1eedae commit b3e4a34

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
@@ -2004,10 +2004,11 @@ def interact(self):
20042004
while True:
20052005
try:
20062006
request = self.getline('help> ')
2007-
if not request: break
20082007
except (KeyboardInterrupt, EOFError):
20092008
break
20102009
request = request.strip()
2010+
if not request:
2011+
continue # back to the prompt
20112012

20122013
# Make sure significant trailing quoting marks of literals don't
20132014
# 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
@@ -2147,10 +2147,47 @@ def test_url_requests(self):
21472147

21482148

21492149
class TestHelper(unittest.TestCase):
2150+
def mock_interactive_session(self, inputs):
2151+
"""
2152+
Given a list of inputs, run an interactive help session. Returns a string
2153+
of what would be shown on screen.
2154+
"""
2155+
input_iter = iter(inputs)
2156+
2157+
def mock_getline(prompt):
2158+
output.write(prompt)
2159+
next_input = next(input_iter)
2160+
output.write(next_input + os.linesep)
2161+
return next_input
2162+
2163+
with captured_stdout() as output:
2164+
helper = pydoc.Helper(output=output)
2165+
with unittest.mock.patch.object(helper, "getline", mock_getline):
2166+
helper.interact()
2167+
2168+
# handle different line endings across platforms consistently
2169+
return output.getvalue().strip().splitlines(keepends=False)
2170+
21502171
def test_keywords(self):
21512172
self.assertEqual(sorted(pydoc.Helper.keywords),
21522173
sorted(keyword.kwlist))
21532174

2175+
def test_interact_empty_line_continues(self):
2176+
# gh-138568: test pressing Enter without input should continue in help session
2177+
self.assertEqual(
2178+
self.mock_interactive_session(["", " ", "quit"]),
2179+
["help> ", "help> ", "help> quit"],
2180+
)
2181+
2182+
def test_interact_quit_commands_exit(self):
2183+
quit_commands = ["quit", "q", "exit"]
2184+
for quit_cmd in quit_commands:
2185+
with self.subTest(quit_command=quit_cmd):
2186+
self.assertEqual(
2187+
self.mock_interactive_session([quit_cmd]),
2188+
[f"help> {quit_cmd}"],
2189+
)
2190+
21542191

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