GUI mode __del__ fix (TODO: check after v5)

This commit is contained in:
Casper da Costa-Luis 2018-04-06 17:27:24 +01:00
parent 06e25b32c8
commit d7ccbbdabd
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
2 changed files with 26 additions and 36 deletions

View File

@ -421,25 +421,27 @@ class tqdm(object):
Remove from list and reposition other bars
so that newer bars won't overlap previous bars
"""
try: # in case instance was explicitly positioned, it won't be in set
with cls._lock:
with cls._lock:
try:
cls._instances.remove(instance)
except KeyError:
if not instance.gui: # pragma: no cover
raise
else:
for inst in cls._instances:
# negative `pos` means fixed
if inst.pos > abs(instance.pos):
inst.pos -= 1
# TODO: check this doesn't overwrite another fixed bar
# Kill monitor if no instances are left
if not cls._instances and cls.monitor:
try:
cls.monitor.exit()
del cls.monitor
except AttributeError: # pragma: nocover
pass
else:
cls.monitor = None
except KeyError as e:
warn(str(e), TqdmWarning)
# Kill monitor if no instances are left
if not cls._instances and cls.monitor:
try:
cls.monitor.exit()
del cls.monitor
except AttributeError: # pragma: nocover
pass
else:
cls.monitor = None
@classmethod
def write(cls, s, file=None, end="\n", nolock=False):
@ -816,8 +818,6 @@ class tqdm(object):
# not overwrite the outer progress bar
if position is None:
self.pos = self._get_free_pos(self)
elif position < 0:
raise ValueError("negative position")
else: # mark fixed positions as negative
self.pos = -position

View File

@ -989,8 +989,8 @@ def test_position():
# Artificially test nested loop printing
# Without leave
our_file = StringIO()
t = tqdm(total=2, file=our_file, miniters=1, mininterval=0, maxinterval=0,
desc='pos2 bar', leave=False, position=2)
kwargs = dict(file=our_file, miniters=1, mininterval=0, maxinterval=0)
t = tqdm(total=2, desc='pos2 bar', leave=False, position=2, **kwargs)
t.update()
t.close()
our_file.seek(0)
@ -1006,12 +1006,10 @@ def test_position():
# Test iteration-based tqdm positioning
our_file = StringIO()
for _ in trange(2, file=our_file, miniters=1, mininterval=0, maxinterval=0,
desc='pos0 bar', position=0):
for _ in trange(2, file=our_file, miniters=1, mininterval=0,
maxinterval=0, desc='pos1 bar', position=1):
for _ in trange(2, file=our_file, miniters=1, mininterval=0,
maxinterval=0, desc='pos2 bar', position=2):
kwargs["file"] = our_file
for _ in trange(2, desc='pos0 bar', position=0, **kwargs):
for _ in trange(2, desc='pos1 bar', position=1, **kwargs):
for _ in trange(2, desc='pos2 bar', position=2, **kwargs):
pass
our_file.seek(0)
out = our_file.read()
@ -1044,12 +1042,11 @@ def test_position():
# Test manual tqdm positioning
our_file = StringIO()
t1 = tqdm(total=2, file=our_file, miniters=1, mininterval=0, maxinterval=0,
desc='pos0 bar', position=0)
t2 = tqdm(total=2, file=our_file, miniters=1, mininterval=0, maxinterval=0,
desc='pos1 bar', position=1)
t3 = tqdm(total=2, file=our_file, miniters=1, mininterval=0, maxinterval=0,
desc='pos2 bar', position=2)
kwargs["file"] = our_file
kwargs["total"] = 2
t1 = tqdm(desc='pos0 bar', position=0, **kwargs)
t2 = tqdm(desc='pos1 bar', position=1, **kwargs)
t3 = tqdm(desc='pos2 bar', position=2, **kwargs)
for _ in _range(2):
t1.update()
t3.update()
@ -1110,13 +1107,6 @@ def test_position():
t3.close()
t1.close()
try:
t = tqdm(total=1, file=our_file, position=-1)
except ValueError:
pass
else:
raise ValueError("expected negative positions to raise errors")
@with_setup(pretest, posttest)
def test_set_description():