Skip to content

Commit 572ab00

Browse files
committed
BF: Making all existing unit tests happy
1 parent 32e3b05 commit 572ab00

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

nibabel/arrayproxy.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,20 @@ def __del__(self):
138138
'''If this ``ArrayProxy`` was created with ``keep_file_open=True``,
139139
the open file object is closed if necessary.
140140
'''
141-
if self._keep_file_open and hasattr(self, '_opener'):
142-
if not self._opener.closed:
143-
self._opener.close()
144-
self._opener = None
141+
if hasattr(self, '_opener') and not self._opener.closed:
142+
self._opener.close()
143+
self._opener = None
144+
145+
def __getstate__(self):
146+
'''Returns the state of this ``ArrayProxy`` during pickling. '''
147+
state = self.__dict__.copy()
148+
state.pop('_lock', None)
149+
return state
150+
151+
def __setstate__(self, state):
152+
'''Sets the state of this ``ArrayProxy`` during unpickling. '''
153+
self.__dict__.update(state)
154+
self._lock = Lock()
145155

146156
@property
147157
@deprecate_with_version('ArrayProxy.header deprecated', '2.2', '3.0')

nibabel/fileslice.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
""" Utilities for getting array slices out of file-like objects
22
"""
33
from __future__ import division
4-
from contextlib import contextmanager
54

65
import operator
76
from numbers import Integral
@@ -648,10 +647,12 @@ def read_segments(fileobj, segments, n_bytes, lock=None):
648647
"""
649648
# Make a dummy lock-like thing to make the code below a bit nicer
650649
if lock is None:
651-
@contextmanager
652-
def dummy_lock():
653-
yield
654-
lock = dummy_lock
650+
class DummyLock(object):
651+
def __enter__(self):
652+
pass
653+
def __exit__(self, exc_type, exc_val, exc_tb):
654+
return False
655+
lock = DummyLock()
655656

656657
if len(segments) == 0:
657658
if n_bytes != 0:

nibabel/spm99analyze.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ class Spm99AnalyzeImage(analyze.AnalyzeImage):
245245

246246
@classmethod
247247
@kw_only_meth(1)
248-
def from_file_map(klass, file_map, mmap=True):
249-
''' class method to create image from mapping in `file_map ``
248+
def from_file_map(klass, file_map, mmap=True, keep_file_open=False):
249+
'''class method to create image from mapping in `file_map ``
250250
251251
Parameters
252252
----------
@@ -261,13 +261,19 @@ def from_file_map(klass, file_map, mmap=True):
261261
`mmap` value of True gives the same behavior as ``mmap='c'``. If
262262
image data file cannot be memory-mapped, ignore `mmap` value and
263263
read array from file.
264+
keep_file_open: If the file-ilke(s) is a file name, the default
265+
behaviour is to open a new file handle every time the data is
266+
accessed. If this flag is set to `True``, the file handle will be
267+
opened on the first access, and kept open until this
268+
``ArrayProxy`` is garbage-collected.
264269
265270
Returns
266271
-------
267272
img : Spm99AnalyzeImage instance
273+
268274
'''
269-
ret = super(Spm99AnalyzeImage, klass).from_file_map(file_map,
270-
mmap=mmap)
275+
ret = super(Spm99AnalyzeImage, klass).from_file_map(
276+
file_map, mmap=mmap, keep_file_open=keep_file_open)
271277
try:
272278
matf = file_map['mat'].get_prepare_fileobj()
273279
except IOError:

0 commit comments

Comments
 (0)