diff options
Diffstat (limited to 'Lib/fontTools/ttLib/ttCollection.py')
-rw-r--r-- | Lib/fontTools/ttLib/ttCollection.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Lib/fontTools/ttLib/ttCollection.py b/Lib/fontTools/ttLib/ttCollection.py index 3db4c8cd..f0922127 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,21 @@ class TTCollection(object): for i in range(header.numFonts): font = TTFont(file, fontNumber=i, _tableCache=tableCache, **kwargs) fonts.append(font) - + + # don't close file if lazy=True, as the TTFont hold a reference to the original + # file; the file will be closed once the TTFonts are closed in the + # TTCollection.close(). We still want to close the file if lazy is None or + # False, because in that case the TTFont no longer need the original file + # and we want to avoid 'ResourceWarning: unclosed file'. + if not kwargs.get("lazy") and 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() @@ -76,7 +86,7 @@ class TTCollection(object): final.write(file.getvalue()) file.close() - def saveXML(self, fileOrPath, newlinestr=None, writeVersion=True, **kwargs): + def saveXML(self, fileOrPath, newlinestr="\n", writeVersion=True, **kwargs): from fontTools.misc import xmlWriter writer = xmlWriter.XMLWriter(fileOrPath, newlinestr=newlinestr) |