From bf6911d36f8fd18614583c5dc6f6cda63cc8a013 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 7 Dec 2016 18:25:58 +0100 Subject: [PATCH] Correctly close files open by mkstemp 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. --- weasyprint/fonts.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/weasyprint/fonts.py b/weasyprint/fonts.py index 5e95c4aec..b61555cf6 100644 --- a/weasyprint/fonts.py +++ b/weasyprint/fonts.py @@ -225,9 +225,9 @@ def add_font_face(self, rule_descriptors, url_fetcher): **font_features).items(): features_string += '%s %s' % ( 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 = ''' @@ -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 but what about ? - fd.write(xml.encode(FILESYSTEM_ENCODING)) + fd, conf_filename = tempfile.mkstemp() + # TODO: coding is OK for but what about ? + os.write(fd, xml.encode(FILESYSTEM_ENCODING)) + os.close(fd) self._filenames.append(conf_filename) fontconfig.FcConfigParseAndLoad( config, conf_filename.encode(FILESYSTEM_ENCODING),