Skip to content

Commit b3055ab

Browse files
committed
Avoid exiting the new REPL when there are non-string candidates for suggestions.
1 parent a3990df commit b3055ab

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

Lib/_pyrepl/console.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,14 @@ def showsyntaxerror(self, filename=None, **kwargs):
169169

170170
def _excepthook(self, typ, value, tb):
171171
import traceback
172-
lines = traceback.format_exception(
173-
typ, value, tb,
174-
colorize=self.can_colorize,
175-
limit=traceback.BUILTIN_EXCEPTION_LIMIT)
176-
self.write(''.join(lines))
172+
try:
173+
lines = traceback.format_exception(
174+
typ, value, tb,
175+
colorize=self.can_colorize,
176+
limit=traceback.BUILTIN_EXCEPTION_LIMIT)
177+
self.write(''.join(lines))
178+
except TypeError as e:
179+
self.write(repr(e) + '\n')
177180

178181
def runcode(self, code):
179182
try:

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,15 @@ def test_null_byte(self):
13181318
self.assertEqual(exit_code, 0)
13191319
self.assertNotIn("TypeError", output)
13201320

1321+
def test_non_string_suggestion_candidates(self):
1322+
commands = ("import runpy\n"
1323+
"runpy._run_module_code('blech', {0: '', 'bluch': ''}, '')\n"
1324+
"exit()\n")
1325+
1326+
output, exit_code = self.run_repl(commands)
1327+
self.assertEqual(exit_code, 0)
1328+
self.assertIn("all elements in 'candidates' must be strings", output)
1329+
13211330
def test_readline_history_file(self):
13221331
# skip, if readline module is not available
13231332
readline = import_module('readline')

0 commit comments

Comments
 (0)