From c08bfc1e1a45d0322fd8da22f110ec0c6190bd48 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 10 Mar 2022 18:12:23 +0000 Subject: [PATCH] ttCollection: fix 'ResourceWarning: unclosed file' Thanks Mike L. --- Lib/fontTools/ttLib/ttCollection.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/fontTools/ttLib/ttCollection.py b/Lib/fontTools/ttLib/ttCollection.py index 0cc11beac..88734ec79 100644 --- a/Lib/fontTools/ttLib/ttCollection.py +++ b/Lib/fontTools/ttLib/ttCollection.py @@ -26,8 +26,10 @@ class TTCollection(object): assert 'fontNumber' not in kwargs, kwargs + closeStream = False if not hasattr(file, "read"): file = open(file, "rb") + closeStream = True tableCache = {} if shareTables else None @@ -35,13 +37,16 @@ class TTCollection(object): for i in range(header.numFonts): font = TTFont(file, fontNumber=i, _tableCache=tableCache, **kwargs) fonts.append(font) - + + if closeStream: + file.close() + def __enter__(self): return self - + def __exit__(self, type, value, traceback): self.close() - + def close(self): for font in self.fonts: font.close()