summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/tests/test_multiarray.py90
-rw-r--r--numpy/core/tests/test_regression.py27
-rw-r--r--numpy/distutils/tests/test_exec_command.py5
-rw-r--r--numpy/f2py/tests/util.py5
-rw-r--r--numpy/testing/_private/utils.py10
5 files changed, 59 insertions, 78 deletions
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 600941cfd..270daad1e 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -4661,25 +4661,23 @@ class TestIO:
assert_array_equal(d, e)
def test_empty_files_binary(self):
- f = open(self.filename, 'w')
- f.close()
+ with open(self.filename, 'w') as f:
+ pass
y = np.fromfile(self.filename)
assert_(y.size == 0, "Array not empty")
def test_empty_files_text(self):
- f = open(self.filename, 'w')
- f.close()
+ with open(self.filename, 'wb') as f:
+ pass
y = np.fromfile(self.filename, sep=" ")
assert_(y.size == 0, "Array not empty")
def test_roundtrip_file(self):
- f = open(self.filename, 'wb')
- self.x.tofile(f)
- f.close()
+ with open(self.filename, 'wb') as f:
+ self.x.tofile(f)
# NB. doesn't work with flush+seek, due to use of C stdio
- f = open(self.filename, 'rb')
- y = np.fromfile(f, dtype=self.dtype)
- f.close()
+ with open(self.filename, 'rb') as f:
+ y = np.fromfile(f, dtype=self.dtype)
assert_array_equal(y, self.x.flat)
def test_roundtrip_filename(self):
@@ -4778,19 +4776,17 @@ class TestIO:
io.DEFAULT_BUFFER_SIZE*8]
for size in sizes:
- f = open(self.filename, 'wb')
- f.seek(size-1)
- f.write(b'\0')
- f.close()
+ with open(self.filename, 'wb') as f:
+ f.seek(size-1)
+ f.write(b'\0')
for mode in ['rb', 'r+b']:
err_msg = "%d %s" % (size, mode)
- f = open(self.filename, mode)
- f.read(2)
- np.fromfile(f, dtype=np.float64, count=1)
- pos = f.tell()
- f.close()
+ with open(self.filename, mode) as f:
+ f.read(2)
+ np.fromfile(f, dtype=np.float64, count=1)
+ pos = f.tell()
assert_equal(pos, 10, err_msg=err_msg)
def test_file_position_after_tofile(self):
@@ -4802,22 +4798,20 @@ class TestIO:
for size in sizes:
err_msg = "%d" % (size,)
- f = open(self.filename, 'wb')
- f.seek(size-1)
- f.write(b'\0')
- f.seek(10)
- f.write(b'12')
- np.array([0], dtype=np.float64).tofile(f)
- pos = f.tell()
- f.close()
+ with open(self.filename, 'wb') as f:
+ f.seek(size-1)
+ f.write(b'\0')
+ f.seek(10)
+ f.write(b'12')
+ np.array([0], dtype=np.float64).tofile(f)
+ pos = f.tell()
assert_equal(pos, 10 + 2 + 8, err_msg=err_msg)
- f = open(self.filename, 'r+b')
- f.read(2)
- f.seek(0, 1) # seek between read&write required by ANSI C
- np.array([0], dtype=np.float64).tofile(f)
- pos = f.tell()
- f.close()
+ with open(self.filename, 'r+b') as f:
+ f.read(2)
+ f.seek(0, 1) # seek between read&write required by ANSI C
+ np.array([0], dtype=np.float64).tofile(f)
+ pos = f.tell()
assert_equal(pos, 10, err_msg=err_msg)
def test_load_object_array_fromfile(self):
@@ -4870,9 +4864,8 @@ class TestIO:
y = np.fromstring(s, **kw)
assert_array_equal(y, value)
- f = open(self.filename, 'wb')
- f.write(s)
- f.close()
+ with open(self.filename, 'wb') as f:
+ f.write(s)
y = np.fromfile(self.filename, **kw)
assert_array_equal(y, value)
@@ -4956,33 +4949,28 @@ class TestIO:
# can't use _check_from because fromstring can't handle True/False
v = np.array([True, False, True, False], dtype=np.bool_)
s = b'1,0,-2.3,0'
- f = open(self.filename, 'wb')
- f.write(s)
- f.close()
+ with open(self.filename, 'wb') as f:
+ f.write(s)
y = np.fromfile(self.filename, sep=',', dtype=np.bool_)
assert_(y.dtype == '?')
assert_array_equal(y, v)
def test_tofile_sep(self):
x = np.array([1.51, 2, 3.51, 4], dtype=float)
- f = open(self.filename, 'w')
- x.tofile(f, sep=',')
- f.close()
- f = open(self.filename, 'r')
- s = f.read()
- f.close()
+ with open(self.filename, 'w') as f:
+ x.tofile(f, sep=',')
+ with open(self.filename, 'r') as f:
+ s = f.read()
#assert_equal(s, '1.51,2.0,3.51,4.0')
y = np.array([float(p) for p in s.split(',')])
assert_array_equal(x,y)
def test_tofile_format(self):
x = np.array([1.51, 2, 3.51, 4], dtype=float)
- f = open(self.filename, 'w')
- x.tofile(f, sep=',', format='%.2f')
- f.close()
- f = open(self.filename, 'r')
- s = f.read()
- f.close()
+ with open(self.filename, 'w') as f:
+ x.tofile(f, sep=',', format='%.2f')
+ with open(self.filename, 'r') as f:
+ s = f.read()
assert_equal(s, '1.51,2.00,3.51,4.00')
def test_locale(self):
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 3a9b96886..321723b9b 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -36,11 +36,10 @@ class TestRegression:
# Ticket #16
a = np.transpose(np.array([[2, 9], [7, 0], [3, 8]]))
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
- f = BytesIO()
- pickle.dump(a, f, protocol=proto)
- f.seek(0)
- b = pickle.load(f)
- f.close()
+ with BytesIO() as f:
+ pickle.dump(a, f, protocol=proto)
+ f.seek(0)
+ b = pickle.load(f)
assert_array_equal(a, b)
def test_typeNA(self):
@@ -94,11 +93,10 @@ class TestRegression:
# Ticket #50
ca = np.char.array(np.arange(1000, 1010), itemsize=4)
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
- f = BytesIO()
- pickle.dump(ca, f, protocol=proto)
- f.seek(0)
- ca = np.load(f, allow_pickle=True)
- f.close()
+ with BytesIO() as f:
+ pickle.dump(ca, f, protocol=proto)
+ f.seek(0)
+ ca = np.load(f, allow_pickle=True)
def test_noncontiguous_fill(self):
# Ticket #58.
@@ -358,11 +356,10 @@ class TestRegression:
# Implemented in r2840
dt = np.dtype([('x', int), ('y', np.object_), ('z', 'O')])
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
- f = BytesIO()
- pickle.dump(dt, f, protocol=proto)
- f.seek(0)
- dt_ = pickle.load(f)
- f.close()
+ with BytesIO() as f:
+ pickle.dump(dt, f, protocol=proto)
+ f.seek(0)
+ dt_ = pickle.load(f)
assert_equal(dt, dt_)
def test_mem_array_creation_invalid_specification(self):
diff --git a/numpy/distutils/tests/test_exec_command.py b/numpy/distutils/tests/test_exec_command.py
index 3bbad9386..d6eb7d1c3 100644
--- a/numpy/distutils/tests/test_exec_command.py
+++ b/numpy/distutils/tests/test_exec_command.py
@@ -186,9 +186,8 @@ class TestExecCommand:
with tempdir() as tmpdir:
fn = "file"
tmpfile = os.path.join(tmpdir, fn)
- f = open(tmpfile, 'w')
- f.write('Hello')
- f.close()
+ with open(tmpfile, 'w') as f:
+ f.write('Hello')
s, o = exec_command.exec_command(
'"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %
diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py
index c430a6608..6dcc2ed12 100644
--- a/numpy/f2py/tests/util.py
+++ b/numpy/f2py/tests/util.py
@@ -282,9 +282,8 @@ def build_module_distutils(source_files, config_code, module_name, **kw):
script = os.path.join(d, get_temp_module_name() + '.py')
dst_sources.append(script)
- f = open(script, 'wb')
- f.write(asbytes(code))
- f.close()
+ with open(script, 'wb') as f:
+ f.write(asbytes(code))
# Build
cwd = os.getcwd()
diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py
index 1b88d91f6..8b098f1d1 100644
--- a/numpy/testing/_private/utils.py
+++ b/numpy/testing/_private/utils.py
@@ -192,9 +192,8 @@ elif sys.platform[:5] == 'linux':
"""
try:
- f = open(_proc_pid_stat, 'r')
- l = f.readline().split(' ')
- f.close()
+ with open(_proc_pid_stat, 'r') as f:
+ l = f.readline().split(' ')
return int(l[22])
except Exception:
return
@@ -221,9 +220,8 @@ if sys.platform[:5] == 'linux':
if not _load_time:
_load_time.append(time.time())
try:
- f = open(_proc_pid_stat, 'r')
- l = f.readline().split(' ')
- f.close()
+ with open(_proc_pid_stat, 'r') as f:
+ l = f.readline().split(' ')
return int(l[13])
except Exception:
return int(100*(time.time()-_load_time[0]))