From e5130180526d6eb79b5af56ef859c7ea6fa7f567 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Thu, 18 Oct 2012 22:07:18 -0400 Subject: [PATCH] Fix handling of temporary file. Opening an already-opened file was unsafe. Close the file, then manually delete it. --- skimage/io/_io.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/skimage/io/_io.py b/skimage/io/_io.py index e400f6b3..bf75cf76 100644 --- a/skimage/io/_io.py +++ b/skimage/io/_io.py @@ -1,6 +1,7 @@ __all__ = ['Image', 'imread', 'imread_collection', 'imsave', 'imshow', 'show', 'push', 'pop'] +import os import re import urllib2 import tempfile @@ -129,12 +130,12 @@ def imread(fname, as_grey=False, plugin=None, flatten=None, as_grey = flatten if is_url(fname): - with tempfile.NamedTemporaryFile() as f: + with tempfile.NamedTemporaryFile(delete=False) as f: u = urllib2.urlopen(fname) f.write(u.read()) - f.seek(0) - # Note: the temporary file must be used inside the with-block. + f.close() img = call_plugin('imread', f.name, plugin=plugin, **plugin_args) + os.remove(f.name) else: img = call_plugin('imread', fname, plugin=plugin, **plugin_args)