Skip to content

Commit 093ae6e

Browse files
committed
RF: Indexed_gzip import test performed once at top of openers.py. Additional
lock protection in ArrayProxy, on call to volumeutils.array_from_file.
1 parent d9a1ebd commit 093ae6e

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

nibabel/arrayproxy.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from .volumeutils import array_from_file, apply_read_scaling
3535
from .fileslice import fileslice
3636
from .keywordonly import kw_only_meth
37-
from .openers import ImageOpener
37+
from .openers import ImageOpener, HAVE_INDEXED_GZIP
3838

3939

4040
class ArrayProxy(object):
@@ -195,13 +195,7 @@ def _should_keep_file_open(self, file_like, keep_file_open):
195195
return bool(keep_file_open)
196196
# Otherwise, if file_like is gzipped, and we have_indexed_gzip, we set
197197
# keep_file_open to True, else we set it to False
198-
try:
199-
import indexed_gzip
200-
have_indexed_gzip = True
201-
except ImportError:
202-
have_indexed_gzip = False
203-
204-
return have_indexed_gzip and file_like.endswith('gz')
198+
return HAVE_INDEXED_GZIP and file_like.endswith('gz')
205199

206200
@property
207201
@deprecate_with_version('ArrayProxy.header deprecated', '2.2', '3.0')
@@ -259,7 +253,7 @@ def get_unscaled(self):
259253
260254
This is an optional part of the proxy API
261255
"""
262-
with self._get_fileobj() as fileobj:
256+
with self._get_fileobj() as fileobj, self._lock:
263257
raw_data = array_from_file(self._shape,
264258
self._dtype,
265259
fileobj,

nibabel/openers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
import sys
1515
from os.path import splitext
1616

17+
# is indexed_gzip present?
18+
try:
19+
from indexed_gzip import SafeIndexedGzipFile
20+
HAVE_INDEXED_GZIP = True
21+
except:
22+
HAVE_INDEXED_GZIP = False
23+
1724

1825
# The largest memory chunk that gzip can use for reads
1926
GZIP_MAX_READ_CHUNK = 100 * 1024 * 1024 # 100Mb
@@ -62,13 +69,6 @@ def readinto(self, buf):
6269

6370
def _gzip_open(fileish, mode='rb', compresslevel=9):
6471

65-
# is indexed_gzip present?
66-
try:
67-
from indexed_gzip import SafeIndexedGzipFile
68-
have_indexed_gzip = True
69-
except:
70-
have_indexed_gzip = False
71-
7272
# is this a file? if not we assume it is a string
7373
is_file = hasattr(fileish, 'read') and hasattr(fileish, 'write')
7474

@@ -77,7 +77,7 @@ def _gzip_open(fileish, mode='rb', compresslevel=9):
7777
mode = fileish.mode
7878

7979
# use indexed_gzip if possible for faster read access
80-
if mode == 'rb' and have_indexed_gzip:
80+
if mode == 'rb' and HAVE_INDEXED_GZIP:
8181
if is_file:
8282
gzip_file = SafeIndexedGzipFile(fid=fileish)
8383
else:

0 commit comments

Comments
 (0)