Skip to content

Commit 2ac2d74

Browse files
committed
BF: make Bomber raise AttributeError instance
Bomber instance was causing problems for Python 3 inspect module because it was raising an error for hasattr checks. Make BomberError that is instance of AttributeError to allow inspect to - er - inspect.
1 parent ebdcc01 commit 2ac2d74

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

nibabel/data.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
1818
'path to the package correctly?')
1919

2020

21-
class DataError(OSError):
21+
class DataError(Exception):
22+
pass
23+
24+
25+
class BomberError(DataError, AttributeError):
26+
""" Error when trying to access Bomber instance
27+
28+
Should be instance of AttributeError to allow Python 3 inspect to do various
29+
``hasattr`` checks without raising an error
30+
"""
2231
pass
2332

2433

@@ -302,7 +311,7 @@ def __init__(self, name, msg):
302311

303312
def __getattr__(self, attr_name):
304313
''' Raise informative error accessing not-found attributes '''
305-
raise DataError(
314+
raise BomberError(
306315
'Trying to access attribute "%s" '
307316
'of non-existent data "%s"\n\n%s\n' %
308317
(attr_name, self.name, self.msg))

nibabel/tests/test_data.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
from nose import with_setup
2121

22-
from nose.tools import assert_equal, \
23-
assert_raises, raises
22+
from nose.tools import (assert_equal, assert_raises, raises, assert_false)
2423

2524
from .test_environment import (setup_environment,
2625
teardown_environment,
@@ -241,6 +240,11 @@ def test_bomber():
241240
res = b.any_attribute
242241

243242

243+
def test_bomber_inspect():
244+
b = Bomber('bomber example', 'a message')
245+
assert_false(hasattr(b, 'any_attribute'))
246+
247+
244248
@with_environment
245249
def test_datasource_or_bomber():
246250
pkg_def = dict(

0 commit comments

Comments
 (0)