Skip to content

Commit

Permalink
test and fix disable when writing to closed files
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Sep 28, 2020
1 parent ea2367e commit 098745d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tqdm/std.py
Expand Up @@ -1310,7 +1310,7 @@ def update(self, n=1):

def close(self):
"""Cleanup and (if leave=False) close the progressbar."""
if self.disable:
if self.disable is True: # could be truthy yet non-True
return

# Prevent multiple closures
Expand Down
9 changes: 9 additions & 0 deletions tqdm/tests/tests_tqdm.py
Expand Up @@ -2010,3 +2010,12 @@ def test_colour():
pass
out = our_file2.getvalue()
assert '\x1b[34m' in out


@with_setup(pretest, posttest)
def test_closed():
"""Test writing to closed file"""
with closing(StringIO()) as our_file:
for i in trange(9, file=our_file, miniters=1, mininterval=0):
if i == 5:
our_file.close()
8 changes: 6 additions & 2 deletions tqdm/utils.py
Expand Up @@ -219,15 +219,19 @@ class DisableOnWriteError(ObjectWrapper):
@staticmethod
def disable_on_exception(tqdm_instance, func):
"""
Quietly set `tqdm_instance.disable=True` if `func` raises `errno=5`.
Quietly set `tqdm_instance.disable=1` if `func` raises `errno=5`.
"""
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except (IOError, OSError) as e:
if e.errno != 5:
raise
tqdm_instance.disable = True
tqdm_instance.disable = 1
except ValueError as e:
if 'closed' not in str(e):
raise
tqdm_instance.disable = 1
return inner

def __init__(self, wrapped, tqdm_instance):
Expand Down

0 comments on commit 098745d

Please sign in to comment.