Skip to content

Commit

Permalink
Merge pull request #2226 from jdufresne/font-file-close
Browse files Browse the repository at this point in the history
Use a context manager in FontFile.save() to ensure file is always closed
  • Loading branch information
wiredfool committed Nov 22, 2016
2 parents 87b8d28 + 25ac9a2 commit 8b4cd7e
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions PIL/FontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ def save(self, filename):
self.bitmap.save(os.path.splitext(filename)[0] + ".pbm", "PNG")

# font metrics
fp = open(os.path.splitext(filename)[0] + ".pil", "wb")
fp.write(b"PILfont\n")
fp.write((";;;;;;%d;\n" % self.ysize).encode('ascii')) # HACK!!!
fp.write(b"DATA\n")
for id in range(256):
m = self.metrics[id]
if not m:
puti16(fp, [0] * 10)
else:
puti16(fp, m[0] + m[1] + m[2])
fp.close()
with open(os.path.splitext(filename)[0] + ".pil", "wb") as fp:
fp.write(b"PILfont\n")
fp.write((";;;;;;%d;\n" % self.ysize).encode('ascii')) # HACK!!!
fp.write(b"DATA\n")
for id in range(256):
m = self.metrics[id]
if not m:
puti16(fp, [0] * 10)
else:
puti16(fp, m[0] + m[1] + m[2])

0 comments on commit 8b4cd7e

Please sign in to comment.