tests: don't test for specific device labels
[pygobject.git] / gi / pygboxed.c
blob1f355be308eb9828870658d6fc055e9fa88932bd
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
5 * pygboxed.c: wrapper for GBoxed
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include <Python.h>
24 #include <glib-object.h>
26 #include "pygboxed.h"
27 #include "pygi-type.h"
28 #include "pygi-type.h"
29 #include "pygi-util.h"
31 GQuark pygboxed_type_key;
33 PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);
35 static void
36 gboxed_dealloc(PyGBoxed *self)
38 if (self->free_on_dealloc && pyg_boxed_get_ptr (self)) {
39 PyGILState_STATE state = PyGILState_Ensure();
40 g_boxed_free (self->gtype, pyg_boxed_get_ptr (self));
41 PyGILState_Release(state);
44 Py_TYPE(self)->tp_free((PyObject *)self);
47 static PyObject*
48 gboxed_richcompare(PyObject *self, PyObject *other, int op)
50 if (Py_TYPE(self) == Py_TYPE(other) &&
51 PyObject_IsInstance(self, (PyObject*)&PyGBoxed_Type))
52 return pyg_ptr_richcompare (pyg_boxed_get_ptr (self),
53 pyg_boxed_get_ptr (other),
54 op);
55 else {
56 Py_INCREF(Py_NotImplemented);
57 return Py_NotImplemented;
61 static PYGLIB_Py_hash_t
62 gboxed_hash(PyGBoxed *self)
64 return PYGLIB_Py_hash_t_FromVoidPtr (pyg_boxed_get_ptr (self));
67 static PyObject *
68 gboxed_repr(PyGBoxed *boxed)
70 PyObject *module, *repr, *self = (PyObject *)boxed;
71 gchar *module_str, *namespace;
73 module = PyObject_GetAttrString (self, "__module__");
74 if (module == NULL)
75 return NULL;
77 if (!PYGLIB_PyUnicode_Check (module)) {
78 Py_DECREF (module);
79 return NULL;
82 module_str = PYGLIB_PyUnicode_AsString (module);
83 namespace = g_strrstr (module_str, ".");
84 if (namespace == NULL) {
85 namespace = module_str;
86 } else {
87 namespace += 1;
90 repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
91 namespace, Py_TYPE (self)->tp_name,
92 self, g_type_name (boxed->gtype),
93 pyg_boxed_get_ptr (boxed));
94 Py_DECREF (module);
95 return repr;
98 static int
99 gboxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
101 gchar buf[512];
103 if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
104 return -1;
106 pyg_boxed_set_ptr (self, NULL);
107 self->gtype = 0;
108 self->free_on_dealloc = FALSE;
110 g_snprintf(buf, sizeof(buf), "%s can not be constructed",
111 Py_TYPE(self)->tp_name);
112 PyErr_SetString(PyExc_NotImplementedError, buf);
113 return -1;
116 static void
117 gboxed_free(PyObject *op)
119 PyObject_FREE(op);
122 static PyObject *
123 gboxed_copy(PyGBoxed *self)
125 return pygi_gboxed_new (self->gtype, pyg_boxed_get_ptr (self), TRUE, TRUE);
128 static PyMethodDef pygboxed_methods[] = {
129 { "copy", (PyCFunction) gboxed_copy, METH_NOARGS },
130 { NULL, NULL, 0 }
135 * pygi_register_gboxed:
136 * @dict: the module dictionary to store the wrapper class.
137 * @class_name: the Python name for the wrapper class.
138 * @boxed_type: the GType of the boxed type being wrapped.
139 * @type: the wrapper class.
141 * Registers a wrapper for a boxed type. The wrapper class will be a
142 * subclass of gobject.GBoxed, and a reference to the wrapper class
143 * will be stored in the provided module dictionary.
145 void
146 pygi_register_gboxed (PyObject *dict, const gchar *class_name,
147 GType boxed_type, PyTypeObject *type)
149 PyObject *o;
151 g_return_if_fail(dict != NULL);
152 g_return_if_fail(class_name != NULL);
153 g_return_if_fail(boxed_type != 0);
155 if (!type->tp_dealloc) type->tp_dealloc = (destructor)gboxed_dealloc;
157 Py_TYPE(type) = &PyType_Type;
158 g_assert (Py_TYPE (&PyGBoxed_Type) != NULL);
159 type->tp_base = &PyGBoxed_Type;
161 if (PyType_Ready(type) < 0) {
162 g_warning("could not get type `%s' ready", type->tp_name);
163 return;
166 PyDict_SetItemString(type->tp_dict, "__gtype__",
167 o=pyg_type_wrapper_new(boxed_type));
168 Py_DECREF(o);
170 g_type_set_qdata(boxed_type, pygboxed_type_key, type);
172 PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
176 * pygi_gboxed_new:
177 * @boxed_type: the GType of the boxed value.
178 * @boxed: the boxed value.
179 * @copy_boxed: whether the new boxed wrapper should hold a copy of the value.
180 * @own_ref: whether the boxed wrapper should own the boxed value.
182 * Creates a wrapper for a boxed value. If @copy_boxed is set to
183 * True, the wrapper will hold a copy of the value, instead of the
184 * value itself. If @own_ref is True, then the value held by the
185 * wrapper will be freed when the wrapper is deallocated. If
186 * @copy_boxed is True, then @own_ref must also be True.
188 * Returns: the boxed wrapper or %NULL and sets an exception.
190 PyObject *
191 pygi_gboxed_new (GType boxed_type, gpointer boxed, gboolean copy_boxed,
192 gboolean own_ref)
194 PyGILState_STATE state;
195 PyGBoxed *self;
196 PyTypeObject *tp;
198 g_return_val_if_fail(boxed_type != 0, NULL);
199 g_return_val_if_fail(!copy_boxed || (copy_boxed && own_ref), NULL);
201 state = PyGILState_Ensure();
203 if (!boxed) {
204 Py_INCREF(Py_None);
205 PyGILState_Release(state);
206 return Py_None;
209 tp = g_type_get_qdata(boxed_type, pygboxed_type_key);
211 if (!tp)
212 tp = (PyTypeObject *)pygi_type_import_by_g_type(boxed_type);
214 if (!tp)
215 tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */
217 if (!PyType_IsSubtype (tp, &PyGBoxed_Type)) {
218 PyErr_Format (PyExc_RuntimeError, "%s isn't a GBoxed", tp->tp_name);
219 PyGILState_Release (state);
220 return NULL;
223 self = (PyGBoxed *)tp->tp_alloc(tp, 0);
225 if (self == NULL) {
226 PyGILState_Release(state);
227 return NULL;
230 if (copy_boxed)
231 boxed = g_boxed_copy(boxed_type, boxed);
232 pyg_boxed_set_ptr (self, boxed);
233 self->gtype = boxed_type;
234 self->free_on_dealloc = own_ref;
236 PyGILState_Release(state);
238 return (PyObject *)self;
242 * Returns 0 on success, or -1 and sets an exception.
245 pygi_gboxed_register_types(PyObject *d)
247 pygboxed_type_key = g_quark_from_static_string("PyGBoxed::class");
249 PyGBoxed_Type.tp_dealloc = (destructor)gboxed_dealloc;
250 PyGBoxed_Type.tp_richcompare = gboxed_richcompare;
251 PyGBoxed_Type.tp_repr = (reprfunc)gboxed_repr;
252 PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
253 PyGBoxed_Type.tp_methods = pygboxed_methods;
254 PyGBoxed_Type.tp_init = (initproc)gboxed_init;
255 PyGBoxed_Type.tp_free = (freefunc)gboxed_free;
256 PyGBoxed_Type.tp_hash = (hashfunc)gboxed_hash;
258 PYGOBJECT_REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);
260 return 0;