summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2015-03-11 22:13:00 +0200
committerKai Huuhko <kai.huuhko@gmail.com>2015-03-11 22:13:00 +0200
commit247eb9e452c2490ff1312e6ba2af63937f11a4c6 (patch)
tree2300aa1450bfc0d204a4e9b97905cc21c22a57b1
parenta407e1129807922a087c4bf16bc2ae89b30b4461 (diff)
Evas.SmartObject: Revert SmartCb add/del mechanism partially
We need to keep a better track of the spec references so they don't get mixed up when user adds and deletes the callbacks.
-rw-r--r--efl/evas/efl.evas_object_smart.pxi37
-rw-r--r--include/efl.evas.pxd3
-rw-r--r--tests/elementary/test_01_basics.py30
3 files changed, 58 insertions, 12 deletions
diff --git a/efl/evas/efl.evas_object_smart.pxi b/efl/evas/efl.evas_object_smart.pxi
index cc9ee11..f22b4f1 100644
--- a/efl/evas/efl.evas_object_smart.pxi
+++ b/efl/evas/efl.evas_object_smart.pxi
@@ -598,7 +598,7 @@ cdef class SmartObject(Object):
598 parameter ``smart`` 598 parameter ``smart``
599 """ 599 """
600 def __cinit__(self): 600 def __cinit__(self):
601 self._owned_references = list() 601 self._smart_callback_specs = dict()
602 602
603 def __init__(self, Canvas canvas not None, Smart smart not None, **kwargs): 603 def __init__(self, Canvas canvas not None, Smart smart not None, **kwargs):
604 #_smart_classes.append(<uintptr_t>cls_def) 604 #_smart_classes.append(<uintptr_t>cls_def)
@@ -756,8 +756,8 @@ cdef class SmartObject(Object):
756 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event) 756 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event)
757 757
758 spec = (<uintptr_t><void *>event_conv, func, args, kargs) 758 spec = (<uintptr_t><void *>event_conv, func, args, kargs)
759 self._owned_references.append(spec) 759 lst = self._smart_callback_specs.setdefault(event, [])
760 #Py_INCREF(spec) 760 lst.append(spec)
761 761
762 evas_object_smart_callback_add(self.obj, 762 evas_object_smart_callback_add(self.obj,
763 <const char *>event if event is not None else NULL, 763 <const char *>event if event is not None else NULL,
@@ -784,21 +784,36 @@ cdef class SmartObject(Object):
784 784
785 """ 785 """
786 cdef: 786 cdef:
787 void *tmp
788 tuple spec 787 tuple spec
788 int found = 0
789 int i
790 void *tmp
789 791
790 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event) 792 if isinstance(event, unicode): event = PyUnicode_AsUTF8String(event)
791 793
792 tmp = evas_object_smart_callback_del(self.obj, 794 lst = self._smart_callback_specs.get(event, None)
795
796 if lst is None:
797 raise ValueError("No callbacks registered for the given event type")
798
799 for i, spec in enumerate(lst):
800 if spec[1] is func:
801 found = 1
802 break
803
804 if found == 0:
805 raise ValueError("func not registered")
806
807 tmp = evas_object_smart_callback_del_full(self.obj,
793 <const char *>event if event is not None else NULL, 808 <const char *>event if event is not None else NULL,
794 _smart_callback) 809 _smart_callback,
810 <void *>spec
811 )
795 812
796 if tmp == NULL: 813 if tmp == NULL:
797 return 1 814 raise RuntimeError("Something went wrong while unregistering!")
798 else: 815
799 spec = <tuple>tmp 816 lst.pop(i)
800 self._owned_references.remove(spec)
801 #Py_DECREF(spec)
802 817
803 return 1 818 return 1
804 819
diff --git a/include/efl.evas.pxd b/include/efl.evas.pxd
index 2f1c94f..f9326b8 100644
--- a/include/efl.evas.pxd
+++ b/include/efl.evas.pxd
@@ -800,6 +800,7 @@ cdef extern from "Evas.h":
800 void evas_object_smart_data_set(Evas_Object *obj, void *data) 800 void evas_object_smart_data_set(Evas_Object *obj, void *data)
801 void evas_object_smart_callback_add(Evas_Object *obj, const char *event, Evas_Smart_Cb func, const void *data) 801 void evas_object_smart_callback_add(Evas_Object *obj, const char *event, Evas_Smart_Cb func, const void *data)
802 void *evas_object_smart_callback_del(Evas_Object *obj, const char *event, Evas_Smart_Cb func) 802 void *evas_object_smart_callback_del(Evas_Object *obj, const char *event, Evas_Smart_Cb func)
803 void *evas_object_smart_callback_del_full(Evas_Object *obj, const char *event, Evas_Smart_Cb func, const void *data)
803 void evas_object_smart_callback_call(Evas_Object *obj, const char *event, void *event_info) 804 void evas_object_smart_callback_call(Evas_Object *obj, const char *event, void *event_info)
804 void evas_object_smart_changed(Evas_Object *obj) 805 void evas_object_smart_changed(Evas_Object *obj)
805 void evas_object_smart_need_recalculate_set(Evas_Object *obj, int value) 806 void evas_object_smart_need_recalculate_set(Evas_Object *obj, int value)
@@ -1211,7 +1212,7 @@ cdef class Textblock(Object):
1211 1212
1212cdef class SmartObject(Object): 1213cdef class SmartObject(Object):
1213 cdef: 1214 cdef:
1214 list _owned_references 1215 dict _smart_callback_specs
1215 int _set_obj(self, cEo *obj) except 0 1216 int _set_obj(self, cEo *obj) except 0
1216 int _callback_add_full(self, event, object(*)(void*), func, tuple args, dict kargs) except 0 1217 int _callback_add_full(self, event, object(*)(void*), func, tuple args, dict kargs) except 0
1217 int _callback_del_full(self, event, object(*)(void*), func) except 0 1218 int _callback_del_full(self, event, object(*)(void*), func) except 0
diff --git a/tests/elementary/test_01_basics.py b/tests/elementary/test_01_basics.py
index e75e08b..a24c4fe 100644
--- a/tests/elementary/test_01_basics.py
+++ b/tests/elementary/test_01_basics.py
@@ -9,6 +9,13 @@ from efl.elementary.button import Button
9 9
10elementary.init() 10elementary.init()
11 11
12def cb1(*args):
13 pass
14
15def cb2(*args):
16 pass
17
18
12class TestElmBasics(unittest.TestCase): 19class TestElmBasics(unittest.TestCase):
13 20
14 def setUp(self): 21 def setUp(self):
@@ -22,6 +29,29 @@ class TestElmBasics(unittest.TestCase):
22 self.assertEqual(Eo.parent_get(o), self.o) 29 self.assertEqual(Eo.parent_get(o), self.o)
23 o.delete() 30 o.delete()
24 31
32 def testCallbacks1(self):
33 self.o.callback_iconified_add(cb1)
34 self.o.callback_iconified_del(cb1)
35
36 def testCallbacks2(self):
37 self.o.callback_iconified_add(cb1)
38 self.o.callback_iconified_add(cb2)
39 self.o.callback_iconified_del(cb1)
40 self.o.callback_iconified_del(cb2)
41
42 def testCallbacks3(self):
43 self.o.callback_iconified_add(cb1)
44 self.o.callback_fullscreen_add(cb1)
45 self.o.callback_iconified_del(cb1)
46 self.o.callback_fullscreen_del(cb1)
47
48 def testCallbacks4(self):
49 self.o.callback_iconified_add(cb1)
50 self.o.callback_fullscreen_add(cb2)
51 self.assertRaises(ValueError, self.o.callback_iconified_del, cb2)
52 self.assertRaises(ValueError, self.o.callback_fullscreen_del, cb1)
53
54
25if __name__ == '__main__': 55if __name__ == '__main__':
26 unittest.main(verbosity=2) 56 unittest.main(verbosity=2)
27 elementary.shutdown() 57 elementary.shutdown()