diff --git a/tqdm/std.py b/tqdm/std.py index 217e809db..a8b701c82 100644 --- a/tqdm/std.py +++ b/tqdm/std.py @@ -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 diff --git a/tqdm/tests/tests_tqdm.py b/tqdm/tests/tests_tqdm.py index 5d344034b..42f01962b 100644 --- a/tqdm/tests/tests_tqdm.py +++ b/tqdm/tests/tests_tqdm.py @@ -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() diff --git a/tqdm/utils.py b/tqdm/utils.py index 235469c6c..9c0aa2da0 100644 --- a/tqdm/utils.py +++ b/tqdm/utils.py @@ -219,7 +219,7 @@ 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: @@ -227,7 +227,11 @@ def inner(*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):