Skip to content

Commit 172bb61

Browse files
committed
DOC: Clarification in read_segments doc, and inline comments in
openers._gzip_open function
1 parent 052ec60 commit 172bb61

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

nibabel/fileslice.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,9 @@ def read_segments(fileobj, segments, n_bytes, lock=None):
637637
lock : threading.Lock
638638
If provided, used to ensure that paired calls to ``seek`` and ``read``
639639
cannot be interrupted by another thread accessing the same ``fileobj``.
640-
640+
Each thread which accesses the same file via ``read_segments`` must
641+
share a lock in order to ensure that the file access is thread-safe.
642+
A lock does not need to be provided for single-threaded access.
641643
642644
Returns
643645
-------
@@ -647,11 +649,14 @@ def read_segments(fileobj, segments, n_bytes, lock=None):
647649
"""
648650
# Make a dummy lock-like thing to make the code below a bit nicer
649651
if lock is None:
652+
650653
class DummyLock(object):
651654
def __enter__(self):
652655
pass
656+
653657
def __exit__(self, exc_type, exc_val, exc_tb):
654658
return False
659+
655660
lock = DummyLock()
656661

657662
if len(segments) == 0:

nibabel/openers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def _gzip_open(fileish, mode='rb', compresslevel=9):
7272
# is this a file? if not we assume it is a string
7373
is_file = hasattr(fileish, 'read') and hasattr(fileish, 'write') and \
7474
hasattr(fileish, 'mode')
75+
76+
# If we've been given a file object, we can't change its mode.
7577
if is_file:
7678
mode = fileish.mode
7779

@@ -82,6 +84,9 @@ def _gzip_open(fileish, mode='rb', compresslevel=9):
8284
gzip_file = SafeIndexedGzipFile(fid=fileish, **kwargs)
8385
else:
8486
gzip_file = SafeIndexedGzipFile(filename=fileish, **kwargs)
87+
88+
# Fall-back to built-in GzipFile (wrapped with the BufferedGzipFile class
89+
# defined above)
8590
else:
8691
gzip_file = BufferedGzipFile(fileish, mode, compresslevel)
8792

0 commit comments

Comments
 (0)