Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion xarray/core/extensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import traceback

from .dataarray import DataArray
from .dataset import Dataset
from .pycompat import PY2


class AccessorRegistrationError(Exception):
Expand All @@ -16,7 +19,16 @@ def __get__(self, obj, cls):
if obj is None:
# we're accessing the attribute of the class, i.e., Dataset.geo
return self._accessor
accessor_obj = self._accessor(obj)
try:
accessor_obj = self._accessor(obj)
except AttributeError:
# __getattr__ on data object will swallow any AttributeErrors raised
# when initializing the accessor, so we need to raise as something
# else (GH933):
msg = 'error initializing %r accessor.' % self._name
if PY2:
msg += ' Full traceback:\n' + traceback.format_exc()
raise RuntimeError(msg)
# Replace the property with the accessor object. Inspired by:
# http://www.pydanny.com/cached-property.html
# We need to use object.__setattr__ because we overwrite __setattr__ on
Expand Down
11 changes: 11 additions & 0 deletions xarray/test/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ def test_pickle_dataarray(self):
assert array.example_accessor is array.example_accessor
array_restored = pickle.loads(pickle.dumps(array))
assert array.identical(array_restored)

def test_broken_accessor(self):
# regression test for GH933

@xr.register_dataset_accessor('stupid_accessor')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to name it otherwise ;)

class BrokenAccessor(object):
def __init__(self, xarray_obj):
raise AttributeError('broken')

with self.assertRaisesRegexp(RuntimeError, 'error initializing'):
xr.Dataset().stupid_accessor