Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix write errors when disowned #1036

Merged
merged 5 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion tqdm/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .utils import _supports_unicode, _screen_shape_wrapper, _range, _unich, \
_term_move_up, _unicode, WeakSet, _basestring, _OrderedDict, \
Comparable, _is_ascii, FormatReplace, disp_len, disp_trim, \
SimpleTextIOWrapper, CallbackIOWrapper
SimpleTextIOWrapper, DisableOnWriteError, CallbackIOWrapper
from ._monitor import TMonitor
# native libraries
from contextlib import contextmanager
Expand Down Expand Up @@ -929,6 +929,8 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
file = SimpleTextIOWrapper(
file, encoding=getattr(file, 'encoding', None) or 'utf-8')

file = DisableOnWriteError(file, tqdm_instance=self)

if disable is None and hasattr(file, "isatty") and not file.isatty():
disable = True

Expand Down
32 changes: 16 additions & 16 deletions tqdm/tests/tests_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ def test_manual_overhead_hard():
total = int(1e5)

with closing(MockIO()) as our_file:
t = tqdm(total=total * 10, file=our_file, leave=True, miniters=1,
mininterval=0, maxinterval=0)
a = 0
with relative_timer() as time_tqdm:
for i in _range(total):
a += i
t.update(10)
with tqdm(total=total * 10, file=our_file, leave=True, miniters=1,
mininterval=0, maxinterval=0) as t:
a = 0
with relative_timer() as time_tqdm:
for i in _range(total):
a += i
t.update(10)

a = 0
with relative_timer() as time_bench:
Expand Down Expand Up @@ -334,7 +334,7 @@ def test_iter_overhead_simplebar_hard():
a += i

assert_performance(
7.5, 'trange', time_tqdm(), 'simple_progress', time_bench())
8, 'trange', time_tqdm(), 'simple_progress', time_bench())


@with_setup(pretest, posttest)
Expand All @@ -345,13 +345,13 @@ def test_manual_overhead_simplebar_hard():
total = int(1e4)

with closing(MockIO()) as our_file:
t = tqdm(total=total * 10, file=our_file, leave=True, miniters=1,
mininterval=0, maxinterval=0)
a = 0
with relative_timer() as time_tqdm:
for i in _range(total):
a += i
t.update(10)
with tqdm(total=total * 10, file=our_file, leave=True, miniters=1,
mininterval=0, maxinterval=0) as t:
a = 0
with relative_timer() as time_tqdm:
for i in _range(total):
a += i
t.update(10)

simplebar_update = simple_progress(
total=total * 10, file=our_file, leave=True, miniters=1,
Expand All @@ -363,4 +363,4 @@ def test_manual_overhead_simplebar_hard():
simplebar_update(10)

assert_performance(
7.5, 'tqdm', time_tqdm(), 'simple_progress', time_bench())
8, 'tqdm', time_tqdm(), 'simple_progress', time_bench())
31 changes: 31 additions & 0 deletions tqdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,37 @@ def __eq__(self, other):
return self._wrapped == getattr(other, '_wrapped', other)


class DisableOnWriteError(ObjectWrapper):
"""
Disable the given `tqdm_instance` upon `write()` or `flush()` errors.
"""
@staticmethod
def disable_on_exception(tqdm_instance, func):
"""
Quietly set `tqdm_instance.disable=True` 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
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))
if hasattr(wrapped, 'flush'):
self.wrapper_setattr('flush', self.disable_on_exception(
tqdm_instance, wrapped.flush))

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


class CallbackIOWrapper(ObjectWrapper):
def __init__(self, callback, stream, method="read"):
"""
Expand Down