Skip to content

Commit daf4dd4

Browse files
committed
Fix output format bug when exception occurs during completion
This addresses GitHub Issue #1565: #1565
1 parent 3fe9b61 commit daf4dd4

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

cmd2/cmd2.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
)
6565

6666
import rich.box
67-
from rich.console import Group, RenderableType
67+
from rich.console import Console, Group, RenderableType
6868
from rich.highlighter import ReprHighlighter
6969
from rich.rule import Rule
7070
from rich.style import Style, StyleType
@@ -1467,17 +1467,22 @@ def pwarning(
14671467
def pexcept(
14681468
self,
14691469
exception: BaseException,
1470+
*,
1471+
console: Console | None = None,
14701472
**kwargs: Any, # noqa: ARG002
14711473
) -> None:
14721474
"""Print an exception to sys.stderr.
14731475
14741476
If `debug` is true, a full traceback is also printed, if one exists.
14751477
14761478
:param exception: the exception to be printed.
1479+
:param console: optional Rich console to use for printing. If None, a new Cmd2ExceptionConsole
1480+
instance is created which writes to sys.stderr.
14771481
:param kwargs: Arbitrary keyword arguments. This allows subclasses to extend the signature of this
14781482
method and still call `super()` without encountering unexpected keyword argument errors.
14791483
"""
1480-
console = Cmd2ExceptionConsole(sys.stderr)
1484+
if console is None:
1485+
console = Cmd2ExceptionConsole(sys.stderr)
14811486

14821487
# Only print a traceback if we're in debug mode and one exists.
14831488
if self.debug and sys.exc_info() != (None, None, None):
@@ -2555,9 +2560,9 @@ def complete(
25552560
# above the prompt so it remains in the scrollback.
25562561
if ex.apply_style:
25572562
# Render the error with style to a string using Rich
2558-
console = ru.Cmd2GeneralConsole()
2559-
with console.capture() as capture:
2560-
console.print("\n" + err_str, style=Cmd2Style.ERROR)
2563+
general_console = ru.Cmd2GeneralConsole()
2564+
with general_console.capture() as capture:
2565+
general_console.print("\n" + err_str, style=Cmd2Style.ERROR)
25612566
self.completion_header = capture.get()
25622567

25632568
# Otherwise, this is a hint that should be displayed below the prompt.
@@ -2566,8 +2571,11 @@ def complete(
25662571
return None
25672572
except Exception as ex: # noqa: BLE001
25682573
# Insert a newline so the exception doesn't print in the middle of the command line being tab completed
2569-
self.perror()
2570-
self.pexcept(ex)
2574+
exception_console = ru.Cmd2ExceptionConsole()
2575+
with exception_console.capture() as capture:
2576+
exception_console.print()
2577+
self.pexcept(ex, console=exception_console)
2578+
self.completion_header = capture.get()
25712579
return None
25722580

25732581
def in_script(self) -> bool:

tests/test_completion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ def test_complete_exception(cmd2_app, capsys) -> None:
262262
begidx = endidx - len(text)
263263

264264
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
265-
_out, err = capsys.readouterr()
265+
out, _err = capsys.readouterr()
266266

267267
assert first_match is None
268-
assert "IndexError" in err
268+
assert "IndexError" in out
269269

270270

271271
def test_complete_macro(base_app, request) -> None:

0 commit comments

Comments
 (0)