GNOME Bugzilla – Bug 584259
unable to call metacity gradient_create_multi function in Python
Last modified: 2014-08-02 13:02:33 UTC
Please describe the problem: The function needs an array of GdkColors as an argument, like the C counterpart. But I can't send any array in Python. So I tried sending a list of gtk.gtk.Colors, but the result is a bit strange: the color in the gradient are not the one sent to the function. For reference, the function is defined like this in gnome-python-desktop/metacity/metacity.defs : > (define-function gradient_create_multi > (c-name "meta_gradient_create_multi") > (return-type "GdkPixbuf*") > (parameters > '("int" "width") > '("int" "height") > '("const-GdkColor*" "colors") > '("int" "n_colors") > '("MetaGradientType" "style") > ) > ) And here is the C function definition > GdkPixbuf* > meta_gradient_create_multi (int width, > int height, > const GdkColor *colors, > int n_colors, > MetaGradientType style) Which then calls meta_gradient_create_multi_vertical (c part) > static GdkPixbuf* > meta_gradient_create_multi_vertical (int width, int height, > GdkColor *colors, > int count) Here is an example of the call to the meta_gradient_create_multi in C, from metacity/src/testgradient.c (simplified) > GdkColor colors[5]; > pixbuf = meta_gradient_create_multi (width, height, colors, 5, type); Steps to reproduce: 1. create a list of gtk.gdk.Colors objects 2. send the list to the metacity.gradient_create_multi function Actual results: the pixbuf returned by the function will have strange colors (not the one passed in the list) Expected results: The gradient created should always be based on the colors passed by the list. Does this happen every time? When calling the function with a list in which you add/remove objects, the result will differ greatly from one call to another. Other information: I looked at another gtk function which takes an array in C, and the Python bindings has been overrided to use a list. The function is gtk.targets_include_image, which then transform the list to an array in c. But that is not the case here. Maybe some code should be added in the gnome-python-desktop/metacity/metacitymodule.c or something related. I don't know much about Python binding C functions, so I'm kind of unaware of what needs to be done. If anyone knows how it's handled in Python in other functions, when the C counterpart works with an array of G objects as an argument, that may help.
Created attachment 135601 [details] Here is an example, drawing a pixbuf returned by the gradient_create_multi function on a drawingarea The important part is in the on_drawing_expose_event: > metacity.gradient_create_multi(100, 100, colors[0], len(colors), metacity.GRADIENT_VERTICAL) And the colors variable is a list of gtk.gdk.Colors objects defined as > colors = [ > colormap.alloc_color( "yellow" ), > colormap.alloc_color( "green" ), > colormap.alloc_color( "white" ) > ]
Look at the generated C code if (pyg_boxed_check(py_colors, GDK_TYPE_COLOR)) colors = pyg_boxed_get(py_colors, GdkColor); else { PyErr_SetString(PyExc_TypeError, "colors should be a GdkColor"); return NULL; } ret = meta_gradient_create_multi(width, height, colors, n_colors, style); which is wrong, it doesn't expect a list of colors but a single GdkColor, this function should be wrapped manually.
(In reply to comment #2) > which is wrong, it doesn't expect a list of colors but a single GdkColor, this > function should be wrapped manually. > That's my point. It would be better if we could send a list in Python. But I know this needs a lot of work, and I don't have the knowledge to do that. I already looked at some other functions in C, like I said, that waits for a list in Python, and send an array in C. And I don't understand much of that code. But I suppose it's always the same technique. So if someone has the ability, the experience, and the time to do that, he will be able to copy/paste the important parts, and adapt the code, which could take extra work, but it would be worth it, really. Anyway, if you have a link to some Python code calling any C function waiting for an array, or even an brief theorical explanation on how to do this (like which module are needed), it would be greatly appreciated, because I didn't found anything relevant on the subject yet.
Created attachment 135675 [details] An example of what it could be This is what I meant by copy/pasticodes codes ng. I just did tried to retrieve some bits of from the pygtk code, and adapting it for this function. I don't know if it would be working, or even compiling. And anyway I don't how to test this code. So I'm just putting it in here for reference.
I've done some work to make it easier to handle lists in the override files by factoring out the common list handling code but I've been stymied in checking in code because of the git changeover. Once I get up to speed using git then I can add some helper functions for dealing with GSList and GList items.
The last gnome-python-desktop code changes took place in January 2011: https://git.gnome.org/browse/archive/gnome-python-desktop/log/ This project is not under active development anymore. This project got recently archived in GNOME Git. It is currently unlikely that there will be any further active development. Closing this report as WONTFIX as part of Bugzilla Housekeeping to reflect reality. Please feel free to reopen this bug report in the future if anyone takes the responsibility for active development again. If you are interested in maintainership, inform https://mail.gnome.org/mailman/listinfo/desktop-devel-list gnome-python-desktop wrapped application API in GNOME 2. For Python in GNOME 3, see https://wiki.gnome.org/Projects/PyGObject and its gobject-introspection approach.