davemds pushed a commit to branch master. http://git.enlightenment.org/bindings/python/python-efl.git/commit/?id=32448918f6ebe7578e29e7269881f954284ddf9b
commit 32448918f6ebe7578e29e7269881f954284ddf9b Author: davemds <[email protected]> Date: Thu Feb 27 22:38:04 2014 +0100 Python-EFL: new 1.9 API Multibuttonentry.format_function_set() Sadly this add 2 new points of reference leaks :( --- TODO | 9 +++++-- efl/elementary/multibuttonentry.pxd | 4 ++- efl/elementary/multibuttonentry.pyx | 38 ++++++++++++++++++++++++++++ examples/elementary/test_multibuttonentry.py | 21 +++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 39af69c..c1dfb72 100644 --- a/TODO +++ b/TODO @@ -3,8 +3,13 @@ BUGS ==== * EdjeEdit: PartState API does not work * Elm.Map: overlays_show segfaults, scrollers in examples are jumpy -* Entry.markup_filter_append() and Fileselector.custom_filter_append() are - leaking badly, fix and check all other place that have the same issue. +* Elementary: when we use custom function callbacks we usually leak some + reference around, some examples: + - Entry.markup_filter_append() + - Fileselector.custom_filter_append() + - Multibuttonentry.format_function_set() + - Multibuttonentry.filter_append() + - Multibuttonentry.filterprepend() Failing unit tests ------------------ diff --git a/efl/elementary/multibuttonentry.pxd b/efl/elementary/multibuttonentry.pxd index db4282a..682b971 100644 --- a/efl/elementary/multibuttonentry.pxd +++ b/efl/elementary/multibuttonentry.pxd @@ -1,10 +1,11 @@ from efl.evas cimport Eina_Bool, const_Eina_List, Evas_Object, const_Evas_Object, Evas_Smart_Cb from object_item cimport Elm_Object_Item, const_Elm_Object_Item, ObjectItem -from libc.string cimport const_char +from libc.string cimport const_char, const_void cdef extern from "Elementary.h": ctypedef Eina_Bool (*Elm_Multibuttonentry_Item_Filter_Cb)(Evas_Object *obj, const_char *item_label, void *item_data, void *data) + ctypedef char * (*Elm_Multibuttonentry_Format_Cb)(int count, void *data) Evas_Object *elm_multibuttonentry_add(Evas_Object *parent) Evas_Object *elm_multibuttonentry_entry_get(const_Evas_Object *obj) @@ -28,3 +29,4 @@ cdef extern from "Elementary.h": # TODO: void elm_multibuttonentry_item_filter_remove(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_Cb func, void *data) void elm_multibuttonentry_editable_set(Evas_Object *obj, Eina_Bool editable) Eina_Bool elm_multibuttonentry_editable_get(const_Evas_Object *obj) + void elm_multibuttonentry_format_function_set(Evas_Object *obj, Elm_Multibuttonentry_Format_Cb f_func, const_void *data) diff --git a/efl/elementary/multibuttonentry.pyx b/efl/elementary/multibuttonentry.pyx index 231c8fe..6e37d71 100644 --- a/efl/elementary/multibuttonentry.pyx +++ b/efl/elementary/multibuttonentry.pyx @@ -92,6 +92,18 @@ cdef Eina_Bool _multibuttonentry_filter_callback(Evas_Object *obj, \ return ret +cdef char * _multibuttonentry_format_cb(int count, void *data) with gil: + (callback, a, ka) = <object>data + + try: + ret = callback(count, *a, **ka) + except: + traceback.print_exc() + + # TODO leak here + return strdup(ret) + + cdef class MultiButtonEntryItem(ObjectItem): """An item for the MultiButtonEntry widget.""" @@ -432,6 +444,32 @@ cdef class MultiButtonEntry(Object): def editable_get(self): return bool(elm_multibuttonentry_editable_get(self.obj)) + def format_function_set(self, func, *args, **kwargs): + """format_function_set(func, *args, **kwargs) + + Set a function to format the string that will be used to display + the hidden items counter. + + :param func: The actual format function. + signature: (int count, args, kwargs)->string + :type func: callable + + .. note:: Setting ``func`` to `None` will restore the default format. + + .. versionadded:: 1.9 + + """ + if func is None: + elm_multibuttonentry_format_function_set(self.obj, NULL, NULL) + return + + cbdata = (func, args, kwargs) + elm_multibuttonentry_format_function_set(self.obj, + _multibuttonentry_format_cb, + <void *>cbdata) + # TODO leak here + Py_INCREF(cbdata) + def callback_item_selected_add(self, func, *args, **kwargs): self._callback_add("item,selected", func, *args, **kwargs) diff --git a/examples/elementary/test_multibuttonentry.py b/examples/elementary/test_multibuttonentry.py index ede1fdb..e401461 100644 --- a/examples/elementary/test_multibuttonentry.py +++ b/examples/elementary/test_multibuttonentry.py @@ -61,6 +61,10 @@ def cb_print(btn, mbe): for i in mbe.items: print(i.text) +def custom_format_func(count): + return "+ {} rabbits".format(count) + + def multibuttonentry_clicked(obj, item=None): win = StandardWindow("multibuttonentry", "MultiButtonEntry test", autodel=True, size=(320, 320)) @@ -148,6 +152,23 @@ def multibuttonentry_clicked(obj, item=None): hbox.pack_end(bt) bt.show() + + hbox = Box(win, horizontal=True, size_hint_weight=EXPAND_HORIZ) + vbox.pack_end(hbox) + hbox.show() + + bt = Button(win, text="Change format function", + size_hint_align=FILL_HORIZ, size_hint_weight=EXPAND_HORIZ) + bt.callback_clicked_add(lambda b: mbe.format_function_set(custom_format_func)) + hbox.pack_end(bt) + bt.show() + + bt = Button(win, text="Unset format function", + size_hint_align=FILL_HORIZ, size_hint_weight=EXPAND_HORIZ) + bt.callback_clicked_add(lambda b: mbe.format_function_set(None)) + hbox.pack_end(bt) + bt.show() + mbe.focus = True win.show() --
