diff options
Diffstat (limited to 'numpy/lib/tests/test_io.py')
-rw-r--r-- | numpy/lib/tests/test_io.py | 111 |
1 files changed, 72 insertions, 39 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index e3ccb391c..03e238261 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, mktemp, NamedTemporaryFile +from tempfile import mkstemp, NamedTemporaryFile import time import warnings import gc @@ -17,9 +17,12 @@ from numpy.lib._iotools import (ConverterError, ConverterLockError, ConversionWarning) from numpy.compat import asbytes, asbytes_nested, bytes, asstr from nose import SkipTest -from numpy.ma.testutils import (TestCase, assert_equal, assert_array_equal, - assert_raises, run_module_suite) +from numpy.ma.testutils import ( + TestCase, assert_equal, assert_array_equal, + assert_raises, assert_raises_regex, run_module_suite +) from numpy.testing import assert_warns, assert_, build_err_msg +from numpy.testing.utils import tempdir class TextIO(BytesIO): @@ -77,32 +80,32 @@ class RoundtripTest(object): file_on_disk = kwargs.get('file_on_disk', False) if file_on_disk: - # Do not delete the file on windows, because we can't - # reopen an already opened file on that platform, so we - # need to close the file and reopen it, implying no - # automatic deletion. - if sys.platform == 'win32' and MAJVER >= 2 and MINVER >= 6: - target_file = NamedTemporaryFile(delete=False) - else: - target_file = NamedTemporaryFile() + target_file = NamedTemporaryFile(delete=False) load_file = target_file.name else: target_file = BytesIO() load_file = target_file - arr = args + try: + arr = args - save_func(target_file, *arr, **save_kwds) - target_file.flush() - target_file.seek(0) + save_func(target_file, *arr, **save_kwds) + target_file.flush() + target_file.seek(0) - if sys.platform == 'win32' and not isinstance(target_file, BytesIO): - target_file.close() + if sys.platform == 'win32' and not isinstance(target_file, BytesIO): + target_file.close() - arr_reloaded = np.load(load_file, **load_kwds) + arr_reloaded = np.load(load_file, **load_kwds) - self.arr = arr - self.arr_reloaded = arr_reloaded + self.arr = arr + self.arr_reloaded = arr_reloaded + finally: + if not isinstance(target_file, BytesIO): + target_file.close() + # holds an open file descriptor so it can't be deleted on win + if not isinstance(arr_reloaded, np.lib.npyio.NpzFile): + os.remove(target_file.name) def check_roundtrips(self, a): self.roundtrip(a) @@ -155,6 +158,13 @@ class RoundtripTest(object): a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) self.check_roundtrips(a) + def test_format_2_0(self): + dt = [(("%d" % i) * 100, float) for i in range(500)] + a = np.ones(1000, dtype=dt) + with warnings.catch_warnings(record=True): + warnings.filterwarnings('always', '', UserWarning) + self.check_roundtrips(a) + class TestSaveLoad(RoundtripTest, TestCase): def roundtrip(self, *args, **kwargs): @@ -167,24 +177,30 @@ class TestSaveLoad(RoundtripTest, TestCase): class TestSavezLoad(RoundtripTest, TestCase): def roundtrip(self, *args, **kwargs): RoundtripTest.roundtrip(self, np.savez, *args, **kwargs) - for n, arr in enumerate(self.arr): - reloaded = self.arr_reloaded['arr_%d' % n] - assert_equal(arr, reloaded) - assert_equal(arr.dtype, reloaded.dtype) - assert_equal(arr.flags.fnc, reloaded.flags.fnc) + try: + for n, arr in enumerate(self.arr): + reloaded = self.arr_reloaded['arr_%d' % n] + assert_equal(arr, reloaded) + assert_equal(arr.dtype, reloaded.dtype) + assert_equal(arr.flags.fnc, reloaded.flags.fnc) + finally: + # delete tempfile, must be done here on windows + if self.arr_reloaded.fid: + self.arr_reloaded.fid.close() + os.remove(self.arr_reloaded.fid.name) @np.testing.dec.skipif(not IS_64BIT, "Works only with 64bit systems") @np.testing.dec.slow def test_big_arrays(self): L = (1 << 31) + 100000 - tmp = mktemp(suffix='.npz') a = np.empty(L, dtype=np.uint8) - np.savez(tmp, a=a) - del a - npfile = np.load(tmp) - a = npfile['a'] - npfile.close() - os.remove(tmp) + with tempdir(prefix="numpy_test_big_arrays_") as tmpdir: + tmp = os.path.join(tmpdir, "file.npz") + np.savez(tmp, a=a) + del a + npfile = np.load(tmp) + a = npfile['a'] + npfile.close() def test_multiple_arrays(self): a = np.array([[1, 2], [3, 4]], float) @@ -287,13 +303,14 @@ class TestSavezLoad(RoundtripTest, TestCase): # Check that zipfile owns file and can close it. # This needs to pass a file name to load for the # test. - fd, tmp = mkstemp(suffix='.npz') - os.close(fd) - np.savez(tmp, lab='place holder') - data = np.load(tmp) - fp = data.zip.fp - data.close() - assert_(fp.closed) + with tempdir(prefix="numpy_test_closing_zipfile_after_load_") as tmpdir: + fd, tmp = mkstemp(suffix='.npz', dir=tmpdir) + os.close(fd) + np.savez(tmp, lab='place holder') + data = np.load(tmp) + fp = data.zip.fp + data.close() + assert_(fp.closed) class TestSaveTxt(TestCase): @@ -750,6 +767,14 @@ class TestLoadTxt(TestCase): res = np.loadtxt(count()) assert_array_equal(res, np.arange(10)) + def test_bad_line(self): + c = TextIO() + c.write('1 2 3\n4 5 6\n2 3') + c.seek(0) + + # Check for exception and that exception contains line number + assert_raises_regex(ValueError, "3", np.loadtxt, c) + class Testfromregex(TestCase): # np.fromregex expects files opened in binary mode. @@ -1563,6 +1588,14 @@ M 33 21.99 dtype=[('a', np.int), ('b', np.int)]) self.assertTrue(isinstance(test, np.recarray)) assert_equal(test, control) + # + data = TextIO('A,B\n0,1\n2,3') + dtype = [('a', np.int), ('b', np.float)] + test = np.recfromcsv(data, missing_values='N/A', dtype=dtype) + control = np.array([(0, 1), (2, 3)], + dtype=dtype) + self.assertTrue(isinstance(test, np.recarray)) + 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 |