Skip to content
Closed
2 changes: 1 addition & 1 deletion Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,7 @@ def interact(self):
while True:
try:
request = self.getline('help> ')
if not request: break
if not request: continue
except (KeyboardInterrupt, EOFError):
break
request = request.strip()
Comment thread
yihong0618 marked this conversation as resolved.
Outdated
Expand Down
42 changes: 42 additions & 0 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,48 @@ 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
with captured_stdout() as output:
helper = pydoc.Helper(output=output)
input_sequence = ['', 'quit']
call_count = [0]
def mock_getline(prompt):
output.write(prompt)
result = input_sequence[call_count[0]]
call_count[0] += 1
return result

with unittest.mock.patch.object(helper, 'getline', side_effect=mock_getline):
helper.interact()

output_text = output.getvalue()
prompt_count = output_text.count('help> ')
self.assertEqual(prompt_count, 2)
Comment thread
yihong0618 marked this conversation as resolved.
Outdated

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):
with captured_stdout() as output:
helper = pydoc.Helper(output=output)

call_count = [0]

def mock_getline(prompt):
output.write(prompt)
call_count[0] += 1
return quit_cmd

with unittest.mock.patch.object(helper, 'getline', side_effect=mock_getline):
helper.interact()

self.assertEqual(call_count[0], 1)
output_text = output.getvalue()
prompt_count = output_text.count('help> ')
self.assertEqual(prompt_count, 1)
Comment thread
yihong0618 marked this conversation as resolved.
Outdated


class PydocWithMetaClasses(unittest.TestCase):
def tearDown(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In :mod:`pydoc` interactive help mode to handle empty input correct as doc.
press Enter without input should continue in help session instead of exiting.
Comment thread
yihong0618 marked this conversation as resolved.
Outdated
Loading