8000 MAINT: Use temppath in test_not_closing_opened_fid. · numpy/numpy@c4156cf · GitHub
[go: up one dir, main page]

Skip to content

Commit c4156cf

Browse files
committed
MAINT: Use temppath in test_not_closing_opened_fid.
The test is in numpy/lib/tests/test_io.py. This commit is intended as a demonstration of using temppath.
1 parent 443184b commit c4156cf

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

numpy/lib/tests/test_io.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from numpy.testing import (
2020
TestCase, run_module_suite, assert_warns, assert_,
2121
assert_raises_regex, assert_raises, assert_allclose,
22-
assert_array_equal,
22+
assert_array_equal,temppath
2323
)
2424
from numpy.testing.utils import tempdir
2525

@@ -259,26 +259,17 @@ def writer(error_list):
259259
def test_not_closing_opened_fid(self):
260260
# Test that issue #2178 is fixed:
261261
# verify could seek on 'loaded' file
262-
263-
fd, tmp = mkstemp(suffix='.npz')
264-
os.close(fd)
265-
try:
266-
fp = open(tmp, 'wb')
267-
np.savez(fp, data='LOVELY LOAD')
268-
fp.close()
269-
270-
fp = open(tmp, 'rb', 10000)
271-
fp.seek(0)
272-
assert_(not fp.closed)
273-
np.load(fp)['data']
274-
# fp must not get closed by .load
275-
assert_(not fp.closed)
276-
fp.seek(0)
277-
assert_(not fp.closed)
278-
279-
finally:
280-
fp.close()
281-
os.remove(tmp)
262+
with temppath(suffix='.npz') as tmp:
263+
with open(tmp, 'wb') as fp:
264+
np.savez(fp, data='LOVELY LOAD')
265+
with open(tmp, 'rb', 10000) as fp:
266+
fp.seek(0)
267+
assert_(not fp.closed)
268+
np.load(fp)['data']
269+
# fp must not get closed by .load
270+
assert_(not fp.closed)
271+
fp.seek(0)
272+
assert_(not fp.closed)
282273

283274
def test_closing_fid(self):
284275
# Test that issue #1517 (too many opened files) remains closed

numpy/testing/tests/test_utils.py

Lines changed: 6 additions & 2 deletions
57BE
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,13 @@ def test_tempdir():
788788
pass
789789
assert_(not os.path.isdir(tdir))
790790

791+
raised = False
791792
try:
792793
with tempdir() as tdir:
793794
raise ValueError()
794795
except ValueError:
795-
pass
796+
raised = True
797+
assert_(raised)
796798
assert_(not os.path.isdir(tdir))
797799

798800

@@ -803,11 +805,13 @@ def test_temppath():
803805
pass
804806
assert_(not os.path.isfile(fpath))
805807

808+
raised = False
806809
try:
807810
with temppath() as fpath:
808811
raise ValueError()
809812
except ValueError:
810-
pass
813+
raised = True
814+
assert_(raised)
811815
assert_(not os.path.isfile(fpath))
812816

813817

0 commit comments

Comments
 (0)
0