python-progressbar icon indicating copy to clipboard operation
python-progressbar copied to clipboard

Progress bar is duplicated when used both as a context manager and as an iterable wrapper

Open dreamscached opened this issue 1 year ago • 3 comments

Description

When used both as context manager (e.g. with) and as iterable wrapper, exhausting the iterable and exiting the with scope causes the progress bar to duplicate.

Code

Consider the following example code:

from time import sleep
from progressbar import ProgressBar
from progressbar.widgets import AnimatedMarker, SimpleProgress, GranularBar, Timer

def simple_iterator():
    for i in range(100):
        sleep(0.02)
        yield i

progress = ProgressBar(
    max_value=100,
    redirect_stdout=True,
    redirect_stderr=True,
    widgets=[AnimatedMarker("⡏⠟⠻⢹⣸⣴⣦⣇"), " ",
             SimpleProgress(), " ",
             GranularBar(markers="░▒▓█"), " ",
             Timer()]
)

with progress:
    for i in progress(simple_iterator()):
        pass

ASCIInema demo: https://asciinema.org/a/UEEc81s5GyrAR3QaqLDWGIMGX

Versions

  • Python version: 3.12.6 (main, Sep 8 2024, 13:18:56) [GCC 14.2.1 20240805]
  • Python distribution/environment: CPython
  • Operating System: Arch Linux, etc.
  • Package version: 4.5.0

dreamscached avatar Sep 22 '24 16:09 dreamscached

I assume finish is invoked twice for some reason, because leaving in either a context manager only or iterable wrapper only, it works as expected.

dreamscached avatar Sep 22 '24 16:09 dreamscached

Bumping because it's still an issue and was not solved.

dreamscached avatar Oct 30 '24 07:10 dreamscached

Bumping because it's still an issue and was not solved.

Sorry about he inactivity, I've seen the issue but I've had very little time.

It's definitely a bug :)

wolph avatar Oct 30 '24 09:10 wolph