@@ -357,7 +357,12 @@ def _count_ImageOpeners(proxy, data, voxels):
357
357
358
358
def test_keep_file_open_true_false_invalid ():
359
359
# 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
361
366
CountingImageOpener .num_openers = 0
362
367
fname = 'testdata'
363
368
dtype = np .float32
@@ -366,10 +371,11 @@ def test_keep_file_open_true_false_invalid():
366
371
with InTemporaryDirectory ():
367
372
with open (fname , 'wb' ) as fobj :
368
373
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 ):
373
379
proxy_no_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
374
380
keep_file_open = False )
375
381
assert not proxy_no_kfp ._keep_file_open
@@ -380,6 +386,26 @@ def test_keep_file_open_true_false_invalid():
380
386
assert _count_ImageOpeners (proxy_kfp , data , voxels ) == 1
381
387
del proxy_kfp
382
388
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
383
409
# Test that the keep_file_open flag has no effect if an open file
384
410
# handle is passed in
385
411
with open (fname , 'rb' ) as fobj :
@@ -405,34 +431,38 @@ def test_keep_file_open_true_false_invalid():
405
431
406
432
def test_keep_file_open_auto ():
407
433
# 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
411
438
dtype = np .float32
412
439
data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
413
440
voxels = np .random .randint (0 , 10 , (10 , 3 ))
414
441
with InTemporaryDirectory ():
415
442
fname = 'testdata.gz'
416
443
with gzip .open (fname , 'wb' ) as fobj :
417
444
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 ), \
421
448
mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ):
422
449
CountingImageOpener .num_openers = 0
423
450
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
424
451
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 ), \
429
458
mock .patch ('nibabel.openers.ImageOpener' , CountingImageOpener ):
430
459
CountingImageOpener .num_openers = 0
431
460
proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
432
461
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
436
466
fname = 'testdata'
437
467
with open (fname , 'wb' ) as fobj :
438
468
fobj .write (data .tostring (order = 'F' ))
0 commit comments