release
[pygobject.git] / gi / pyglib.c
blobd21ba4d09a8e239849b97c31b9e1efe73b17b8e2
1 /* -*- Mode: C; c-set-style: python; c-basic-offset: 4 -*-
2 * pyglib - Python bindings for GLib toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
4 * 2004-2008 Johan Dahlin
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <Python.h>
25 #include <pythread.h>
26 #include "pyglib.h"
27 #include "pygoptioncontext.h"
28 #include "pygoptiongroup.h"
31 /**
32 * pyg_option_group_transfer_group:
33 * @group: a GOptionGroup wrapper
35 * This is used to transfer the GOptionGroup to a GOptionContext. After this
36 * is called, the calle must handle the release of the GOptionGroup.
38 * When #NULL is returned, the GOptionGroup was already transfered.
40 * Returns: Either #NULL or the wrapped GOptionGroup.
42 GOptionGroup *
43 pyglib_option_group_transfer_group(PyObject *obj)
45 PyGOptionGroup *self = (PyGOptionGroup*)obj;
47 if (self->is_in_context)
48 return NULL;
50 self->is_in_context = TRUE;
52 /* Here we increase the reference count of the PyGOptionGroup, because now
53 * the GOptionContext holds an reference to us (it is the userdata passed
54 * to g_option_group_new().
56 * The GOptionGroup is freed with the GOptionContext.
58 * We set it here because if we would do this in the init method we would
59 * hold two references and the PyGOptionGroup would never be freed.
61 Py_INCREF(self);
63 return self->group;
67 /****** Private *****/
69 /**
70 * _pyglib_destroy_notify:
71 * @user_data: a PyObject pointer.
73 * A function that can be used as a GDestroyNotify callback that will
74 * call Py_DECREF on the data.
76 void
77 _pyglib_destroy_notify(gpointer user_data)
79 PyObject *obj = (PyObject *)user_data;
80 PyGILState_STATE state;
82 state = PyGILState_Ensure();
83 Py_DECREF(obj);
84 PyGILState_Release(state);
87 gboolean
88 _pyglib_handler_marshal(gpointer user_data)
90 PyObject *tuple, *ret;
91 gboolean res;
92 PyGILState_STATE state;
94 g_return_val_if_fail(user_data != NULL, FALSE);
96 state = PyGILState_Ensure();
98 tuple = (PyObject *)user_data;
99 ret = PyObject_CallObject(PyTuple_GetItem(tuple, 0),
100 PyTuple_GetItem(tuple, 1));
101 if (!ret) {
102 PyErr_Print();
103 res = FALSE;
104 } else {
105 res = PyObject_IsTrue(ret);
106 Py_DECREF(ret);
109 PyGILState_Release(state);
111 return res;
114 PyObject*
115 _pyglib_generic_ptr_richcompare(void* a, void *b, int op)
117 PyObject *res;
119 switch (op) {
121 case Py_EQ:
122 res = (a == b) ? Py_True : Py_False;
123 break;
125 case Py_NE:
126 res = (a != b) ? Py_True : Py_False;
127 break;
129 case Py_LT:
130 res = (a < b) ? Py_True : Py_False;
131 break;
133 case Py_LE:
134 res = (a <= b) ? Py_True : Py_False;
135 break;
137 case Py_GT:
138 res = (a > b) ? Py_True : Py_False;
139 break;
141 case Py_GE:
142 res = (a >= b) ? Py_True : Py_False;
143 break;
145 default:
146 res = Py_NotImplemented;
147 break;
150 Py_INCREF(res);
151 return res;
154 PyObject*
155 _pyglib_generic_long_richcompare(long a, long b, int op)
157 PyObject *res;
159 switch (op) {
161 case Py_EQ:
162 res = (a == b) ? Py_True : Py_False;
163 Py_INCREF(res);
164 break;
166 case Py_NE:
167 res = (a != b) ? Py_True : Py_False;
168 Py_INCREF(res);
169 break;
172 case Py_LT:
173 res = (a < b) ? Py_True : Py_False;
174 Py_INCREF(res);
175 break;
177 case Py_LE:
178 res = (a <= b) ? Py_True : Py_False;
179 Py_INCREF(res);
180 break;
182 case Py_GT:
183 res = (a > b) ? Py_True : Py_False;
184 Py_INCREF(res);
185 break;
187 case Py_GE:
188 res = (a >= b) ? Py_True : Py_False;
189 Py_INCREF(res);
190 break;
192 default:
193 res = Py_NotImplemented;
194 Py_INCREF(res);
195 break;
198 return res;