wolph / python-progressbar

Progressbar 2 - A progress bar for Python 2 and Python 3 - "pip install progressbar2"
http://progressbar-2.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
857 stars 101 forks source link

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

Open dreamscached opened 1 month ago

dreamscached commented 1 month ago

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

dreamscached commented 1 month ago

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 commented 5 days ago

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

wolph commented 5 days ago

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 :)