Debian Codegraph @

gi/pygi-fundamental.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2009 Simon van der Linden <[email protected]>
5 * Copyright (C) 2010 Tomeu Vizoso <[email protected]>
6 * Copyright (C) 2012 Bastian Winkler <[email protected]>
7 *
8 * pygi-fundamental.c: wrapper to handle instances of fundamental types.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 * USA
24 */
25
26
27 #include "pygobject-types.h"
28 #include "pygi-info.h"
29 #include "pygi-util.h"
30 #include "pygi-fundamental.h"
31 #include "pygi-repository.h"
32 #include "pygobject-object.h" // for pygobject_lookup_class
33
34
35 static PyGIFundamental *_pygi_fundamental_new_internal (PyTypeObject *type,
36 gpointer pointer);
37
38
39 static void
40 fundamental_dealloc (PyGIFundamental *self)
41 {
42 pygi_fundamental_unref (self);
43 self->instance = NULL;
44
45 PyObject_GC_UnTrack ((PyObject *)self);
46 if (self->weaklist) PyObject_ClearWeakRefs ((PyObject *)self);
47
48 Py_TYPE (self)->tp_free ((PyObject *)self);
49 }
50
51 static PyObject *
52 fundamental_new (PyTypeObject *type, PyObject *args, PyObject *kwargs)
53 {
54 GIBaseInfo *info;
55 gpointer pointer;
56 PyGIFundamental *self = NULL;
57 GType g_type;
58
59 info = _pygi_object_get_gi_info ((PyObject *)type, &PyGIObjectInfo_Type);
60 if (info == NULL) {
61 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
62 PyErr_Format (PyExc_TypeError,
63 "missing introspection information");
64 }
65 return NULL;
66 }
67
68 g_type = pyg_type_from_object ((PyObject *)type);
69 if (G_TYPE_IS_ABSTRACT (g_type)) {
70 PyErr_Format (PyExc_TypeError, "cannot instantiate abstract type %s",
71 g_type_name (g_type));
72 return NULL;
73 }
74
75 pointer = g_type_create_instance (g_type);
76 if (pointer == NULL) {
77 PyErr_NoMemory ();
78 goto out;
79 }
80
81 self = _pygi_fundamental_new_internal (type, pointer);
82 if (self == NULL) {
83 g_free (pointer);
84 PyErr_Format (PyExc_TypeError,
85 "cannot instantiate Fundamental Python wrapper type %s",
86 g_type_name (g_type));
87 goto out;
88 }
89
90 out:
91 gi_base_info_unref (info);
92
93 return (PyObject *)self;
94 }
95
96 static int
97 fundamental_init (PyObject *self, PyObject *args, PyObject *kwargs)
98 {
99 static char *kwlist[] = { NULL };
100
101 if (!PyArg_ParseTupleAndKeywords (args, kwargs, ":Fundamental.__init__",
102 kwlist)) {
103 return -1;
104 }
105 return 0;
106 }
107
108 static PyObject *
109 fundamental_richcompare (PyObject *self, PyObject *other, int op)
110 {
111 if (Py_TYPE (self) == Py_TYPE (other)) {
112 return pyg_ptr_richcompare (((PyGIFundamental *)self)->instance,
113 ((PyGIFundamental *)other)->instance, op);
114 } else {
115 Py_INCREF (Py_NotImplemented);
116 return Py_NotImplemented;
117 }
118 }
119
120 static Py_hash_t
121 fundamental_hash (PyGIFundamental *self)
122 {
123 return (Py_hash_t)(gintptr)(self->instance);
124 }
125
126 static PyObject *
127 fundamental_repr (PyGIFundamental *self)
128 {
129 gchar buf[128];
130
131 g_snprintf (buf, sizeof (buf), "<%s at 0x%" G_GUINTPTR_FORMAT ">",
132 g_type_name (self->gtype), (guintptr)self->instance);
133 return PyUnicode_FromString (buf);
134 }
135
136 PYGI_DEFINE_TYPE ("gi.Fundamental", PyGIFundamental_Type, PyGIFundamental);
137
138
139 PyObject *
140 pygi_fundamental_new (gpointer instance)
141 {
142 GType gtype;
143 PyTypeObject *type;
144 PyGIFundamental *self;
145
146 if (!instance) {
147 Py_RETURN_NONE;
148 }
149
150 gtype = G_TYPE_FROM_INSTANCE (instance);
151 type = pygobject_lookup_class (gtype);
152
153 self = _pygi_fundamental_new_internal (type, instance);
154 pygi_fundamental_ref (self);
155 return (PyObject *)self;
156 }
157
158 static PyGIFundamental *
159 _pygi_fundamental_new_internal (PyTypeObject *type, gpointer instance)
160 {
161 PyGIFundamental *self;
162 GIObjectInfo *info;
163
164 if (!PyType_IsSubtype (type, &PyGIFundamental_Type)) {
165 PyErr_SetString (PyExc_TypeError,
166 "must be a subtype of gi.Fundamental");
167 return NULL;
168 }
169
170 info = GI_OBJECT_INFO (
171 _pygi_object_get_gi_info ((PyObject *)type, &PyGIObjectInfo_Type));
172 if (info == NULL) {
173 if (PyErr_ExceptionMatches (PyExc_AttributeError)) {
174 PyErr_Format (PyExc_TypeError,
175 "missing introspection information");
176 }
177 return NULL;
178 }
179
180 self = (PyGIFundamental *)type->tp_alloc (type, 0);
181 if (self == NULL) {
182 return NULL;
183 }
184
185 self->gtype = pyg_type_from_object ((PyObject *)type);
186 self->instance = instance;
187
188 self->ref_func = gi_object_info_get_ref_function_pointer (info);
189 self->unref_func = gi_object_info_get_unref_function_pointer (info);
190
191 /* A special case for GParamSpec. It has a floating reference when created.
192 * We make sure we have a proper reference, so we can ref/unref normally.
193 */
194 if (G_TYPE_IS_PARAM (self->gtype)) {
195 g_param_spec_ref_sink (self->instance);
196 }
197
198 gi_base_info_unref (info);
199
200 return self;
201 }
202
203 void
204 pygi_fundamental_ref (PyGIFundamental *self)
205 {
206 if (self->ref_func && self->instance) self->ref_func (self->instance);
207 }
208
209 void
210 pygi_fundamental_unref (PyGIFundamental *self)
211 {
212 if (self->unref_func && self->instance) self->unref_func (self->instance);
213 }
214
215 GTypeInstance *
216 pygi_fundamental_get (PyObject *self)
217 {
218 if (PyObject_TypeCheck (self, &PyGIFundamental_Type)) {
219 return ((PyGIFundamental *)self)->instance;
220 } else {
221 PyErr_SetString (PyExc_TypeError, "Expected GObject Fundamental type");
222 return NULL;
223 }
224 }
225
226 int
227 pygi_fundamental_register_types (PyObject *m)
228 {
229 Py_SET_TYPE (&PyGIFundamental_Type, &PyType_Type);
230 g_assert (Py_TYPE (&PyGIFundamental_Type) != NULL);
231
232 PyGIFundamental_Type.tp_alloc = PyType_GenericAlloc;
233 PyGIFundamental_Type.tp_new = (newfunc)fundamental_new;
234 PyGIFundamental_Type.tp_init = (initproc)fundamental_init;
235 PyGIFundamental_Type.tp_dealloc = (destructor)fundamental_dealloc;
236 PyGIFundamental_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
237 PyGIFundamental_Type.tp_richcompare = fundamental_richcompare;
238 PyGIFundamental_Type.tp_repr = (reprfunc)fundamental_repr;
239 PyGIFundamental_Type.tp_hash = (hashfunc)fundamental_hash;
240 PyGIFundamental_Type.tp_weaklistoffset =
241 offsetof (PyGIFundamental, weaklist);
242
243 if (PyType_Ready (&PyGIFundamental_Type)) return -1;
244 if (PyModule_AddObject (m, "Fundamental",
245 (PyObject *)&PyGIFundamental_Type))
246 return -1;
247
248 return 0;
249 }
250
251
252 GTypeInstance *
253 pygi_fundamental_from_value (const GValue *value)
254 {
255 GIRepository *repository =
256 pygi_repository_get_default (); // Access to system wide (default) repo instead
257 GIBaseInfo *info =
258 gi_repository_find_by_gtype (repository, G_VALUE_TYPE (value));
259 GTypeInstance *instance = NULL;
260
261 if (info == NULL) return NULL;
262
263 if (GI_IS_OBJECT_INFO (info)) {
264 GIObjectInfoGetValueFunction get_value_func =
265 gi_object_info_get_get_value_function_pointer (
266 (GIObjectInfo *)info);
267 if (get_value_func) {
268 instance = get_value_func (value);
269 }
270 }
271
272 gi_base_info_unref (info);
273
274 return instance;
275 }
276
277 gboolean
278 pygi_fundamental_set_value (GValue *value, GTypeInstance *instance)
279 {
280 GIRepository *repository;
281 GIBaseInfo *info;
282 gboolean result = FALSE;
283
284 if (instance == NULL) return result;
285
286 repository = pygi_repository_get_default ();
287 info = gi_repository_find_by_gtype (repository,
288 G_TYPE_FROM_INSTANCE (instance));
289
290 if (info == NULL) return result;
291
292 if (GI_IS_OBJECT_INFO (info)) {
293 GIObjectInfoSetValueFunction set_value_func =
294 gi_object_info_get_set_value_function_pointer (
295 (GIObjectInfo *)info);
296 if (set_value_func) {
297 set_value_func (value, instance);
298 result = TRUE;
299 }
300 }
301
302 gi_base_info_unref (info);
303 return result;
304 }