@@ -357,7 +357,12 @@ def _count_ImageOpeners(proxy, data, voxels):
357357
358358def test_keep_file_open_true_false_invalid ():
359359 # Test the behaviour of the keep_file_open __init__ flag, when it is set to
360- # True or False.
360+ # True or False. Expected behaviour is as follows:
361+ # igzip present | keep_file_open | persist ImageOpener | igzip.drop_handles
362+ # False | False | False | n/a
363+ # False | True | True | n/a
364+ # True | False | True | True
365+ # True | True | True | False
361366 CountingImageOpener .num_openers = 0
362367 fname = 'testdata'
363368 dtype = np .float32
@@ -366,10 +371,11 @@ def test_keep_file_open_true_false_invalid():
366371 with InTemporaryDirectory ():
367372 with open (fname , 'wb' ) as fobj :
368373 fobj .write (data .tostring (order = 'F' ))
369- # Test that ArrayProxy(keep_file_open=True) only creates one file
370- # handle, and that ArrayProxy(keep_file_open=False) creates a file
371- # handle on every data access.
372- with mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ):
374+ # Without indexed_gzip, test that ArrayProxy(keep_file_open=True) only
375+ # creates one ImageOpener, and that ArrayProxy(keep_file_open=False)
376+ # creates an ImageOpener on every data access.
377+ with mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ), \
378+ patch_indexed_gzip (False ):
373379 proxy_no_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
374380 keep_file_open = False )
375381 assert not proxy_no_kfp ._keep_file_open
@@ -380,6 +386,26 @@ def test_keep_file_open_true_false_invalid():
380386 assert _count_ImageOpeners (proxy_kfp , data , voxels ) == 1
381387 del proxy_kfp
382388 del proxy_no_kfp
389+ # With indexed_gzip, test that both ArrayProxy(keep_file_open=True)
390+ # and ArrayProxy(keep_file_open=False) only create one ImageOpener,
391+ # but that the drop_handles parameter passed to the IndexedGzipFile
392+ # is set appropriately
393+ with mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ), \
394+ patch_indexed_gzip (True ):
395+ proxy_no_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
396+ keep_file_open = False )
397+ assert proxy_no_kfp ._keep_file_open
398+ assert _count_ImageOpeners (proxy_no_kfp , data , voxels ) == 1
399+ # check that the drop_handles flag is set - the fobj attribute
400+ # should be a MockIndexedGzipFile, defined in test_openers.
401+ assert proxy_no_kfp ._opener .fobj ._drop_handles
402+ proxy_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
403+ keep_file_open = True )
404+ assert proxy_kfp ._keep_file_open
405+ assert _count_ImageOpeners (proxy_kfp , data , voxels ) == 1
406+ assert not proxy_no_kfp ._opener .fobj ._drop_handles
407+ del proxy_kfp
408+ del proxy_no_kfp
383409 # Test that the keep_file_open flag has no effect if an open file
384410 # handle is passed in
385411 with open (fname , 'rb' ) as fobj :
@@ -405,34 +431,38 @@ def test_keep_file_open_true_false_invalid():
405431
406432def test_keep_file_open_auto ():
407433 # Test the behaviour of the keep_file_open __init__ flag, when it is set to
408- # 'auto'.
409- # if indexed_gzip is present, the ArrayProxy should persist its ImageOpener.
410- # Otherwise the ArrayProxy should drop openers.
434+ # 'auto'. Expected behaviour is as follows:
435+ # igzip present | keep_file_open | persist ImageOpener | igzip.drop_handles
436+ # False | 'auto' | False | n/a
437+ # True | 'auto' | False | False
411438 dtype = np .float32
412439 data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
413440 voxels = np .random .randint (0 , 10 , (10 , 3 ))
414441 with InTemporaryDirectory ():
415442 fname = 'testdata.gz'
416443 with gzip .open (fname , 'wb' ) as fobj :
417444 fobj .write (data .tostring (order = 'F' ))
418- # If have_indexed_gzip, then the arrayproxy should create one
419- # ImageOpener
420- with patch_indexed_gzip (True ), \
445+ # If no have_indexed_gzip, then a separate ImageOpener should be
446+ # created on every access.
447+ with patch_indexed_gzip (False ), \
421448 mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ):
422449 CountingImageOpener .num_openers = 0
423450 proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
424451 keep_file_open = 'auto' )
425- assert proxy ._keep_file_open == 'auto'
426- assert _count_ImageOpeners (proxy , data , voxels ) == 1
427- # If no have_indexed_gzip, then keep_file_open should be False
428- with patch_indexed_gzip (False ), \
452+ assert not proxy ._keep_file_open
453+ assert _count_ImageOpeners (proxy , data , voxels ) == 10
454+ # If have_indexed_gzip, then the arrayproxy should create one
455+ # ImageOpener, and the IndexedGzipFile drop_handles parameter should
456+ # be set to False, so the file handle stays open.
457+ with patch_indexed_gzip (True ), \
429458 mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ):
430459 CountingImageOpener .num_openers = 0
431460 proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
432461 keep_file_open = 'auto' )
433- assert proxy ._keep_file_open is False
434- assert _count_ImageOpeners (proxy , data , voxels ) == 10
435- # If not a gzip file, keep_file_open should be False
462+ assert proxy ._keep_file_open
463+ assert _count_ImageOpeners (proxy , data , voxels ) == 1
464+ assert not proxy ._opener .fobj ._drop_handles
465+ # If not a gzip file, keep_file_open should be False
436466 fname = 'testdata'
437467 with open (fname , 'wb' ) as fobj :
438468 fobj .write (data .tostring (order = 'F' ))
0 commit comments