Skip to content

Commit

Permalink
pw_console: Fix progress bar division by zero
Browse files Browse the repository at this point in the history
In some cases it is possible to render a pw_console progress bar while
the elapsed time is still zero. This causes a divide by zero error:

  File
  ".../python-venv/lib/site-packages/prompt_toolkit/shortcuts/progress_bar/formatters.py",
  line 325, in <listcomp>
    len(f"{c.items_completed / c.time_elapsed.total_seconds():.2f}")
Exception float division by zero

The background on why this is an issue is that pw_console removes the
required context manager of prompt_toolkits progress bar
implementation:
https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/shortcuts/progress_bar/base.py#L6
So new runtime errors are possible depending on how the pw_console
start_progress and update_progress functions are
used. https://cs.opensource.google/pigweed/pigweed/+/main:pw_console/py/pw_console/progress_bar/__init__.py

Change-Id: Ifb81038bc838a919f72c2d8f86509bb35edb5016
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/233033
Docs-Not-Needed: Anthony DiGirolamo <[email protected]>
Pigweed-Auto-Submit: Anthony DiGirolamo <[email protected]>
Lint: Lint 🤖 <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Reviewed-by: William Abajian <[email protected]>
  • Loading branch information
AnthonyDiGirolamo authored and CQ Bot Account committed Oct 8, 2024
1 parent 2828847 commit 8bd77ab
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pw_console/py/pw_console/progress_bar/progress_bar_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from __future__ import annotations

from datetime import datetime, timedelta
import functools
from typing import (
Iterable,
Expand Down Expand Up @@ -167,5 +168,8 @@ def __call__(
remove_when_done=remove_when_done,
total=total,
)
# Ensure the elapsed time for the progress counter isn't ever
# zero by making the start time one second in the past.
counter.start_time = datetime.now() + timedelta(seconds=-1)
self.counters.append(counter)
return counter

0 comments on commit 8bd77ab

Please sign in to comment.