release
[pygobject.git] / gi / pygboxed.c
blobe521c969843bc1ce140af746841d857a071599b3
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 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <Python.h>
26 #include <glib-object.h>
28 #include <pyglib.h>
29 #include "pygboxed.h"
30 #include "pygtype.h"
32 #include "pygi-type.h"
34 GQuark pygboxed_type_key;
35 GQuark pygboxed_marshal_key;
37 PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);
39 static void
40 pyg_boxed_dealloc(PyGBoxed *self)
42 if (self->free_on_dealloc && pyg_boxed_get_ptr (self)) {
43 PyGILState_STATE state = PyGILState_Ensure();
44 g_boxed_free (self->gtype, pyg_boxed_get_ptr (self));
45 PyGILState_Release(state);
48 Py_TYPE(self)->tp_free((PyObject *)self);
51 static PyObject*
52 pyg_boxed_richcompare(PyObject *self, PyObject *other, int op)
54 if (Py_TYPE(self) == Py_TYPE(other) &&
55 PyObject_IsInstance(self, (PyObject*)&PyGBoxed_Type))
56 return _pyglib_generic_ptr_richcompare (pyg_boxed_get_ptr (self),
57 pyg_boxed_get_ptr (other),
58 op);
59 else {
60 Py_INCREF(Py_NotImplemented);
61 return Py_NotImplemented;
66 static PYGLIB_Py_hash_t
67 pyg_boxed_hash(PyGBoxed *self)
69 return PYGLIB_Py_hash_t_FromVoidPtr (pyg_boxed_get_ptr (self));
72 static PyObject *
73 pyg_boxed_repr(PyGBoxed *boxed)
75 PyObject *module, *repr, *self = (PyObject *)boxed;
76 gchar *module_str, *namespace;
78 module = PyObject_GetAttrString (self, "__module__");
79 if (module == NULL)
80 return NULL;
82 if (!PYGLIB_PyUnicode_Check (module)) {
83 Py_DECREF (module);
84 return NULL;
87 module_str = PYGLIB_PyUnicode_AsString (module);
88 namespace = g_strrstr (module_str, ".");
89 if (namespace == NULL) {
90 namespace = module_str;
91 } else {
92 namespace += 1;
95 repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
96 namespace, Py_TYPE (self)->tp_name,
97 self, g_type_name (boxed->gtype),
98 pyg_boxed_get_ptr (boxed));
99 Py_DECREF (module);
100 return repr;
103 static int
104 pyg_boxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
106 gchar buf[512];
108 if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
109 return -1;
111 pyg_boxed_set_ptr (self, NULL);
112 self->gtype = 0;
113 self->free_on_dealloc = FALSE;
115 g_snprintf(buf, sizeof(buf), "%s can not be constructed",
116 Py_TYPE(self)->tp_name);
117 PyErr_SetString(PyExc_NotImplementedError, buf);
118 return -1;
121 static void
122 pyg_boxed_free(PyObject *op)
124 PyObject_FREE(op);
127 static PyObject *
128 pyg_boxed_copy(PyGBoxed *self)
130 return pyg_boxed_new (self->gtype, pyg_boxed_get_ptr (self), TRUE, TRUE);
135 static PyMethodDef pygboxed_methods[] = {
136 { "copy", (PyCFunction) pyg_boxed_copy, METH_NOARGS },
137 { NULL, NULL, 0 }
142 * pyg_register_boxed:
143 * @dict: the module dictionary to store the wrapper class.
144 * @class_name: the Python name for the wrapper class.
145 * @boxed_type: the GType of the boxed type being wrapped.
146 * @type: the wrapper class.
148 * Registers a wrapper for a boxed type. The wrapper class will be a
149 * subclass of gobject.GBoxed, and a reference to the wrapper class
150 * will be stored in the provided module dictionary.
152 void
153 pyg_register_boxed(PyObject *dict, const gchar *class_name,
154 GType boxed_type, PyTypeObject *type)
156 PyObject *o;
158 g_return_if_fail(dict != NULL);
159 g_return_if_fail(class_name != NULL);
160 g_return_if_fail(boxed_type != 0);
162 if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_boxed_dealloc;
164 Py_TYPE(type) = &PyType_Type;
165 type->tp_base = &PyGBoxed_Type;
167 if (PyType_Ready(type) < 0) {
168 g_warning("could not get type `%s' ready", type->tp_name);
169 return;
172 PyDict_SetItemString(type->tp_dict, "__gtype__",
173 o=pyg_type_wrapper_new(boxed_type));
174 Py_DECREF(o);
176 g_type_set_qdata(boxed_type, pygboxed_type_key, type);
178 PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
182 * pyg_boxed_new:
183 * @boxed_type: the GType of the boxed value.
184 * @boxed: the boxed value.
185 * @copy_boxed: whether the new boxed wrapper should hold a copy of the value.
186 * @own_ref: whether the boxed wrapper should own the boxed value.
188 * Creates a wrapper for a boxed value. If @copy_boxed is set to
189 * True, the wrapper will hold a copy of the value, instead of the
190 * value itself. If @own_ref is True, then the value held by the
191 * wrapper will be freed when the wrapper is deallocated. If
192 * @copy_boxed is True, then @own_ref must also be True.
194 * Returns: the boxed wrapper or %NULL and sets an exception.
196 PyObject *
197 pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
198 gboolean own_ref)
200 PyGILState_STATE state;
201 PyGBoxed *self;
202 PyTypeObject *tp;
204 g_return_val_if_fail(boxed_type != 0, NULL);
205 g_return_val_if_fail(!copy_boxed || (copy_boxed && own_ref), NULL);
207 state = PyGILState_Ensure();
209 if (!boxed) {
210 Py_INCREF(Py_None);
211 PyGILState_Release(state);
212 return Py_None;
215 tp = g_type_get_qdata(boxed_type, pygboxed_type_key);
217 if (!tp)
218 tp = (PyTypeObject *)pygi_type_import_by_g_type(boxed_type);
220 if (!tp)
221 tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */
223 if (!PyType_IsSubtype (tp, &PyGBoxed_Type)) {
224 PyErr_Format (PyExc_RuntimeError, "%s isn't a GBoxed", tp->tp_name);
225 PyGILState_Release (state);
226 return NULL;
229 self = (PyGBoxed *)tp->tp_alloc(tp, 0);
231 if (self == NULL) {
232 PyGILState_Release(state);
233 return NULL;
236 if (copy_boxed)
237 boxed = g_boxed_copy(boxed_type, boxed);
238 pyg_boxed_set_ptr (self, boxed);
239 self->gtype = boxed_type;
240 self->free_on_dealloc = own_ref;
242 PyGILState_Release(state);
244 return (PyObject *)self;
247 void
248 pygobject_boxed_register_types(PyObject *d)
250 pygboxed_type_key = g_quark_from_static_string("PyGBoxed::class");
251 pygboxed_marshal_key = g_quark_from_static_string("PyGBoxed::marshal");
253 PyGBoxed_Type.tp_dealloc = (destructor)pyg_boxed_dealloc;
254 PyGBoxed_Type.tp_richcompare = pyg_boxed_richcompare;
255 PyGBoxed_Type.tp_repr = (reprfunc)pyg_boxed_repr;
256 PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
257 PyGBoxed_Type.tp_methods = pygboxed_methods;
258 PyGBoxed_Type.tp_init = (initproc)pyg_boxed_init;
259 PyGBoxed_Type.tp_free = (freefunc)pyg_boxed_free;
260 PyGBoxed_Type.tp_hash = (hashfunc)pyg_boxed_hash;
262 PYGOBJECT_REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);