Skip to content

Commit f6236ac

Browse files
committed
TEST: Avoid file close errors on windows
1 parent 5a9b7f3 commit f6236ac

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

nibabel/tests/test_arrayproxy.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,22 @@ def test_keep_file_open_true_false_invalid():
427427
else:
428428
with open(fname, 'wb') as fobj:
429429
fobj.write(data.tostring(order='F'))
430+
# pass in a file name or open file handle. If the latter, we open
431+
# two file handles, because we're going to create two proxies
432+
# below.
430433
if filetype == 'open':
431-
fname = open(fname, 'rb')
434+
fobj1 = open(fname, 'rb')
435+
fobj2 = open(fname, 'rb')
436+
else:
437+
fobj1 = fname
438+
fobj2 = fname
432439
try:
433-
proxy = ArrayProxy(fname, ((10, 10, 10), dtype),
440+
proxy = ArrayProxy(fobj1, ((10, 10, 10), dtype),
434441
keep_file_open=kfo)
435442
# We also test that we get the same behaviour when the
436443
# KEEP_FILE_OPEN_DEFAULT flag is changed
437444
with patch_keep_file_open_default(kfo):
438-
proxy_def = ArrayProxy(fname, ((10, 10, 10), dtype))
445+
proxy_def = ArrayProxy(fobj2, ((10, 10, 10), dtype))
439446
# check internal flags
440447
assert proxy._persist_opener == exp_persist
441448
assert proxy._keep_file_open == exp_kfo
@@ -458,13 +465,17 @@ def test_keep_file_open_true_false_invalid():
458465
# if we were using an open file handle, check that the proxy
459466
# didn't close it
460467
if filetype == 'open':
461-
assert not fname.closed
468+
assert not fobj1.closed
469+
assert not fobj2.closed
462470
except Exception:
463471
print('Failed test', test)
464472
raise
465473
finally:
474+
del proxy
475+
del proxy_def
466476
if filetype == 'open':
467-
fname.close()
477+
fobj1.close()
478+
fobj2.close()
468479
# Test invalid values of keep_file_open
469480
print('testinv')
470481
with InTemporaryDirectory():

0 commit comments

Comments
 (0)