diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-12-19 15:01:55 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-12-19 15:01:55 -0700 |
commit | 443184b12513ce2a8adcc2a81c143bc4bc697219 (patch) | |
tree | fc417d094f5d027b8a3a1dfcd8bce94257989356 /numpy/testing/tests | |
parent | e2bdaccba1a8691f9223b059b981b2890bb13b09 (diff) | |
download | python-numpy-443184b12513ce2a8adcc2a81c143bc4bc697219.tar.gz python-numpy-443184b12513ce2a8adcc2a81c143bc4bc697219.tar.bz2 python-numpy-443184b12513ce2a8adcc2a81c143bc4bc697219.zip |
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/tests')
-rw-r--r-- | numpy/testing/tests/test_utils.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 13aeffe02..b558270b3 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -2,6 +2,7 @@ from __future__ import division, absolute_import, print_function import warnings import sys +import os import numpy as np from numpy.testing import ( @@ -10,7 +11,7 @@ from numpy.testing import ( assert_warns, assert_no_warnings, assert_allclose, assert_approx_equal, assert_array_almost_equal_nulp, assert_array_max_ulp, clear_and_catch_warnings, run_module_suite, - assert_string_equal + assert_string_equal, assert_, tempdir, temppath, ) import unittest @@ -780,6 +781,36 @@ def test_clear_and_catch_warnings(): assert_warn_len_equal(my_mod, 2) +def test_tempdir(): + with tempdir() as tdir: + fpath = os.path.join(tdir, 'tmp') + with open(fpath, 'w'): + pass + assert_(not os.path.isdir(tdir)) + + try: + with tempdir() as tdir: + raise ValueError() + except ValueError: + pass + assert_(not os.path.isdir(tdir)) + + + +def test_temppath(): + with temppath() as fpath: + with open(fpath, 'w') as f: + pass + assert_(not os.path.isfile(fpath)) + + try: + with temppath() as fpath: + raise ValueError() + except ValueError: + pass + assert_(not os.path.isfile(fpath)) + + class my_cacw(clear_and_catch_warnings): class_modules = (sys.modules[__name__],) |