Merge pull request #2549 from fonttools/fix-lazy-collection

fix opening TTCollection with lazy=True
This commit is contained in:
Cosimo Lupo 2022-03-17 12:43:24 +00:00 committed by GitHub
commit ac21636137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -38,7 +38,12 @@ class TTCollection(object):
font = TTFont(file, fontNumber=i, _tableCache=tableCache, **kwargs)
fonts.append(font)
if closeStream:
# 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):

View File

@ -0,0 +1,27 @@
import os
from pathlib import Path
from fontTools.ttLib import TTCollection
import pytest
TTX_DATA_DIR = Path(__file__).parent.parent / "ttx" / "data"
@pytest.fixture(params=[None, True, False])
def lazy(request):
return request.param
def test_lazy_open_path(lazy):
ttc_path = TTX_DATA_DIR / "TestTTC.ttc"
with TTCollection(ttc_path, lazy=lazy) as collection:
assert len(collection) == 2
assert collection[0]["maxp"].numGlyphs == 6
assert collection[1]["maxp"].numGlyphs == 6
def test_lazy_open_file(lazy):
with (TTX_DATA_DIR / "TestTTC.ttc").open("rb") as file:
collection = TTCollection(file, lazy=lazy)
assert len(collection) == 2
assert collection[0]["maxp"].numGlyphs == 6
assert collection[1]["maxp"].numGlyphs == 6