Skip to content

Commit

Permalink
Fixed ZeroDivisionError in async_alert() when shutil.get_terminal_siz…
Browse files Browse the repository at this point in the history
…e().columns returned 0.
  • Loading branch information
kmvanbrunt committed Nov 6, 2024
1 parent 4c5bcce commit 747f16b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.5.4 (TBD)
* Bug Fixes
* Fixed `ZeroDivisionError` in `async_alert()` when `shutil.get_terminal_size().columns`
returned 0.

## 2.5.3 (November 5, 2024)
* Enhancements
* Changed `CommandSet._cmd` to a read-only property which never returns `None` because it
Expand Down
5 changes: 4 additions & 1 deletion cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5335,9 +5335,12 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
if update_terminal:
import shutil

# Prior to Python 3.11 this can return 0, so use a fallback if needed.
terminal_columns = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH

# Print a string which replaces the onscreen prompt and input lines with the alert.
terminal_str = ansi.async_alert_str(
terminal_columns=shutil.get_terminal_size().columns,
terminal_columns=terminal_columns,
prompt=rl_get_display_prompt(),
line=readline.get_line_buffer(),
cursor_offset=rl_get_point(),
Expand Down
3 changes: 3 additions & 0 deletions cmd2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@

# custom attributes added to argparse Namespaces
NS_ATTR_SUBCMD_HANDLER = '__subcmd_handler__'

# For cases prior to Python 3.11 when shutil.get_terminal_size().columns can return 0.
DEFAULT_TERMINAL_WIDTH = 80
3 changes: 2 additions & 1 deletion cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,8 @@ def align_text(
)

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

if width < 1:
raise ValueError("width must be at least 1")
Expand Down
4 changes: 3 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ def test_align_text_term_width():
text = 'foo'
fill_char = ' '

term_width = shutil.get_terminal_size().columns
# Prior to Python 3.11 this can return 0, so use a fallback, so
# use the same fallback that cu.align_text() does if needed.
term_width = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH

Check failure on line 629 in tests/test_utils.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F821)

tests/test_utils.py:629:56: F821 Undefined name `constants`

Check failure on line 629 in tests/test_utils.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F821)

tests/test_utils.py:629:56: F821 Undefined name `constants`
expected_fill = (term_width - ansi.style_aware_wcswidth(text)) * fill_char

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

0 comments on commit 747f16b

Please sign in to comment.