Remove pygi.h and pygi-private.h
[pygobject.git] / gi / pygboxed.c
blob814cdb957abbaefdb4a8c9ba9b1f2328ea3389e2
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 <pyglib.h>
26 #include "pygobject-private.h"
27 #include "pygboxed.h"
29 #include "pygi-type.h"
31 GQuark pygboxed_type_key;
32 GQuark pygboxed_marshal_key;
34 PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);
36 static void
37 pyg_boxed_dealloc(PyGBoxed *self)
39 if (self->free_on_dealloc && pyg_boxed_get_ptr (self)) {
40 PyGILState_STATE state = pyglib_gil_state_ensure();
41 g_boxed_free (self->gtype, pyg_boxed_get_ptr (self));
42 pyglib_gil_state_release(state);
45 Py_TYPE(self)->tp_free((PyObject *)self);
48 static PyObject*
49 pyg_boxed_richcompare(PyObject *self, PyObject *other, int op)
51 if (Py_TYPE(self) == Py_TYPE(other) &&
52 PyObject_IsInstance(self, (PyObject*)&PyGBoxed_Type))
53 return _pyglib_generic_ptr_richcompare (pyg_boxed_get_ptr (self),
54 pyg_boxed_get_ptr (other),
55 op);
56 else {
57 Py_INCREF(Py_NotImplemented);
58 return Py_NotImplemented;
63 static long
64 pyg_boxed_hash(PyGBoxed *self)
66 return (long)pyg_boxed_get_ptr (self);
69 static PyObject *
70 pyg_boxed_repr(PyGBoxed *boxed)
72 PyObject *module, *repr, *self = (PyObject *)boxed;
73 gchar *module_str, *namespace;
75 module = PyObject_GetAttrString (self, "__module__");
76 if (module == NULL)
77 return NULL;
79 if (!PYGLIB_PyUnicode_Check (module)) {
80 Py_DECREF (module);
81 return NULL;
84 module_str = PYGLIB_PyUnicode_AsString (module);
85 namespace = g_strrstr (module_str, ".");
86 if (namespace == NULL) {
87 namespace = module_str;
88 } else {
89 namespace += 1;
92 repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
93 namespace, Py_TYPE (self)->tp_name,
94 self, g_type_name (boxed->gtype),
95 pyg_boxed_get_ptr (boxed));
96 Py_DECREF (module);
97 return repr;
100 static int
101 pyg_boxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
103 gchar buf[512];
105 if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
106 return -1;
108 pyg_boxed_set_ptr (self, NULL);
109 self->gtype = 0;
110 self->free_on_dealloc = FALSE;
112 g_snprintf(buf, sizeof(buf), "%s can not be constructed",
113 Py_TYPE(self)->tp_name);
114 PyErr_SetString(PyExc_NotImplementedError, buf);
115 return -1;
118 static void
119 pyg_boxed_free(PyObject *op)
121 PyObject_FREE(op);
124 static PyObject *
125 pyg_boxed_copy(PyGBoxed *self)
127 return pyg_boxed_new (self->gtype, pyg_boxed_get_ptr (self), TRUE, TRUE);
132 static PyMethodDef pygboxed_methods[] = {
133 { "copy", (PyCFunction) pyg_boxed_copy, METH_NOARGS },
134 { NULL, NULL, 0 }
139 * pyg_register_boxed:
140 * @dict: the module dictionary to store the wrapper class.
141 * @class_name: the Python name for the wrapper class.
142 * @boxed_type: the GType of the boxed type being wrapped.
143 * @type: the wrapper class.
145 * Registers a wrapper for a boxed type. The wrapper class will be a
146 * subclass of gobject.GBoxed, and a reference to the wrapper class
147 * will be stored in the provided module dictionary.
149 void
150 pyg_register_boxed(PyObject *dict, const gchar *class_name,
151 GType boxed_type, PyTypeObject *type)
153 PyObject *o;
155 g_return_if_fail(dict != NULL);
156 g_return_if_fail(class_name != NULL);
157 g_return_if_fail(boxed_type != 0);
159 if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_boxed_dealloc;
161 Py_TYPE(type) = &PyType_Type;
162 type->tp_base = &PyGBoxed_Type;
164 if (PyType_Ready(type) < 0) {
165 g_warning("could not get type `%s' ready", type->tp_name);
166 return;
169 PyDict_SetItemString(type->tp_dict, "__gtype__",
170 o=pyg_type_wrapper_new(boxed_type));
171 Py_DECREF(o);
173 g_type_set_qdata(boxed_type, pygboxed_type_key, type);
175 PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
179 * pyg_boxed_new:
180 * @boxed_type: the GType of the boxed value.
181 * @boxed: the boxed value.
182 * @copy_boxed: whether the new boxed wrapper should hold a copy of the value.
183 * @own_ref: whether the boxed wrapper should own the boxed value.
185 * Creates a wrapper for a boxed value. If @copy_boxed is set to
186 * True, the wrapper will hold a copy of the value, instead of the
187 * value itself. If @own_ref is True, then the value held by the
188 * wrapper will be freed when the wrapper is deallocated. If
189 * @copy_boxed is True, then @own_ref must also be True.
191 * Returns: the boxed wrapper or %NULL and sets an exception.
193 PyObject *
194 pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
195 gboolean own_ref)
197 PyGILState_STATE state;
198 PyGBoxed *self;
199 PyTypeObject *tp;
201 g_return_val_if_fail(boxed_type != 0, NULL);
202 g_return_val_if_fail(!copy_boxed || (copy_boxed && own_ref), NULL);
204 state = pyglib_gil_state_ensure();
206 if (!boxed) {
207 Py_INCREF(Py_None);
208 pyglib_gil_state_release(state);
209 return Py_None;
212 tp = g_type_get_qdata(boxed_type, pygboxed_type_key);
214 if (!tp)
215 tp = (PyTypeObject *)pygi_type_import_by_g_type(boxed_type);
217 if (!tp)
218 tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */
220 if (!PyType_IsSubtype (tp, &PyGBoxed_Type)) {
221 PyErr_Format (PyExc_RuntimeError, "%s isn't a GBoxed", tp->tp_name);
222 pyglib_gil_state_release (state);
223 return NULL;
226 self = (PyGBoxed *)tp->tp_alloc(tp, 0);
228 if (self == NULL) {
229 pyglib_gil_state_release(state);
230 return NULL;
233 if (copy_boxed)
234 boxed = g_boxed_copy(boxed_type, boxed);
235 pyg_boxed_set_ptr (self, boxed);
236 self->gtype = boxed_type;
237 self->free_on_dealloc = own_ref;
239 pyglib_gil_state_release(state);
241 return (PyObject *)self;
244 void
245 pygobject_boxed_register_types(PyObject *d)
247 pygboxed_type_key = g_quark_from_static_string("PyGBoxed::class");
248 pygboxed_marshal_key = g_quark_from_static_string("PyGBoxed::marshal");
250 PyGBoxed_Type.tp_dealloc = (destructor)pyg_boxed_dealloc;
251 PyGBoxed_Type.tp_richcompare = pyg_boxed_richcompare;
252 PyGBoxed_Type.tp_repr = (reprfunc)pyg_boxed_repr;
253 PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
254 PyGBoxed_Type.tp_methods = pygboxed_methods;
255 PyGBoxed_Type.tp_init = (initproc)pyg_boxed_init;
256 PyGBoxed_Type.tp_free = (freefunc)pyg_boxed_free;
257 PyGBoxed_Type.tp_hash = (hashfunc)pyg_boxed_hash;
259 PYGOBJECT_REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);