From b9d28439cbacf00264e9e09851dc06bacad79991 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Sun, 30 Oct 2011 13:13:27 +0000 Subject: Use with for file usage Replace old style file open and close with a `with` block for clarity and it's error handling. --- wkhtmltopdf/utils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index 413a343..3bfa61a 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -85,11 +85,12 @@ def render_to_pdf(template_name, dictionary=None, context_instance=None, header_ def template_to_temp_file(*args, **kwargs): - """Renders a template to a temp file, and returns the path of the file.""" - fd, tmppath = mkstemp(suffix='.html') - f = fdopen(fd, 'wt') - f.write(smart_str(loader.render_to_string(*args, **kwargs))) - f.close() - return tmppath + """ + Renders a template to a temp file, and returns the path of the file. + """ + file_descriptor, tempfile_path = mkstemp(suffix='.html') + with fdopen(file_descriptor, 'wt') as f: + f.write(smart_str(loader.render_to_string(*args, **kwargs))) + return tempfile_path -- cgit v1.2.3