11
11
from __future__ import division , print_function , absolute_import
12
12
13
13
import warnings
14
+ import gzip
14
15
15
16
import pickle
16
17
from io import BytesIO
@@ -332,16 +333,22 @@ def check_mmap(hdr, offset, proxy_class,
332
333
assert_raises (ValueError , proxy_class , fname , hdr , mmap = 'r+' )
333
334
334
335
335
- def test_keep_file_open ():
336
- # Test the behaviour of the keep_file_open __init__ flag.
337
- numopeners = [0 ]
338
- class CountingImageOpener (ImageOpener ):
336
+ # An image opener class which counts how many instances of itself have been
337
+ # created
338
+ class CountingImageOpener (ImageOpener ):
339
339
340
- def __init__ ( self , * args , ** kwargs ):
340
+ numOpeners = 0
341
341
342
- super (CountingImageOpener , self ).__init__ (* args , ** kwargs )
343
- numopeners [0 ] += 1
342
+ def __init__ (self , * args , ** kwargs ):
344
343
344
+ super (CountingImageOpener , self ).__init__ (* args , ** kwargs )
345
+ CountingImageOpener .numOpeners += 1
346
+
347
+
348
+ def test_keep_file_open_true_false ():
349
+ # Test the behaviour of the keep_file_open __init__ flag, when it is set to
350
+ # True or False.
351
+ CountingImageOpener .numOpeners = 0
345
352
fname = 'testdata'
346
353
dtype = np .float32
347
354
data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
@@ -354,22 +361,25 @@ def __init__(self, *args, **kwargs):
354
361
# handle on every data access.
355
362
with mock .patch ('nibabel.arrayproxy.ImageOpener' , CountingImageOpener ):
356
363
proxy_no_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
364
+ assert not proxy_no_kfp ._keep_file_open
357
365
for i in range (voxels .shape [0 ]):
358
366
x , y , z = [int (c ) for c in voxels [i , :]]
359
367
assert proxy_no_kfp [x , y , z ] == x * 100 + y * 10 + z
360
- assert numopeners [ 0 ] == i + 1
361
- numopeners [ 0 ] = 0
368
+ assert CountingImageOpener . numOpeners == i + 1
369
+ CountingImageOpener . numOpeners = 0
362
370
proxy_kfp = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ),
363
371
keep_file_open = True )
372
+ assert proxy_kfp ._keep_file_open
364
373
for i in range (voxels .shape [0 ]):
365
374
x , y , z = [int (c ) for c in voxels [i , :]]
366
375
assert proxy_kfp [x , y , z ] == x * 100 + y * 10 + z
367
- assert numopeners [ 0 ] == 1
376
+ assert CountingImageOpener . numOpeners == 1
368
377
# Test that the keep_file_open flag has no effect if an open file
369
378
# handle is passed in
370
379
with open (fname , 'rb' ) as fobj :
371
380
proxy_no_kfp = ArrayProxy (fobj , ((10 , 10 , 10 ), dtype ),
372
381
keep_file_open = False )
382
+ assert not proxy_no_kfp ._keep_file_open
373
383
for i in range (voxels .shape [0 ]):
374
384
assert proxy_no_kfp [x , y , z ] == x * 100 + y * 10 + z
375
385
assert not fobj .closed
@@ -378,6 +388,7 @@ def __init__(self, *args, **kwargs):
378
388
assert not fobj .closed
379
389
proxy_kfp = ArrayProxy (fobj , ((10 , 10 , 10 ), dtype ),
380
390
keep_file_open = True )
391
+ assert not proxy_kfp ._keep_file_open
381
392
for i in range (voxels .shape [0 ]):
382
393
assert proxy_kfp [x , y , z ] == x * 100 + y * 10 + z
383
394
assert not fobj .closed
@@ -386,6 +397,27 @@ def __init__(self, *args, **kwargs):
386
397
assert not fobj .closed
387
398
388
399
400
+ def test_keep_file_open_default ():
401
+ # Test the behaviour of the keep_file_open __init__ flag, when it is set to
402
+ # its default value
403
+ dtype = np .float32
404
+ data = np .arange (1000 , dtype = dtype ).reshape ((10 , 10 , 10 ))
405
+ voxels = np .random .randint (0 , 10 , (10 , 3 ))
406
+ mockmod = mock .MagicMock ()
407
+ with InTemporaryDirectory ():
408
+ fname = 'testdata.gz'
409
+ with gzip .open (fname , 'wb' ) as fobj :
410
+ fobj .write (data .tostring (order = 'F' ))
411
+ # If have_indexed_gzip, then keep_file_open should be True
412
+ with mock .patch .dict ('sys.modules' , {'indexed_gzip' : mockmod }), \
413
+ mock .patch ('indexed_gzip.SafeIndexedGzipFile' , gzip .GzipFile ):
414
+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
415
+ assert proxy ._keep_file_open
416
+ # If no have_indexed_gzip, then keep_file_open should be False
417
+ with mock .patch .dict ('sys.modules' , {'indexed_gzip' : None }):
418
+ proxy = ArrayProxy (fname , ((10 , 10 , 10 ), dtype ))
419
+ assert not proxy ._keep_file_open
420
+
389
421
def test_pickle_lock ():
390
422
# Test that ArrayProxy can be pickled, and that thread lock is created
391
423
0 commit comments