errorvalue and errorhandler attributes for Property base class

This commit is contained in:
will 2012-08-10 11:47:36 -04:00
parent bdc4c13119
commit a8f7092886
2 changed files with 31 additions and 1 deletions

View File

@ -2,6 +2,9 @@ cdef class Property:
cdef str _name
cdef int allownone
cdef object defaultvalue
cdef object errorvalue
cdef object errorhandler
cdef int errorvalue_set
cdef init_storage(self, dict storage)
cpdef link(self, object obj, str name)
cpdef link_deps(self, object obj, str name)

View File

@ -191,10 +191,18 @@ cdef class Property:
self._name = ''
self.allownone = 0
self.defaultvalue = None
self.errorvalue = None
self.errorhandler = None
self.errorvalue_set = 0
def __init__(self, defaultvalue, **kw):
self.defaultvalue = defaultvalue
self.allownone = <int>kw.get('allownone', 0)
self.errorvalue = kw.get('errorvalue', None)
self.errorhandler = kw.get('errorhandler', None)
if 'errorvalue' in kw: self.errorvalue_set = 1
property name:
def __get__(self):
@ -203,8 +211,12 @@ cdef class Property:
cdef init_storage(self, dict storage):
storage['value'] = self.defaultvalue
storage['allownone'] = self.allownone
storage['errorvalue'] = self.errorvalue
storage['errorhandler'] = self.errorhandler
storage['errorvalue_set'] = self.errorvalue_set
storage['observers'] = []
cpdef link(self, object obj, str name):
'''Link the instance with its real name.
@ -266,7 +278,22 @@ cdef class Property:
realvalue = d['value']
if self.compare_value(realvalue, value):
return False
try:
self.check(obj, value)
except ValueError as e:
errorvalue = obj.__storage[self._name]['errorvalue']
errorhandler = obj.__storage[self._name]['errorhandler']
errorvalue_set = obj.__storage[self._name]['errorvalue_set']
if errorvalue_set == 1:
value = errorvalue
self.check(obj, value)
elif errorhandler is not None:
value = errorhandler(value)
self.check(obj, value)
else:
raise e
d['value'] = value
self.dispatch(obj)
return True