summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_format.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/tests/test_format.py')
-rw-r--r--numpy/lib/tests/test_format.py41
1 files changed, 25 insertions, 16 deletions
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index 2dbaeb8cb..1bfd63bba 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -285,7 +285,7 @@ from io import BytesIO
import numpy as np
from numpy.testing import (
assert_, assert_array_equal, assert_raises, assert_raises_regex,
- assert_warns
+ assert_warns, IS_PYPY, break_cycles
)
from numpy.lib import format
@@ -488,11 +488,8 @@ def test_memmap_roundtrip():
# Write it out normally and through mmap.
nfn = os.path.join(tempdir, 'normal.npy')
mfn = os.path.join(tempdir, 'memmap.npy')
- fp = open(nfn, 'wb')
- try:
+ with open(nfn, 'wb') as fp:
format.write_array(fp, arr)
- finally:
- fp.close()
fortran_order = (
arr.flags.f_contiguous and not arr.flags.c_contiguous)
@@ -500,26 +497,29 @@ def test_memmap_roundtrip():
shape=arr.shape, fortran_order=fortran_order)
ma[...] = arr
del ma
+ if IS_PYPY:
+ break_cycles()
# Check that both of these files' contents are the same.
- fp = open(nfn, 'rb')
- normal_bytes = fp.read()
- fp.close()
- fp = open(mfn, 'rb')
- memmap_bytes = fp.read()
- fp.close()
+ with open(nfn, 'rb') as fp:
+ normal_bytes = fp.read()
+ with open(mfn, 'rb') as fp:
+ memmap_bytes = fp.read()
assert_equal_(normal_bytes, memmap_bytes)
# Check that reading the file using memmap works.
ma = format.open_memmap(nfn, mode='r')
del ma
+ if IS_PYPY:
+ break_cycles()
def test_compressed_roundtrip():
arr = np.random.rand(200, 200)
npz_file = os.path.join(tempdir, 'compressed.npz')
np.savez_compressed(npz_file, arr=arr)
- arr1 = np.load(npz_file)['arr']
+ with np.load(npz_file) as npz:
+ arr1 = npz['arr']
assert_array_equal(arr, arr1)
@@ -545,7 +545,8 @@ def test_load_padded_dtype(dt):
arr[i] = i + 5
npz_file = os.path.join(tempdir, 'aligned.npz')
np.savez(npz_file, arr=arr)
- arr1 = np.load(npz_file)['arr']
+ with np.load(npz_file) as npz:
+ arr1 = npz['arr']
assert_array_equal(arr, arr1)
@@ -610,8 +611,8 @@ def test_pickle_disallow():
allow_pickle=False, encoding='latin1')
path = os.path.join(data_dir, 'py2-objarr.npz')
- f = np.load(path, allow_pickle=False, encoding='latin1')
- assert_raises(ValueError, f.__getitem__, 'x')
+ with np.load(path, allow_pickle=False, encoding='latin1') as f:
+ assert_raises(ValueError, f.__getitem__, 'x')
path = os.path.join(tempdir, 'pickle-disabled.npy')
assert_raises(ValueError, np.save, path, np.array([None], dtype=object),
@@ -713,6 +714,8 @@ def test_version_2_0_memmap():
shape=d.shape, version=(2, 0))
ma[...] = d
del ma
+ if IS_PYPY:
+ break_cycles()
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', UserWarning)
@@ -721,9 +724,14 @@ def test_version_2_0_memmap():
assert_(w[0].category is UserWarning)
ma[...] = d
del ma
+ if IS_PYPY:
+ break_cycles()
ma = format.open_memmap(tf, mode='r')
assert_array_equal(ma, d)
+ del ma
+ if IS_PYPY:
+ break_cycles()
def test_write_version():
@@ -925,7 +933,8 @@ def test_empty_npz():
# Test for gh-9989
fname = os.path.join(tempdir, "nothing.npz")
np.savez(fname)
- np.load(fname)
+ with np.load(fname) as nps:
+ pass
def test_unicode_field_names():