From 61b116cea45911d3aa7e51c06d1b15942572b5ee Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Wed, 25 Apr 2018 18:19:12 +0100 Subject: [PATCH] fix AttributeError 'fp' for `tqdm.write()` - fixes #383 --- tqdm/_tqdm.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index 098a2fab..83e5e084 100755 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -483,16 +483,14 @@ class tqdm(object): # Clear instance if in the target output file # or if write output + tqdm output are both either # sys.stdout or sys.stderr (because both are mixed in terminal) - if inst.fp == fp or all( - f in (sys.stdout, sys.stderr) for f in (fp, inst.fp)): + if hasattr(inst, "start_t") and (inst.fp == fp or all( + f in (sys.stdout, sys.stderr) for f in (fp, inst.fp))): inst.clear(nolock=True) inst_cleared.append(inst) yield # Force refresh display of bars we cleared for inst in inst_cleared: - # Avoid race conditions by checking that the instance started - if hasattr(inst, 'start_t'): # pragma: nocover - inst.refresh(nolock=True) + inst.refresh(nolock=True) if not nolock: cls._lock.release() @@ -843,10 +841,10 @@ class tqdm(object): # if nested, at initial sp() call we replace '\r' by '\n' to # not overwrite the outer progress bar with self._lock: - if position is None: - self.pos = self._get_free_pos(self) - else: # mark fixed positions as negative - self.pos = -position + if position is None: + self.pos = self._get_free_pos(self) + else: # mark fixed positions as negative + self.pos = -position if not gui: # Initialize the screen printer