Skip to content

Commit

Permalink
more DummyTqdmFile safety
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Feb 17, 2021
1 parent 8e139fc commit d649576
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tqdm/contrib/__init__.py
Expand Up @@ -21,15 +21,24 @@ def __init__(self, wrapped):
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"""
Expand Down

0 comments on commit d649576

Please sign in to comment.