##// END OF EJS Templates
BUG: Do not store class-specific state on TraitTypes since they may be shared through subclassing.
Robert Kern -
Show More
@@ -142,15 +142,27 b' class TestTraitType(TestCase):'
142
142
143 a = A()
143 a = A()
144 self.assertEquals(a._trait_values, {})
144 self.assertEquals(a._trait_values, {})
145 self.assertEquals(a._trait_dyn_inits.keys(), ['x'])
145 self.assertEquals(a.x, 11)
146 self.assertEquals(a.x, 11)
146 self.assertEquals(a._trait_values, {'x': 11})
147 self.assertEquals(a._trait_values, {'x': 11})
147 b = B()
148 b = B()
148 self.assertEquals(b._trait_values, {'x': 20})
149 self.assertEquals(b._trait_values, {'x': 20})
150 self.assertEquals(a._trait_dyn_inits.keys(), ['x'])
149 self.assertEquals(b.x, 20)
151 self.assertEquals(b.x, 20)
150 c = C()
152 c = C()
151 self.assertEquals(c._trait_values, {})
153 self.assertEquals(c._trait_values, {})
154 self.assertEquals(a._trait_dyn_inits.keys(), ['x'])
152 self.assertEquals(c.x, 21)
155 self.assertEquals(c.x, 21)
153 self.assertEquals(c._trait_values, {'x': 21})
156 self.assertEquals(c._trait_values, {'x': 21})
157 # Ensure that the base class remains unmolested when the _default
158 # initializer gets overridden in a subclass.
159 a = A()
160 c = C()
161 self.assertEquals(a._trait_values, {})
162 self.assertEquals(a._trait_dyn_inits.keys(), ['x'])
163 self.assertEquals(a.x, 11)
164 self.assertEquals(a._trait_values, {'x': 11})
165
154
166
155
167
156 class TestHasTraitsMeta(TestCase):
168 class TestHasTraitsMeta(TestCase):
@@ -262,7 +262,7 b' class TraitType(object):'
262 obj._trait_values[self.name] = newdv
262 obj._trait_values[self.name] = newdv
263 return
263 return
264 # Complete the dynamic initialization.
264 # Complete the dynamic initialization.
265 self.dynamic_initializer = cls.__dict__[meth_name]
265 obj._trait_dyn_inits[self.name] = cls.__dict__[meth_name]
266
266
267 def __get__(self, obj, cls=None):
267 def __get__(self, obj, cls=None):
268 """Get the value of the trait by self.name for the instance.
268 """Get the value of the trait by self.name for the instance.
@@ -279,8 +279,8 b' class TraitType(object):'
279 value = obj._trait_values[self.name]
279 value = obj._trait_values[self.name]
280 except KeyError:
280 except KeyError:
281 # Check for a dynamic initializer.
281 # Check for a dynamic initializer.
282 if hasattr(self, 'dynamic_initializer'):
282 if self.name in obj._trait_dyn_inits:
283 value = self.dynamic_initializer(obj)
283 value = obj._trait_dyn_inits[self.name](obj)
284 # FIXME: Do we really validate here?
284 # FIXME: Do we really validate here?
285 value = self._validate(obj, value)
285 value = self._validate(obj, value)
286 obj._trait_values[self.name] = value
286 obj._trait_values[self.name] = value
@@ -318,11 +318,6 b' class TraitType(object):'
318 else:
318 else:
319 return value
319 return value
320
320
321 def set_dynamic_initializer(self, method):
322 """ Set the dynamic initializer method, if any.
323 """
324 self.dynamic_initializer = method
325
326 def info(self):
321 def info(self):
327 return self.info_text
322 return self.info_text
328
323
@@ -399,6 +394,7 b' class HasTraits(object):'
399 inst = new_meth(cls, **kw)
394 inst = new_meth(cls, **kw)
400 inst._trait_values = {}
395 inst._trait_values = {}
401 inst._trait_notifiers = {}
396 inst._trait_notifiers = {}
397 inst._trait_dyn_inits = {}
402 # Here we tell all the TraitType instances to set their default
398 # Here we tell all the TraitType instances to set their default
403 # values on the instance.
399 # values on the instance.
404 for key in dir(cls):
400 for key in dir(cls):
General Comments 0
You need to be logged in to leave comments. Login now