Skip to content

Commit 7462dbb

Browse files
yihong0618adqm
andcommitted
fix: address comments
Signed-off-by: yihong0618 <[email protected]> Co-authored-by: adam j hartz <[email protected]>
1 parent 4201a09 commit 7462dbb

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

Lib/pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ def interact(self):
20592059
while True:
20602060
try:
20612061
request = self.getline('help> ')
2062-
if not request: continue
2062+
if not request.strip(): continue
20632063
except (KeyboardInterrupt, EOFError):
20642064
break
20652065
request = request.strip()

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,51 +2161,45 @@ 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+
return output.getvalue().replace(os.linesep, "\n")
2183+
21642184
def test_keywords(self):
21652185
self.assertEqual(sorted(pydoc.Helper.keywords),
21662186
sorted(keyword.kwlist))
21672187

21682188
def test_interact_empty_line_continues(self):
21692189
# gh-138568: test pressing Enter without input should continue in help session
2170-
with captured_stdout() as output:
2171-
helper = pydoc.Helper(output=output)
2172-
input_sequence = ['', 'quit']
2173-
call_count = [0]
2174-
def mock_getline(prompt):
2175-
output.write(prompt)
2176-
result = input_sequence[call_count[0]]
2177-
call_count[0] += 1
2178-
return result
2179-
2180-
with unittest.mock.patch.object(helper, 'getline', side_effect=mock_getline):
2181-
helper.interact()
2182-
2183-
output_text = output.getvalue()
2184-
prompt_count = output_text.count('help> ')
2185-
self.assertEqual(prompt_count, 2)
2190+
self.assertEqual(
2191+
self.mock_interactive_session(["", " ", "quit"]),
2192+
"\nhelp> \nhelp> \nhelp> quit\n",
2193+
)
21862194

21872195
def test_interact_quit_commands_exit(self):
2188-
quit_commands = ['quit', 'q', 'exit']
2189-
2196+
quit_commands = ["quit", "q", "exit"]
21902197
for quit_cmd in quit_commands:
21912198
with self.subTest(quit_command=quit_cmd):
2192-
with captured_stdout() as output:
2193-
helper = pydoc.Helper(output=output)
2194-
2195-
call_count = [0]
2196-
2197-
def mock_getline(prompt):
2198-
output.write(prompt)
2199-
call_count[0] += 1
2200-
return quit_cmd
2201-
2202-
with unittest.mock.patch.object(helper, 'getline', side_effect=mock_getline):
2203-
helper.interact()
2204-
2205-
self.assertEqual(call_count[0], 1)
2206-
output_text = output.getvalue()
2207-
prompt_count = output_text.count('help> ')
2208-
self.assertEqual(prompt_count, 1)
2199+
self.assertEqual(
2200+
self.mock_interactive_session([quit_cmd]),
2201+
f"\nhelp> {quit_cmd}\n",
2202+
)
22092203

22102204

22112205
class PydocWithMetaClasses(unittest.TestCase):

0 commit comments

Comments
 (0)