[bugfix] PickingResults raised incorrect error on missing attribute

Accessing a non existing attribute raised a KeyError instead of an AttributeError, breaking methods that relied on the correct error type. Maybe this fixes the __setstate__ bug during event picking from GUI.
This commit is contained in:
Darius Arnold 2018-08-08 19:25:11 +02:00
parent d4e2f4a070
commit 45370e2c67

View File

@ -192,7 +192,8 @@ class PickingParameters(object):
class PickingResults(dict): class PickingResults(dict):
""" """
Store picking results Used to store picking results.
PickingResults is a dict like class that adds attribute (dot) access to the dictionaries values.
""" """
def __init__(self): def __init__(self):
@ -229,11 +230,21 @@ class PickingResults(dict):
self.spe = None # symmetrized picking error self.spe = None # symmetrized picking error
self.weight = 4 # weight of onset self.weight = 4 # weight of onset
def __setattr__(self, key, value): # to correctly provide dot access to dictionary attributes, all attribute access of the class is forwarded to the
self[key] = value # dictionary
def __getattr__(self, item):
"""Override getattr to return an AttributeError instead of a KeyError when the instance doesn't have the
attribute.
"""
try:
attr = dict.__getitem__(self, item)
except KeyError:
raise AttributeError('{classname} has no attribute {attrname}'.format(classname=self.__class__.__name__,
attrname=item))
return attr
def __getattr__(self, key): __setattr__ = dict.__setitem__
return self[key] __delattr__ = dict.__delitem__
class PickingContainer: class PickingContainer: