Debian Codegraph @

gi/pygpointer.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
4 *
5 * pygpointer.c: wrapper for GPointer
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include "pygi-type.h"
22 #include "pygi-util.h"
23 #include "pygpointer.h"
24
25 GQuark pygpointer_class_key;
26
27 PYGI_DEFINE_TYPE ("gobject.GPointer", PyGPointer_Type, PyGPointer);
28
29 static void
30 pyg_pointer_dealloc (PyGPointer *self)
31 {
32 Py_TYPE (self)->tp_free ((PyObject *)self);
33 }
34
35 static PyObject *
36 pyg_pointer_richcompare (PyObject *self, PyObject *other, int op)
37 {
38 if (Py_TYPE (self) == Py_TYPE (other))
39 return pyg_ptr_richcompare (pyg_pointer_get_ptr (self),
40 pyg_pointer_get_ptr (other), op);
41 else {
42 Py_RETURN_NOTIMPLEMENTED;
43 }
44 }
45
46 static Py_hash_t
47 pyg_pointer_hash (PyGPointer *self)
48 {
49 return (Py_hash_t)(gintptr)(pyg_pointer_get_ptr (self));
50 }
51
52 static PyObject *
53 pyg_pointer_repr (PyGPointer *self)
54 {
55 gchar buf[128];
56
57 g_snprintf (buf, sizeof (buf), "<%s at 0x%" G_GUINTPTR_FORMAT ">",
58 g_type_name (self->gtype),
59 (guintptr)pyg_pointer_get_ptr (self));
60 return PyUnicode_FromString (buf);
61 }
62
63 static int
64 pyg_pointer_init (PyGPointer *self, PyObject *args, PyObject *kwargs)
65 {
66 gchar buf[512];
67
68 if (!PyArg_ParseTuple (args, ":GPointer.__init__")) return -1;
69
70 pyg_pointer_set_ptr (self, NULL);
71 self->gtype = 0;
72
73 g_snprintf (buf, sizeof (buf), "%s can not be constructed",
74 Py_TYPE (self)->tp_name);
75 PyErr_SetString (PyExc_NotImplementedError, buf);
76 return -1;
77 }
78
79 static void
80 pyg_pointer_free (PyObject *op)
81 {
82 PyObject_Free (op);
83 }
84
85 /**
86 * pyg_register_pointer:
87 * @dict: the module dictionary to store the wrapper class.
88 * @class_name: the Python name for the wrapper class.
89 * @pointer_type: the GType of the pointer type being wrapped.
90 * @type: the wrapper class.
91 *
92 * Registers a wrapper for a pointer type. The wrapper class will be
93 * a subclass of gobject.GPointer, and a reference to the wrapper
94 * class will be stored in the provided module dictionary.
95 */
96 void
97 pyg_register_pointer (PyObject *dict, const gchar *class_name,
98 GType pointer_type, PyTypeObject *type)
99 {
100 PyObject *o;
101
102 g_return_if_fail (dict != NULL);
103 g_return_if_fail (class_name != NULL);
104 g_return_if_fail (pointer_type != 0);
105
106 if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_pointer_dealloc;
107
108 Py_SET_TYPE (type, &PyType_Type);
109 g_assert (Py_TYPE (&PyGPointer_Type) != NULL);
110 type->tp_base = &PyGPointer_Type;
111
112 if (PyType_Ready (type) < 0) {
113 g_warning ("could not get type `%s' ready", type->tp_name);
114 return;
115 }
116
117 PyDict_SetItemString (type->tp_dict, "__gtype__",
118 o = pyg_type_wrapper_new (pointer_type));
119 Py_DECREF (o);
120
121 g_type_set_qdata (pointer_type, pygpointer_class_key, type);
122
123 PyDict_SetItemString (dict, (char *)class_name, (PyObject *)type);
124 }
125
126 /**
127 * pyg_pointer_new:
128 * @pointer_type: the GType of the pointer value.
129 * @pointer: the pointer value.
130 *
131 * Creates a wrapper for a pointer value. Since G_TYPE_POINTER types
132 * don't register any information about how to copy/free them, there
133 * is no guarantee that the pointer will remain valid, and there is
134 * nothing registered to release the pointer when the pointer goes out
135 * of scope. This is why we don't recommend people use these types.
136 *
137 * Returns: the boxed wrapper.
138 */
139 PyObject *
140 pyg_pointer_new (GType pointer_type, gpointer pointer)
141 {
142 PyGILState_STATE state;
143 PyGPointer *self;
144 PyTypeObject *tp;
145 g_return_val_if_fail (pointer_type != 0, NULL);
146
147 state = PyGILState_Ensure ();
148
149 if (!pointer) {
150 Py_INCREF (Py_None);
151 PyGILState_Release (state);
152 return Py_None;
153 }
154
155 tp = g_type_get_qdata (pointer_type, pygpointer_class_key);
156
157 if (!tp) tp = (PyTypeObject *)pygi_type_import_by_g_type (pointer_type);
158
159 if (!tp) tp = (PyTypeObject *)&PyGPointer_Type; /* fallback */
160 self = PyObject_New (PyGPointer, tp);
161
162 PyGILState_Release (state);
163
164 if (self == NULL) return NULL;
165
166 pyg_pointer_set_ptr (self, pointer);
167 self->gtype = pointer_type;
168
169 return (PyObject *)self;
170 }
171
172 /**
173 * Returns 0 on success, or -1 and sets an exception.
174 */
175 int
176 pygi_pointer_register_types (PyObject *d)
177 {
178 PyObject *pygtype;
179
180 pygpointer_class_key = g_quark_from_static_string ("PyGPointer::class");
181
182 PyGPointer_Type.tp_dealloc = (destructor)pyg_pointer_dealloc;
183 PyGPointer_Type.tp_richcompare = pyg_pointer_richcompare;
184 PyGPointer_Type.tp_repr = (reprfunc)pyg_pointer_repr;
185 PyGPointer_Type.tp_hash = (hashfunc)pyg_pointer_hash;
186 PyGPointer_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
187 PyGPointer_Type.tp_init = (initproc)pyg_pointer_init;
188 PyGPointer_Type.tp_free = (freefunc)pyg_pointer_free;
189 PyGPointer_Type.tp_alloc = PyType_GenericAlloc;
190 PyGPointer_Type.tp_new = PyType_GenericNew;
191 if (PyType_Ready (&PyGPointer_Type)) return -1;
192
193 pygtype = pyg_type_wrapper_new (G_TYPE_POINTER);
194 PyDict_SetItemString (PyGPointer_Type.tp_dict, "__gtype__", pygtype);
195 Py_DECREF (pygtype);
196
197 PyDict_SetItemString (d, "GPointer", (PyObject *)&PyGPointer_Type);
198
199 return 0;
200 }