Skip to content

Commit 4c850cb

Browse files
committed
RF,STY: Make sure that non-gzip file handles are dropped when keep_file_open
== 'auto'.
1 parent db6c6af commit 4c850cb

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

nibabel/arrayproxy.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
``True``, ``False``, or ``'auto'``.
4444
4545
If ``True``, a single file handle is created and used. If ``False``, a new
46-
file handle is created every time the image is accessed. If ``'auto'``, and
47-
the optional ``indexed_gzip`` dependency is present, a single file handle is
48-
created and persisted. If ``indexed_gzip`` is not available, behaviour is the
49-
same as if ``keep_file_open is False``.
46+
file handle is created every time the image is accessed. For gzip files, if
47+
``'auto'``, and the optional ``indexed_gzip`` dependency is present, a single
48+
file handle is created and persisted. If ``indexed_gzip`` is not available,
49+
behaviour is the same as if ``keep_file_open is False``.
5050
5151
If this is set to any other value, attempts to create an ``ArrayProxy`` without
5252
specifying the ``keep_file_open`` flag will result in a ``ValueError`` being
@@ -186,7 +186,7 @@ def _should_keep_file_open(self, file_like, keep_file_open):
186186
The return value is derived from these rules:
187187
188188
- If ``file_like`` is a file(-like) object, ``False`` is returned.
189-
Otherwise, ``file_like`` is assumed to be a file name
189+
Otherwise, ``file_like`` is assumed to be a file name.
190190
- If ``keep_file_open`` is ``auto``, and ``indexed_gzip`` is
191191
not available, ``False`` is returned.
192192
- Otherwise, the value of ``keep_file_open`` is returned unchanged.
@@ -214,7 +214,8 @@ def _should_keep_file_open(self, file_like, keep_file_open):
214214
if hasattr(file_like, 'read') and hasattr(file_like, 'seek'):
215215
return False
216216
# don't have indexed_gzip - auto -> False
217-
if keep_file_open == 'auto' and not HAVE_INDEXED_GZIP:
217+
if keep_file_open == 'auto' and not (HAVE_INDEXED_GZIP and
218+
file_like.endswith('.gz')):
218219
return False
219220
return keep_file_open
220221

@@ -260,7 +261,7 @@ def _get_fileobj(self):
260261
A newly created ``ImageOpener`` instance, or an existing one,
261262
which provides access to the file.
262263
"""
263-
if bool(self._keep_file_open):
264+
if self._keep_file_open:
264265
if not hasattr(self, '_opener'):
265266
self._opener = ImageOpener(
266267
self.file_like, keep_open=self._keep_file_open)

0 commit comments

Comments
 (0)