From 7f39f4df6677d9e7d1bb495986ec67323d4bdefe Mon Sep 17 00:00:00 2001 From: Alejandro Moreo <alejandro.moreo@isti.cnr.it> Date: Tue, 30 Apr 2024 09:55:28 +0200 Subject: [PATCH] file kept open in utils pickled resource, fixed --- quapy/util.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/quapy/util.py b/quapy/util.py index 7f0abc4..cd08ca2 100644 --- a/quapy/util.py +++ b/quapy/util.py @@ -228,12 +228,14 @@ def pickled_resource(pickle_path:str, generation_func:callable, *args): return generation_func(*args) else: if os.path.exists(pickle_path): - return pickle.load(open(pickle_path, 'rb')) + with open(pickle_path, 'rb') as fin: + instance = pickle.load(fin) else: instance = generation_func(*args) os.makedirs(str(Path(pickle_path).parent), exist_ok=True) - pickle.dump(instance, open(pickle_path, 'wb'), pickle.HIGHEST_PROTOCOL) - return instance + with open(pickle_path, 'wb') as foo: + pickle.dump(instance, foo, pickle.HIGHEST_PROTOCOL) + return instance def _check_sample_size(sample_size):