Skip to content

Commit

Permalink
Correctly close files open by mkstemp
Browse files Browse the repository at this point in the history
Python's tempfile.mkstemp already opens files, we don't have to open
them twice. The documentation is not really clear about that but
there's a good post on Logilab's blog: https://www.logilab.org/17873

Fix #396.
  • Loading branch information
liZe committed Dec 7, 2016
1 parent c9a2a0e commit bf6911d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions weasyprint/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ def add_font_face(self, rule_descriptors, url_fetcher):
**font_features).items():
features_string += '<string>%s %s</string>' % (
key, value)
_, filename = tempfile.mkstemp()
with open(filename, 'wb') as fd:
fd.write(font)
fd, filename = tempfile.mkstemp()
os.write(fd, font)
os.close(fd)
self._filenames.append(filename)
xml = '''<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
Expand Down Expand Up @@ -266,10 +266,10 @@ def add_font_face(self, rule_descriptors, url_fetcher):
FONTCONFIG_STRETCH_CONSTANTS[
rule_descriptors.get('font_stretch', 'normal')],
filename, features_string)
_, conf_filename = tempfile.mkstemp()
with open(conf_filename, 'wb') as fd:
# TODO: coding is OK for <test> but what about <edit>?
fd.write(xml.encode(FILESYSTEM_ENCODING))
fd, conf_filename = tempfile.mkstemp()
# TODO: coding is OK for <test> but what about <edit>?
os.write(fd, xml.encode(FILESYSTEM_ENCODING))
os.close(fd)
self._filenames.append(conf_filename)
fontconfig.FcConfigParseAndLoad(
config, conf_filename.encode(FILESYSTEM_ENCODING),
Expand Down

0 comments on commit bf6911d

Please sign in to comment.