Skip to content

Commit 948e8a2

Browse files
yihong0618ambv
authored andcommitted
fix: make it the same behavior in repl help mode
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent bde1291 commit 948e8a2

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
@@ -2059,9 +2059,10 @@ def interact(self):
20592059
while True:
20602060
try:
20612061
request = self.getline('help> ')
2062-
if not request: break
20632062
except (KeyboardInterrupt, EOFError):
20642063
break
2064+
if not request or request.isspace():
2065+
continue # back to the prompt
20652066
request = request.strip()
20662067

20672068
# Make sure significant trailing quoting marks of literals don't

Lib/test/test_pydoc/test_pydoc.py

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

21622162

21632163
class TestHelper(unittest.TestCase):
2164+
def mock_interactive_session(self, inputs):
2165+
"""
2166+
Given a list of inputs, run an interactive help session. Returns a string
2167+
of what would be shown on screen.
2168+
"""
2169+
input_iter = iter(inputs)
2170+
2171+
def mock_getline(prompt):
2172+
output.write(prompt)
2173+
next_input = next(input_iter)
2174+
output.write(next_input + os.linesep)
2175+
return next_input
2176+
2177+
with captured_stdout() as output:
2178+
helper = pydoc.Helper(output=output)
2179+
with unittest.mock.patch.object(helper, "getline", mock_getline):
2180+
helper.interact()
2181+
2182+
# handle different line endings across platforms consistently
2183+
return output.getvalue().strip().splitlines(keepends=False)
2184+
21642185
def test_keywords(self):
21652186
self.assertEqual(sorted(pydoc.Helper.keywords),
21662187
sorted(keyword.kwlist))
21672188

2189+
def test_interact_empty_line_continues(self):
2190+
# gh-138568: test pressing Enter without input should continue in help session
2191+
self.assertEqual(
2192+
self.mock_interactive_session(["", " ", "quit"]),
2193+
["help> ", "help> ", "help> quit"],
2194+
)
2195+
2196+
def test_interact_quit_commands_exit(self):
2197+
quit_commands = ["quit", "q", "exit"]
2198+
for quit_cmd in quit_commands:
2199+
with self.subTest(quit_command=quit_cmd):
2200+
self.assertEqual(
2201+
self.mock_interactive_session([quit_cmd]),
2202+
[f"help> {quit_cmd}"],
2203+
)
2204+
21682205

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