Picon
Gravatar

Re: [Traits] feature request

Martin Chilvers wrote:
>> Clearly, this will break the trait_set method when 
>> trait_change_notify=False.  I propose to add a 
>> _get_trait_change_notify() to the ctraits.c to be able to get this so 
>> users can write:
>>
>>  if self._get_trait_change_notify():
>>      ...
>>
>> etc. to work around this.
> 
> Hmmm... I wouldn't want to have to put this line in every property setter... smells like 
> duplication... Couldn't the 'trait_property_changed' method handle the check?

Hmm, thats a good point and the patch would be extremely simple too. 
Just do nothing if the flag has been set.  So here is an updated patch 
and test case.

cheers,
prabhu
from enthought.traits.api import HasTraits, Float, Int

import unittest

class Test(HasTraits):
    x = Float
    _test = Int(0)
    def _x_changed(self, value):
        self._test += 1

class TestTraitChangeNotify(unittest.TestCase):
    def test_trait_change_notify(self):
        "Test if _get/_trait_change_notify work."
        t = Test()
        t.x = 10.0
        self.assertEqual(t._test, 1)
        # Turn off notification.
        t._trait_change_notify(False)
        t.x = 20.0
        self.assertEqual(t._test, 1)
        t.trait_property_changed('x', 10.0, 20.0)
        self.assertEqual(t._test, 1)
        # Turn it back on.
        t._trait_change_notify(True)
        t.x = 30.0
        self.assertEqual(t._test, 2)

if __name__ == '__main__':
    unittest.main()
Index: enthought/traits/ctraits.c
===================================================================
--- enthought/traits/ctraits.c	(revision 21706)
+++ enthought/traits/ctraits.c	(working copy)
@@ -1342,7 +1342,10 @@
     PyListObject * onotifiers;
     int null_new_value;
     int rc = 0;
-    
+
+    if ((obj->flags & HASTRAITS_NO_NOTIFY) != 0)
+        return 0;
+
     if ( (trait = (trait_object *) get_trait( obj, name, -1 )) == NULL )
         return -1;

_______________________________________________
Enthought-dev mailing list
Enthought-dev@...
https://mail.enthought.com/mailman/listinfo/enthought-dev

Gmane