From 75ccceef7f71d061cb70372ad2c5dc6a0284a825 Mon Sep 17 00:00:00 2001 From: mariosasko Date: Sun, 18 Jul 2021 23:22:32 +0200 Subject: [PATCH 1/2] Use weakref on tqdm instance in DisableOnWriteError --- tqdm/utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tqdm/utils.py b/tqdm/utils.py index a7352a04..4fe900b6 100644 --- a/tqdm/utils.py +++ b/tqdm/utils.py @@ -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,17 +138,22 @@ 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 - tqdm_instance.miniters = float('inf') + if tqdm_instance is not None: + tqdm_instance.miniters = float('inf') except ValueError as e: if 'closed' not in str(e): raise - tqdm_instance.miniters = float('inf') + if tqdm_instance is not None: + tqdm_instance.miniters = float('inf') return inner def __init__(self, wrapped, tqdm_instance): From 42d7b6e563bbb284070247aef57c9e812814957c Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Sat, 14 Aug 2021 21:35:51 +0100 Subject: [PATCH 2/2] slight tidy --- tqdm/utils.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tqdm/utils.py b/tqdm/utils.py index 4fe900b6..0632b8dd 100644 --- a/tqdm/utils.py +++ b/tqdm/utils.py @@ -4,9 +4,9 @@ General helpers required for `tqdm.std`. import os import re import sys -import weakref from functools import wraps from warnings import warn +from weakref import proxy # py2/3 compat try: @@ -138,22 +138,25 @@ class DisableOnWriteError(ObjectWrapper): """ Quietly set `tqdm_instance.miniters=inf` if `func` raises `errno=5`. """ - tqdm_instance_ref = weakref.ref(tqdm_instance) + tqdm_instance = proxy(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: + try: tqdm_instance.miniters = float('inf') + except ReferenceError: + pass except ValueError as e: if 'closed' not in str(e): raise - if tqdm_instance is not None: + try: tqdm_instance.miniters = float('inf') + except ReferenceError: + pass return inner def __init__(self, wrapped, tqdm_instance):