From 5481339b35657808b8af36f9a0ceaaadc4efad13 Mon Sep 17 00:00:00 2001 From: David Caro Date: Tue, 6 Dec 2016 18:08:52 +0100 Subject: [PATCH] utils: unmask exception on create_file_name * When failing to create the `NamedTemporaryFile` on the output dir, the original exception was masked by another one thrown in the `finally` block. (closes #34) Signed-off-by: David Caro --- invenio_oaiharvester/utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/invenio_oaiharvester/utils.py b/invenio_oaiharvester/utils.py index ac144e1..e28cd23 100644 --- a/invenio_oaiharvester/utils.py +++ b/invenio_oaiharvester/utils.py @@ -26,6 +26,7 @@ import os import re import tempfile +from contextlib import closing from datetime import datetime from flask import current_app @@ -170,16 +171,16 @@ def create_file_name(output_dir): """ prefix = 'oaiharvest_' + datetime.now().strftime('%Y-%m-%d') + '_' - try: - temp = tempfile.NamedTemporaryFile( + with closing( + tempfile.NamedTemporaryFile( prefix=prefix, suffix='.xml', dir=output_dir, mode='w+' ) + ) as temp: file_name = temp.name[:] - finally: - temp.close() + return file_name