more DummyTqdmFile safety

This commit is contained in:
Casper da Costa-Luis 2021-02-17 11:54:22 +00:00
parent 8e139fcf53
commit d6495760a7
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
1 changed files with 12 additions and 3 deletions

View File

@ -21,15 +21,24 @@ class DummyTqdmFile(ObjectWrapper):
self._buf = []
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)
if sep:
tqdm.write(type(nl)().join(self._buf) + pre,
file=self._wrapped, nolock=nolock)
blank = type(nl)()
tqdm.write(blank.join(self._buf + [pre, sep]),
end=blank, file=self._wrapped, nolock=nolock)
self._buf = [post]
else:
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):
"""Wraps `func()` output in a `list()` in py2"""