From a8f70928863cf637bc3419917a04a7526e757469 Mon Sep 17 00:00:00 2001 From: will Date: Fri, 10 Aug 2012 11:47:36 -0400 Subject: [PATCH] errorvalue and errorhandler attributes for Property base class --- kivy/properties.pxd | 3 +++ kivy/properties.pyx | 29 ++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/kivy/properties.pxd b/kivy/properties.pxd index 20ea78ce0..88820f3ba 100644 --- a/kivy/properties.pxd +++ b/kivy/properties.pxd @@ -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) diff --git a/kivy/properties.pyx b/kivy/properties.pyx index 77a8f4766..f21c415ac 100644 --- a/kivy/properties.pyx +++ b/kivy/properties.pyx @@ -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 = 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 - self.check(obj, value) + + 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