diff --git a/tqdm/contrib/__init__.py b/tqdm/contrib/__init__.py index f0b80c2a1..8a6d2474c 100644 --- a/tqdm/contrib/__init__.py +++ b/tqdm/contrib/__init__.py @@ -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"""