Skip to content

Commit

Permalink
safer exception handling, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Sep 18, 2020
1 parent 3100e87 commit 358dab9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions tqdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,27 @@ class DisableOnWriteError(ObjectWrapper):
Disable the given `tqdm_instance` upon `write()` or `flush()` errors.
"""
@staticmethod
def disable_on_exception(tqdm_instance, func, exc):
def disable_on_exception(tqdm_instance, func):
"""
Quietly set `tqdm_instance.disable=True` if `func` raises `exc`.
Quietly set `tqdm_instance.disable=True` if `func` raises `errno=5`.
"""
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except exc:
except (IOError, OSError) as e:
if e.errno != 5:
raise
tqdm_instance.disable = True
return inner

def __init__(self, wrapped, tqdm_instance):
super(DisableOnWriteError, self).__init__(wrapped)
if hasattr(wrapped, 'write'):
self.wrapper_setattr('write', self.disable_on_exception(
tqdm_instance, wrapped.write, (IOError, OSError)))
tqdm_instance, wrapped.write))
if hasattr(wrapped, 'flush'):
self.wrapper_setattr('flush', self.disable_on_exception(
tqdm_instance, wrapped.flush, (IOError, OSError)))
tqdm_instance, wrapped.flush))

def __eq__(self, other):
return self._wrapped == getattr(other, '_wrapped', other)
Expand Down

0 comments on commit 358dab9

Please sign in to comment.