Skip to content

Commit 2849002

Browse files
committed
Fixed ZeroDivisionError in async_alert() when shutil.get_terminal_size().columns returned 0.
1 parent 4c5bcce commit 2849002

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.5.4 (TBD)
2+
* Bug Fixes
3+
* Fixed `ZeroDivisionError` in `async_alert()` when `shutil.get_terminal_size().columns`
4+
returned 0.
5+
16
## 2.5.3 (November 5, 2024)
27
* Enhancements
38
* Changed `CommandSet._cmd` to a read-only property which never returns `None` because it

cmd2/cmd2.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5335,9 +5335,12 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
53355335
if update_terminal:
53365336
import shutil
53375337

5338+
# Prior to Python 3.11 this can return 0, so use a fallback if needed.
5339+
terminal_columns = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH
5340+
53385341
# Print a string which replaces the onscreen prompt and input lines with the alert.
53395342
terminal_str = ansi.async_alert_str(
5340-
terminal_columns=shutil.get_terminal_size().columns,
5343+
terminal_columns=terminal_columns,
53415344
prompt=rl_get_display_prompt(),
53425345
line=readline.get_line_buffer(),
53435346
cursor_offset=rl_get_point(),

cmd2/constants.py

+3
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@
5757

5858
# custom attributes added to argparse Namespaces
5959
NS_ATTR_SUBCMD_HANDLER = '__subcmd_handler__'
60+
61+
# For cases prior to Python 3.11 when shutil.get_terminal_size().columns can return 0.
62+
DEFAULT_TERMINAL_WIDTH = 80

cmd2/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ def align_text(
874874
)
875875

876876
if width is None:
877-
width = shutil.get_terminal_size().columns
877+
# Prior to Python 3.11 this can return 0, so use a fallback if needed.
878+
width = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH
878879

879880
if width < 1:
880881
raise ValueError("width must be at least 1")

tests/test_utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import cmd2.utils as cu
1818
from cmd2 import (
1919
ansi,
20+
constants,
2021
)
2122
from cmd2.constants import (
2223
HORIZONTAL_ELLIPSIS,
@@ -624,7 +625,9 @@ def test_align_text_term_width():
624625
text = 'foo'
625626
fill_char = ' '
626627

627-
term_width = shutil.get_terminal_size().columns
628+
# Prior to Python 3.11 this can return 0, so use a fallback, so
629+
# use the same fallback that cu.align_text() does if needed.
630+
term_width = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH
628631
expected_fill = (term_width - ansi.style_aware_wcswidth(text)) * fill_char
629632

630633
aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char)

0 commit comments

Comments
 (0)