2727"""
2828from contextlib import contextmanager
2929from threading import RLock
30+ import warnings
3031
3132import numpy as np
3233
4041"""This flag controls whether a new file handle is created every time an image
4142is accessed through an ``ArrayProxy``, or a single file handle is created and
4243used for the lifetime of the ``ArrayProxy``. It should be set to one of
43- ``True``, ``False``, or ``'auto' ``.
44+ ``True`` or ``False ``.
4445
4546Management of file handles will be performed either by ``ArrayProxy`` objects,
4647or by the ``indexed_gzip`` package if it is used.
4748
4849If 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.
5353
5454If this is set to any other value, attempts to create an ``ArrayProxy`` without
5555specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
5656raised.
5757
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
6060 in version 3.0.0.
6161"""
6262KEEP_FILE_OPEN_DEFAULT = False
@@ -100,6 +100,10 @@ class ArrayProxy(object):
100100 def __init__ (self , file_like , spec , mmap = True , keep_file_open = None ):
101101 """Initialize array proxy instance
102102
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+
103107 Parameters
104108 ----------
105109 file_like : object
@@ -127,18 +131,15 @@ def __init__(self, file_like, spec, mmap=True, keep_file_open=None):
127131 True gives the same behavior as ``mmap='c'``. If `file_like`
128132 cannot be memory-mapped, ignore `mmap` value and read array from
129133 file.
130- keep_file_open : { None, 'auto', True, False }, optional, keyword only
134+ keep_file_open : { None, True, False }, optional, keyword only
131135 `keep_file_open` controls whether a new file handle is created
132136 every time the image is accessed, or a single file handle is
133137 created and used for the lifetime of this ``ArrayProxy``. If
134138 ``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.
142143 """
143144 if mmap not in (True , False , 'c' , 'r' ):
144145 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):
236237 In this case, file handle management is delegated to the
237238 ``indexed_gzip`` library.
238239
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.
250243
251244 Parameters
252245 ----------
@@ -266,20 +259,26 @@ def _should_keep_file_open(self, file_like, keep_file_open):
266259 """
267260 if keep_file_open is None :
268261 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+
272271 # file_like is a handle - keep_file_open is irrelevant
273272 if hasattr (file_like , 'read' ) and hasattr (file_like , 'seek' ):
274273 return False , False
275274 # if the file is a gzip file, and we have_indexed_gzip,
276275 have_igzip = openers .HAVE_INDEXED_GZIP and file_like .endswith ('.gz' )
276+ # XXX Remove in v3.0
277277 if keep_file_open == 'auto' :
278278 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
283282
284283 @property
285284 @deprecate_with_version ('ArrayProxy.header deprecated' , '2.2' , '3.0' )
0 commit comments