Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ZeroDivisionError in async_alert() when shutil.get_terminal_size().columns returned 0. #1374

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cmd2.utils as cu
from cmd2 import (
ansi,
constants,
)
from cmd2.constants import (
HORIZONTAL_ELLIPSIS,
Expand Down Expand Up @@ -624,7 +625,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
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
Loading