summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2015-12-19 15:01:55 -0700
committerCharles Harris <charlesr.harris@gmail.com>2015-12-19 15:01:55 -0700
commit443184b12513ce2a8adcc2a81c143bc4bc697219 (patch)
treefc417d094f5d027b8a3a1dfcd8bce94257989356 /numpy/testing/utils.py
parente2bdaccba1a8691f9223b059b981b2890bb13b09 (diff)
downloadnumpy-443184b12513ce2a8adcc2a81c143bc4bc697219.tar.gz
ENH: Add context manager `temppath` to manage a temporary file.
Context manager intended for use when the same temporary file needs to be opened and closed more than once. The context manager creates the file, closes it, and returns the path to the file. On exit from the context block the file is removed. The file should be closed before exiting the context as an error will be raised on windows if not. Also fix up the `tempdir` context manager to deal with exceptions. Tests are added for both `temppath` and `tempdir`.
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r--numpy/testing/utils.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index 00f7ce4d1..e85e2f95f 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -12,7 +12,7 @@ import warnings
from functools import partial
import shutil
import contextlib
-from tempfile import mkdtemp
+from tempfile import mkdtemp, mkstemp
from .nosetester import import_nose
from numpy.core import float32, empty, arange, array_repr, ndarray
@@ -30,7 +30,7 @@ __all__ = ['assert_equal', 'assert_almost_equal', 'assert_approx_equal',
'assert_', 'assert_array_almost_equal_nulp', 'assert_raises_regex',
'assert_array_max_ulp', 'assert_warns', 'assert_no_warnings',
'assert_allclose', 'IgnoreException', 'clear_and_catch_warnings',
- 'SkipTest', 'KnownFailureException']
+ 'SkipTest', 'KnownFailureException', 'temppath', 'tempdir']
class KnownFailureException(Exception):
@@ -1810,8 +1810,31 @@ def tempdir(*args, **kwargs):
"""
tmpdir = mkdtemp(*args, **kwargs)
- yield tmpdir
- shutil.rmtree(tmpdir)
+ try:
+ yield tmpdir
+ finally:
+ shutil.rmtree(tmpdir)
+
+@contextlib.contextmanager
+def temppath(*args, **kwargs):
+ """Context manager for temporary files.
+
+ Context manager that returns the path to a closed temporary file. Its
+ parameters are the same as for tempfile.mkstemp and are passed directly
+ to that function. The underlying file is removed when the context is
+ exited, so it should be closed at that time.
+
+ Windows does not allow a temporary file to be opened if it is already
+ open, so the underlying file must be closed after opening before it
+ can be opened again.
+
+ """
+ fd, path = mkstemp(*args, **kwargs)
+ os.close(fd)
+ try:
+ yield path
+ finally:
+ os.remove(path)
class clear_and_catch_warnings(warnings.catch_warnings):