Skip to content

Commit 19a95f2

Browse files
Merge pull request #516 from pauldmccarthy/wrapstruct_fix
MRG: Bugfix to WrapStruct.get A minor bug in WrapStruct.get - it fails for header fields with values that evaluates to False (e.g. 0)
2 parents 30c19e4 + 654e8af commit 19a95f2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

nibabel/tests/test_wrapstruct.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ def test_mappingness(self):
183183
assert_equal(hdr.get(keys[0]), vals[0])
184184
assert_equal(hdr.get(keys[0], 'default'), vals[0])
185185

186+
# make sure .get returns values which evaluate to False. We have to
187+
# use a different falsy value depending on the data type of the first
188+
# header field.
189+
falsyval = 0 if np.issubdtype(hdr_dt[0], np.number) else b''
190+
191+
hdr[keys[0]] = falsyval
192+
assert_equal(hdr[keys[0]], falsyval)
193+
assert_equal(hdr.get(keys[0]), falsyval)
194+
assert_equal(hdr.get(keys[0], -1), falsyval)
195+
196+
186197
def test_endianness_ro(self):
187198
# endianness is a read only property
188199
''' Its use in initialization tested in the init tests.

nibabel/wrapstruct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def items(self):
343343

344344
def get(self, k, d=None):
345345
''' Return value for the key k if present or d otherwise'''
346-
return (k in self.keys()) and self._structarr[k] or d
346+
return self._structarr[k] if k in self.keys() else d
347347

348348
def check_fix(self, logger=None, error_level=None):
349349
''' Check structured data with checks

0 commit comments

Comments
 (0)