diff options
author | George Hickman <george@ghickman.co.uk> | 2011-10-30 13:13:27 +0000 |
---|---|---|
committer | George Hickman <george@ghickman.co.uk> | 2011-10-30 13:13:27 +0000 |
commit | b9d28439cbacf00264e9e09851dc06bacad79991 (patch) | |
tree | 538474b8899dbcf4771ecb18653131d711a5733f /wkhtmltopdf | |
parent | bf782db40aa4ab9df8101988bb3d441841423695 (diff) | |
download | django-wkhtmltopdf-b9d28439cbacf00264e9e09851dc06bacad79991.tar.gz django-wkhtmltopdf-b9d28439cbacf00264e9e09851dc06bacad79991.tar.bz2 django-wkhtmltopdf-b9d28439cbacf00264e9e09851dc06bacad79991.zip |
Use with for file usage
Replace old style file open and close with a `with` block for clarity
and it's error handling.
Diffstat (limited to 'wkhtmltopdf')
-rw-r--r-- | wkhtmltopdf/utils.py | 13 |
1 files 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 |