8000 [ttLib] TTFont.save: create file on disk as late as possible by justvanrossum · Pull Request #2253 · fonttools/fonttools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[ttLib] TTFont.save: create file on disk as late as possible #2253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Lib/fontTools/ttLib/ttFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,17 @@ def save(self, file, reorderTables=True):
if self.lazy and self.reader.file.name == file:
raise TTLibError(
"Can't overwrite TTFont when 'lazy' attribute is True")
closeStream = True
file = open(file, "wb")
createStream = True
else:
# assume "file" is a writable file object
closeStream = False
createStream = False

tmp = BytesIO()

writer_reordersTables = self._save(tmp)

if (reorderTables is None or writer_reordersTables or
if not (reorderTables is None or writer_reordersTables or
(reorderTables is False and self.reader is None)):
# don't reorder tables and save as is
file.write(tmp.getvalue())
tmp.close()
else:
if reorderTables is False:
# sort tables using the original font's order
tableOrder = list(self.reader.keys())
Expand All @@ -186,12 +181,17 @@ def save(self, file, reorderTables=True):
tmp.flush()
tmp2 = BytesIO()
reorderFontTables(tmp, tmp2, tableOrder)
file.write(tmp2.getvalue())
tmp.close()
tmp2.close()
tmp = tmp2

if createStream:
# "file" is a path
with open(file, "wb") as file:
file.write(tmp.getvalue())
else:
file.write(tmp.getvalue())

if closeStream:
file.close()
tmp.close()

def _save(self, file, tableCache=None):
"""Internal function, to be shared by save() and TTCollection.save()"""
Expand Down
0