mirror of https://github.com/tqdm/tqdm.git
more DummyTqdmFile safety
This commit is contained in:
parent
8e139fcf53
commit
d6495760a7
|
@ -21,15 +21,24 @@ class DummyTqdmFile(ObjectWrapper):
|
||||||
self._buf = []
|
self._buf = []
|
||||||
|
|
||||||
def write(self, x, nolock=False):
|
def write(self, x, nolock=False):
|
||||||
nl = "\n" if isinstance(x, str) else b"\n"
|
nl = b"\n" if isinstance(x, bytes) else "\n"
|
||||||
pre, sep, post = x.rpartition(nl)
|
pre, sep, post = x.rpartition(nl)
|
||||||
if sep:
|
if sep:
|
||||||
tqdm.write(type(nl)().join(self._buf) + pre,
|
blank = type(nl)()
|
||||||
file=self._wrapped, nolock=nolock)
|
tqdm.write(blank.join(self._buf + [pre, sep]),
|
||||||
|
end=blank, file=self._wrapped, nolock=nolock)
|
||||||
self._buf = [post]
|
self._buf = [post]
|
||||||
else:
|
else:
|
||||||
self._buf.append(x)
|
self._buf.append(x)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self._buf:
|
||||||
|
blank = type(self._buf[0])()
|
||||||
|
try:
|
||||||
|
tqdm.write(blank.join(self._buf), end=blank, file=self._wrapped)
|
||||||
|
except (OSError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def builtin_iterable(func):
|
def builtin_iterable(func):
|
||||||
"""Wraps `func()` output in a `list()` in py2"""
|
"""Wraps `func()` output in a `list()` in py2"""
|
||||||
|
|
Loading…
Reference in New Issue