Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.

Commit 27cd097

Browse files
committed
Fix bug in cli_ui.message_for_exception
And add missing test
1 parent 49d9a82 commit 27cd097

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

cli_ui/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def message_for_exception(exception: Exception, message: str) -> Sequence[Token]
435435
"""
436436
tb = sys.exc_info()[2]
437437
buffer = io.StringIO()
438-
traceback.print_tb(tb, file=io) # type: ignore
438+
traceback.print_tb(tb, file=buffer)
439439
# fmt: off
440440
return (
441441
red, message + "\n",

cli_ui/tests/test_cli_ui.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,21 @@ def test_color_never(smart_tty: SmartTTY) -> None:
355355
cli_ui.setup(color="never")
356356
cli_ui.info(cli_ui.red, "this is red", fileobj=smart_tty)
357357
assert colorama.Fore.RED not in smart_tty.getvalue()
358+
359+
360+
def test_message_for_exception(
361+
message_recorder: MessageRecorder, dumb_tty: DumbTTY
362+
) -> None:
363+
def foo():
364+
x = 1 / 0
365+
366+
try:
367+
foo()
368+
except Exception as e:
369+
error_message_tokens = cli_ui.message_for_exception(e, "error when fooing")
370+
371+
assert error_message_tokens[:3] == (
372+
cli_ui.red,
373+
"error when fooing\n",
374+
"ZeroDivisionError",
375+
)

0 commit comments

Comments
 (0)