hippo-canvas r7296 - trunk/python



Author: otaylor
Date: Fri Oct 24 16:04:59 2008
New Revision: 7296
URL: http://svn.gnome.org/viewvc/hippo-canvas?rev=7296&view=rev

Log:
Wrap hippo_canvas_box_sort()

Rename marshal_canvas_box_insert_sorted to marshal_compare_child_func()
so that we can use it box_sort() as well. Protect against pygobject_new()
returning NULL for an error. Handle long=>int conversion for the return
value more carefully.


Modified:
   trunk/python/hippo.override

Modified: trunk/python/hippo.override
==============================================================================
--- trunk/python/hippo.override	(original)
+++ trunk/python/hippo.override	Fri Oct 24 16:04:59 2008
@@ -28,6 +28,53 @@
 
 extern Pycairo_CAPI_t *Pycairo_CAPI;
 
+static int
+marshal_compare_child_func(HippoCanvasItem *a, HippoCanvasItem *b, void *data)
+{
+    PyObject *py_a, *py_b;
+    PyObject *compare = (PyObject*) data;
+    PyObject *retobj;
+    PyGILState_STATE state;
+    long retval = 0;
+
+    state = pyg_gil_state_ensure();
+
+    py_a = pygobject_new(G_OBJECT(a));
+    if (py_a == NULL) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        pyg_gil_state_release(state);
+        return 0;
+    } 
+
+    py_b = pygobject_new(G_OBJECT(b));
+    if (py_b == NULL) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        Py_DECREF(py_a);
+        pyg_gil_state_release(state);
+        return 0;
+    } 
+    
+    retobj = PyEval_CallFunction(compare, "(OO)", py_a, py_b);
+
+    Py_DECREF(py_a);
+    Py_DECREF(py_b);
+
+    if (retobj == NULL) {
+        PyErr_Print();
+    } else {
+        retval = PyInt_AsLong(retobj);
+        if (retval == -1 && PyErr_Occurred())
+            PyErr_Print();    
+    }
+
+    Py_XDECREF(retobj);
+
+    pyg_gil_state_release(state);
+    
+    return retval < 0 ? -1 : (retval == 0 ? 0 : 1);
+}
 %%
 modulename hippo
 %%
@@ -182,38 +229,6 @@
 }
 %%
 override hippo_canvas_box_insert_sorted kwargs
-static int
-marshal_canvas_box_insert_sorted(HippoCanvasItem *a, HippoCanvasItem *b, void *data)
-{
-    PyObject *py_a, *py_b;
-	PyObject *compare = (PyObject*) data;
-	PyObject *retobj;
-	PyGILState_STATE state;	
-	int retval = 0;
-	
-    state = pyg_gil_state_ensure();
-	py_a = pygobject_new(G_OBJECT(a));    
-	py_b = pygobject_new(G_OBJECT(b));
-    
-    retobj = PyEval_CallFunction(compare, "(OO)", py_a, py_b);
-
-	Py_DECREF(py_a);
-	Py_DECREF(py_b);
-
-    if (retobj == NULL) {
-        PyErr_Print();
-    } else {
-    	retval = PyInt_AsLong(retobj);
-        if (retval == -1 && PyErr_Occurred())
-            PyErr_Print();    
-    }
-	
-    Py_XDECREF(retobj);
-
-    pyg_gil_state_release(state);
-    
-    return retval;
-}
 static PyObject *
 _wrap_hippo_canvas_box_insert_sorted(PyGObject *self, PyObject *args, PyObject *kwargs)
 {
@@ -235,12 +250,34 @@
 	hippo_canvas_box_insert_sorted(HIPPO_CANVAS_BOX(self->obj), 
 								   HIPPO_CANVAS_ITEM(item->obj),
 								   flags,
-								   marshal_canvas_box_insert_sorted,
+								   marshal_compare_child_func,
 								   compare);
 	Py_INCREF(Py_None);
 	return Py_None;
 }
 %%
+override hippo_canvas_box_sort kwargs
+static PyObject *
+_wrap_hippo_canvas_box_sort(PyGObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "compare_func", NULL };
+    PyObject *compare;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,"O:HippoCanvasBox.sort", kwlist,
+				     &compare))
+        return NULL;
+    if (!PyCallable_Check(compare)) {
+        PyErr_SetString(PyExc_TypeError, "parameter must be callable");
+        return NULL;
+    }
+
+    hippo_canvas_box_sort(HIPPO_CANVAS_BOX(self->obj), 
+			  marshal_compare_child_func,
+			  compare);
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+%%
 override hippo_canvas_box_align kwargs
 static PyObject *
 _wrap_hippo_canvas_box_align(PyGObject *self, PyObject *args, PyObject *kwargs)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]