Skip to content

stdout isnt doesn't unwraped when interupted #212

Open
@ghost

Description

When redirect_stdout is beign used, and the iterator is interrupted, for example by a KeyboardInterrupt, stdout remains wrapped,

Example code

import progressbar 
import time 
for i in progressbar.progressbar(range(100), redirect_stdout=True): 
    print('Some text', i) 
    time.sleep(0.1) 
    if i == 50:
        raise KeyboardInterrupt

You'll end up with the 50% completed progress bar sitting on your terminal and refusing to leave.

When an exception stops the progress of the iterator, the progress bar doesn't take notice and unwrap stdout, as it does when finished() is called.

Manually wrapping the iteration in a try:except: works, but obviously isn't very nice

import progressbar 
import time 
bar = progressbar.bar.ProgressBar(redirect_stdout=True) 
try:
    for i in bar(range(100)): 
        print('Some text', i) 
        time.sleep(0.1) 
        if i==50: 
            raise KeyboardInterrupt 
except:
    bar.finish(dirty=True) 
    raise

Versions

Python 3.7.3, IPython 7.8.0, Linux, progressbar version 3.47.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions