Close source TTFont after instantiateVariableFont rebind

instantiateVariableFont with default inplace=False returns a *new*
TTFont; rebinding `font` strands the source TTFont and its file handle
open until GC. The trailing font.close() then closes only the new
pinned-instance object.

Capture the source as a separate reference and close both with nested
try/finally: inner finally closes the static instance after save (or
on save failure), outer finally closes the source unconditionally.
This commit is contained in:
jpirnay
2026-05-10 10:35:51 +02:00
parent 49b039a775
commit ad52abf069
+16 -8
View File
@@ -81,14 +81,22 @@ def extract_static_instance(source_path: Path, axes: dict, family_name: str, sty
old.unlink()
print(f" Extracting static instance: {family_name}/{style_name} ({axis_key})")
font = TTFont(str(source_path))
# `static` was removed (or never existed) in current fontTools; it raises
# TypeError under recent releases. updateFontNames=True keeps the saved
# name table accurate; optimize=False skips a gvar pass that's wasted
# when every axis is fully pinned.
font = instantiateVariableFont(font, axes, updateFontNames=True, optimize=False)
font.save(str(cached))
font.close()
# Keep separate handles for the source variable font and the static
# instance: instantiateVariableFont with default inplace=False returns a
# *new* TTFont, so rebinding `font` would otherwise strand the source's
# file handle open until GC runs.
#
# updateFontNames=True keeps the saved name table accurate; optimize=False
# skips a gvar pass that's wasted when every axis is fully pinned.
source_font = TTFont(str(source_path))
try:
font = instantiateVariableFont(source_font, axes, updateFontNames=True, optimize=False)
try:
font.save(str(cached))
finally:
font.close()
finally:
source_font.close()
return cached