Use weakref on tqdm instance in DisableOnWriteError

This commit is contained in:
mariosasko 2021-07-18 23:22:32 +02:00 committed by Casper da Costa-Luis
parent 46907ea524
commit 75ccceef7f
No known key found for this signature in database
GPG Key ID: F5126E5FBD2512AD
1 changed files with 8 additions and 2 deletions

View File

@ -4,6 +4,7 @@ General helpers required for `tqdm.std`.
import os
import re
import sys
import weakref
from functools import wraps
from warnings import warn
@ -137,16 +138,21 @@ class DisableOnWriteError(ObjectWrapper):
"""
Quietly set `tqdm_instance.miniters=inf` if `func` raises `errno=5`.
"""
tqdm_instance_ref = weakref.ref(tqdm_instance)
def inner(*args, **kwargs):
tqdm_instance = tqdm_instance_ref()
try:
return func(*args, **kwargs)
except OSError as e:
if e.errno != 5:
raise
if tqdm_instance is not None:
tqdm_instance.miniters = float('inf')
except ValueError as e:
if 'closed' not in str(e):
raise
if tqdm_instance is not None:
tqdm_instance.miniters = float('inf')
return inner