27
27
"""
28
28
from contextlib import contextmanager
29
29
from threading import RLock
30
+ import warnings
30
31
31
32
import numpy as np
32
33
40
41
"""This flag controls whether a new file handle is created every time an image
41
42
is accessed through an ``ArrayProxy``, or a single file handle is created and
42
43
used for the lifetime of the ``ArrayProxy``. It should be set to one of
43
- ``True``, ``False``, or ``'auto' ``.
44
+ ``True`` or ``False ``.
44
45
45
46
Management of file handles will be performed either by ``ArrayProxy`` objects,
46
47
or by the ``indexed_gzip`` package if it is used.
47
48
48
49
If this flag is set to ``True``, a single file handle is created and used. If
49
- ``False``, a new file handle is created every time the image is accessed. For
50
- gzip files, if ``'auto'``, and the optional ``indexed_gzip`` dependency is
51
- present, a single file handle is created and persisted. If ``indexed_gzip`` is
52
- not available, behaviour is the same as if ``keep_file_open is False``.
50
+ ``False``, a new file handle is created every time the image is accessed.
51
+ If this flag is set to ``'auto'``, a ``DeprecationWarning`` will be raised, which
52
+ will become a ``ValueError`` in nibabel 3.0.0.
53
53
54
54
If this is set to any other value, attempts to create an ``ArrayProxy`` without
55
55
specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
56
56
raised.
57
57
58
- .. warning:: Setting this flag to a value of ``'auto'`` will become deprecated
59
- behaviour in version 2.4.0 . Support for ``'auto'`` will be removed
58
+ .. warning:: Setting this flag to a value of ``'auto'`` became deprecated
59
+ behaviour in version 2.4.1 . Support for ``'auto'`` will be removed
60
60
in version 3.0.0.
61
61
"""
62
62
KEEP_FILE_OPEN_DEFAULT = False
@@ -100,6 +100,10 @@ class ArrayProxy(object):
100
100
def __init__ (self , file_like , spec , mmap = True , keep_file_open = None ):
101
101
"""Initialize array proxy instance
102
102
103
+ .. deprecated:: 2.4.1
104
+ ``keep_file_open='auto'`` is redundant with `False` and has
105
+ been deprecated. It will raise an error in nibabel 3.0.
106
+
103
107
Parameters
104
108
----------
105
109
file_like : object
@@ -127,18 +131,15 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
127
131
True gives the same behavior as ``mmap='c'``. If `file_like`
128
132
cannot be memory-mapped, ignore `mmap` value and read array from
129
133
file.
130
- keep_file_open : { None, 'auto', True, False }, optional, keyword only
134
+ keep_file_open : { None, True, False }, optional, keyword only
131
135
`keep_file_open` controls whether a new file handle is created
132
136
every time the image is accessed, or a single file handle is
133
137
created and used for the lifetime of this ``ArrayProxy``. If
134
138
``True``, a single file handle is created and used. If ``False``,
135
- a new file handle is created every time the image is accessed. If
136
- ``'auto'``, and the optional ``indexed_gzip`` dependency is
137
- present, a single file handle is created and persisted. If
138
- ``indexed_gzip`` is not available, behaviour is the same as if
139
- ``keep_file_open is False``. If ``file_like`` is an open file
140
- handle, this setting has no effect. The default value (``None``)
141
- will result in the value of ``KEEP_FILE_OPEN_DEFAULT`` being used.
139
+ a new file handle is created every time the image is accessed.
140
+ If ``file_like`` is an open file handle, this setting has no
141
+ effect. The default value (``None``) will result in the value of
142
+ ``KEEP_FILE_OPEN_DEFAULT`` being used.
142
143
"""
143
144
if mmap not in (True , False , 'c' , 'r' ):
144
145
raise ValueError ("mmap should be one of {True, False, 'c', 'r'}" )
@@ -236,17 +237,9 @@ def _should_keep_file_open(self, file_like, keep_file_open):
236
237
In this case, file handle management is delegated to the
237
238
``indexed_gzip`` library.
238
239
239
- 5. If ``keep_file_open`` is ``'auto'``, ``file_like`` is a path to a
240
- ``.gz`` file, and ``indexed_gzip`` is present, both internal flags
241
- are set to ``True``.
242
-
243
- 6. If ``keep_file_open`` is ``'auto'``, and ``file_like`` is not a
244
- path to a ``.gz`` file, or ``indexed_gzip`` is not present, both
245
- internal flags are set to ``False``.
246
-
247
- Note that a value of ``'auto'`` for ``keep_file_open`` will become
248
- deprecated behaviour in version 2.4.0, and support for ``'auto'`` will
249
- be removed in version 3.0.0.
240
+ .. deprecated:: 2.4.1
241
+ ``keep_file_open='auto'`` is redundant with `False` and has
242
+ been deprecated. It will be removed in nibabel 3.0.
250
243
251
244
Parameters
252
245
----------
@@ -266,20 +259,26 @@ def _should_keep_file_open(self, file_like, keep_file_open):
266
259
"""
267
260
if keep_file_open is None :
268
261
keep_file_open = KEEP_FILE_OPEN_DEFAULT
269
- if keep_file_open not in ('auto' , True , False ):
270
- raise ValueError ('keep_file_open should be one of {None, '
271
- '\' auto\' , True, False}' )
262
+ if keep_file_open == 'auto' :
263
+ warnings .warn ("Setting nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT to 'auto' is "
264
+ "deprecated and will become an error in v3.0." , DeprecationWarning )
265
+ if keep_file_open == 'auto' :
266
+ warnings .warn ("A value of 'auto' for keep_file_open is deprecated and will become an "
267
+ "error in v3.0. You probably want False." , DeprecationWarning )
268
+ elif keep_file_open not in (True , False ):
269
+ raise ValueError ('keep_file_open should be one of {None, True, False}' )
270
+
272
271
# file_like is a handle - keep_file_open is irrelevant
273
272
if hasattr (file_like , 'read' ) and hasattr (file_like , 'seek' ):
274
273
return False , False
275
274
# if the file is a gzip file, and we have_indexed_gzip,
276
275
have_igzip = openers .HAVE_INDEXED_GZIP and file_like .endswith ('.gz' )
276
+ # XXX Remove in v3.0
277
277
if keep_file_open == 'auto' :
278
278
return have_igzip , have_igzip
279
- elif keep_file_open :
280
- return True , True
281
- else :
282
- return False , have_igzip
279
+
280
+ persist_opener = keep_file_open or have_igzip
281
+ return keep_file_open , persist_opener
283
282
284
283
@property
285
284
@deprecate_with_version ('ArrayProxy.header deprecated' , '2.2' , '3.0' )
0 commit comments