Skip to content

Commit bf0d723

Browse files
committed
Use _excepthook for colored exceptions
I've included `test_keyboard_interrupt_preserves_last_exception` as an invariant test, inspired by a catch in GH-157241.
1 parent ab5b8f8 commit bf0d723

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎Lib/_pyrepl/simple_interact.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,20 @@ def maybe_run_command(statement: str) -> bool:
159159
warnings.warn(f"failed to open the history file for writing: {e}")
160160

161161
input_n += 1
162-
except KeyboardInterrupt:
162+
except KeyboardInterrupt as exc:
163163
r = _get_reader()
164164
r.cmpltn_reset()
165165
if r.input_trans is r.isearch_trans:
166166
r.do_cmd(("isearch-end", [""]))
167167
r.pos = len(r.get_unicode())
168168
r.invalidate_full()
169169
r.refresh()
170-
console.write("\nKeyboardInterrupt\n")
170+
console.write("\n")
171+
console._excepthook(type(exc), exc, tb=None) # hide traceback
171172
console.resetbuffer()
172-
except MemoryError:
173-
console.write("\nMemoryError\n")
173+
except MemoryError as exc:
174+
console.write("\n")
175+
console._excepthook(type(exc), exc, tb=None) # hide traceback
174176
console.resetbuffer()
175177
except SystemExit:
176178
raise

‎Lib/test/test_pyrepl/test_pyrepl.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,39 @@ def check(output, exitcode):
23352335
output, exit_code = self.run_repl(commands, env=env)
23362336
check(output, exit_code)
23372337

2338+
def test_keyboard_interrupt_color(self):
2339+
for color in (False, True):
2340+
with self.subTest(color=color):
2341+
env = os.environ.copy()
2342+
env["PYTHON_COLORS"] = str(int(color))
2343+
output, exit_code = self.run_repl(
2344+
"unfinished\x03print(42)\nexit()\n", env=env,
2345+
)
2346+
self.assertEqual(exit_code, 0)
2347+
error = "KeyboardInterrupt"
2348+
if color:
2349+
theme = get_theme(force_color=True).traceback
2350+
error = f"{theme.type}{error}{theme.reset}"
2351+
self.assertIn(f"\r\n{error}\r\n", output)
2352+
self.assertNotIn("Traceback", output)
2353+
self.assertIn("42\r\n", output)
2354+
2355+
def test_keyboard_interrupt_preserves_last_exception(self):
2356+
commands = (
2357+
"import sys\n"
2358+
"1/0\n"
2359+
"saved = sys.last_exc\n"
2360+
"unfinished\x03"
2361+
"print('preserved:', sys.last_exc is saved)\n"
2362+
"exit()\n"
2363+
)
2364+
env = os.environ.copy()
2365+
env["PYTHON_COLORS"] = "0"
2366+
output, exit_code = self.run_repl(commands, env=env)
2367+
self.assertEqual(exit_code, 0)
2368+
self.assertIn("\r\nKeyboardInterrupt\r\n", output)
2369+
self.assertIn("preserved: True\r\n", output)
2370+
23382371
def test_not_wiping_history_file(self):
23392372
# skip, if readline module is not available
23402373
import_module('readline')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Colorize :exc:`KeyboardInterrupt` and :exc:`MemoryError` messages displayed
2+
during input handling in PyREPL. Patch by Bartosz Sławecki.

0 commit comments

Comments
 (0)