summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_io.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/tests/test_io.py')
-rw-r--r--numpy/lib/tests/test_io.py86
1 files changed, 31 insertions, 55 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 45ee0a477..32e0c32de 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -4,7 +4,7 @@ import sys
import gzip
import os
import threading
-from tempfile import mkstemp, NamedTemporaryFile
+from tempfile import NamedTemporaryFile
import time
import warnings
import gc
@@ -194,8 +194,7 @@ class TestSavezLoad(RoundtripTest, TestCase):
def test_big_arrays(self):
L = (1 << 31) + 100000
a = np.empty(L, dtype=np.uint8)
- with tempdir(prefix="numpy_test_big_arrays_") as tmpdir:
- tmp = os.path.join(tmpdir, "file.npz")
+ with temppath(prefix="numpy_test_big_arrays_") as tmp:
np.savez(tmp, a=a)
del a
npfile = np.load(tmp)
@@ -234,16 +233,12 @@ class TestSavezLoad(RoundtripTest, TestCase):
# and savez functions in multithreaded environment
def writer(error_list):
- fd, tmp = mkstemp(suffix='.npz')
- os.close(fd)
- try:
+ with temppath(suffix='.npz') as tmp:
arr = np.random.randn(500, 500)
try:
np.savez(tmp, arr=arr)
except OSError as err:
error_list.append(err)
- finally:
- os.remove(tmp)
errors = []
threads = [threading.Thread(target=writer, args=(errors,))
@@ -277,13 +272,8 @@ class TestSavezLoad(RoundtripTest, TestCase):
# e.g. Debian sid of 2012 Jul 05 but was reported to
# trigger the failure on Ubuntu 10.04:
# http://projects.scipy.org/numpy/ticket/1517#comment:2
- fd, tmp = mkstemp(suffix='.npz')
- os.close(fd)
-
- try:
- fp = open(tmp, 'wb')
- np.savez(fp, data='LOVELY LOAD')
- fp.close()
+ with temppath(suffix='.npz') as tmp:
+ np.savez(tmp, data='LOVELY LOAD')
# We need to check if the garbage collector can properly close
# numpy npz file returned by np.load when their reference count
# goes to zero. Python 3 running in debug mode raises a
@@ -299,16 +289,14 @@ class TestSavezLoad(RoundtripTest, TestCase):
except Exception as e:
msg = "Failed to load data from a file: %s" % e
raise AssertionError(msg)
- finally:
- os.remove(tmp)
def test_closing_zipfile_after_load(self):
- # Check that zipfile owns file and can close it.
- # This needs to pass a file name to load for the
- # test.
- with tempdir(prefix="numpy_test_closing_zipfile_after_load_") as tmpdir:
- fd, tmp = mkstemp(suffix='.npz', dir=tmpdir)
- os.close(fd)
+ # Check that zipfile owns file and can close it. This needs to
+ # pass a file name to load for the test. On windows failure will
+ # cause a second error will be raised when the attempt to remove
+ # the open file is made.
+ prefix = 'numpy_test_closing_zipfile_after_load_'
+ with temppath(suffix='.npz', prefix=prefix) as tmp:
np.savez(tmp, lab='place holder')
data = np.load(tmp)
fp = data.zip.fp
@@ -416,15 +404,11 @@ class TestSaveTxt(TestCase):
asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n'))
def test_file_roundtrip(self):
- f, name = mkstemp()
- os.close(f)
- try:
+ with temppath() as name:
a = np.array([(1, 2), (3, 4)])
np.savetxt(name, a)
b = np.loadtxt(name)
assert_array_equal(a, b)
- finally:
- os.unlink(name)
def test_complex_arrays(self):
ncols = 2
@@ -739,15 +723,11 @@ class TestLoadTxt(TestCase):
assert_equal(res, tgt)
def test_universal_newline(self):
- f, name = mkstemp()
- os.write(f, b'1 21\r3 42\r')
- os.close(f)
-
- try:
+ with temppath() as name:
+ with open(name, 'w') as f:
+ f.write('1 21\r3 42\r')
data = np.loadtxt(name)
- assert_array_equal(data, [[1, 21], [3, 42]])
- finally:
- os.unlink(name)
+ assert_array_equal(data, [[1, 21], [3, 42]])
def test_empty_field_after_tab(self):
c = TextIO()
@@ -1760,8 +1740,9 @@ M 33 21.99
assert_equal(test, control)
def test_gft_using_filename(self):
- # Test that we can load data from a filename as well as a file object
- wanted = np.arange(6).reshape((2, 3))
+ # Test that we can load data from a filename as well as a file
+ # object
+ tgt = np.arange(6).reshape((2, 3))
if sys.version_info[0] >= 3:
# python 3k is known to fail for '\r'
linesep = ('\n', '\r\n')
@@ -1770,15 +1751,11 @@ M 33 21.99
for sep in linesep:
data = '0 1 2' + sep + '3 4 5'
- f, name = mkstemp()
- # We can't use NamedTemporaryFile on windows, because we cannot
- # reopen the file.
- try:
- os.write(f, asbytes(data))
- assert_array_equal(np.genfromtxt(name), wanted)
- finally:
- os.close(f)
- os.unlink(name)
+ with temppath() as name:
+ with open(name, 'w') as f:
+ f.write(data)
+ res = np.genfromtxt(name)
+ assert_array_equal(res, tgt)
def test_gft_using_generator(self):
# gft doesn't work with unicode.
@@ -1838,16 +1815,15 @@ def test_gzip_loadtxt():
g = gzip.GzipFile(fileobj=s, mode='w')
g.write(b'1 2 3\n')
g.close()
+
s.seek(0)
+ with temppath(suffix='.gz') as name:
+ with open(name, 'wb') as f:
+ f.write(s.read())
+ res = np.loadtxt(name)
+ s.close()
- f, name = mkstemp(suffix='.gz')
- try:
- os.write(f, s.read())
- s.close()
- assert_array_equal(np.loadtxt(name), [1, 2, 3])
- finally:
- os.close(f)
- os.unlink(name)
+ assert_array_equal(res, [1, 2, 3])
def test_gzip_loadtxt_from_string():