Skip to content

Commit a128e27

Browse files
authored
Fix silently ignored AttributeError in custom accessors (#935)
Fixes GH933
1 parent 2c58312 commit a128e27

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

xarray/core/extensions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import traceback
2+
13
from .dataarray import DataArray
24
from .dataset import Dataset
5+
from .pycompat import PY2
36

47

58
class AccessorRegistrationError(Exception):
@@ -16,7 +19,16 @@ def __get__(self, obj, cls):
1619
if obj is None:
1720
# we're accessing the attribute of the class, i.e., Dataset.geo
1821
return self._accessor
19-
accessor_obj = self._accessor(obj)
22+
try:
23+
accessor_obj = self._accessor(obj)
24+
except AttributeError:
25+
# __getattr__ on data object will swallow any AttributeErrors raised
26+
# when initializing the accessor, so we need to raise as something
27+
# else (GH933):
28+
msg = 'error initializing %r accessor.' % self._name
29+
if PY2:
30+
msg += ' Full traceback:\n' + traceback.format_exc()
31+
raise RuntimeError(msg)
2032
# Replace the property with the accessor object. Inspired by:
2133
# http://www.pydanny.com/cached-property.html
2234
# We need to use object.__setattr__ because we overwrite __setattr__ on

xarray/test/test_extensions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,14 @@ def test_pickle_dataarray(self):
7474
assert array.example_accessor is array.example_accessor
7575
array_restored = pickle.loads(pickle.dumps(array))
7676
assert array.identical(array_restored)
77+
78+
def test_broken_accessor(self):
79+
# regression test for GH933
80+
81+
@xr.register_dataset_accessor('stupid_accessor')
82+
class BrokenAccessor(object):
83+
def __init__(self, xarray_obj):
84+
raise AttributeError('broken')
85+
86+
with self.assertRaisesRegexp(RuntimeError, 'error initializing'):
87+
xr.Dataset().stupid_accessor

0 commit comments

Comments
 (0)