Debian Codegraph @

gi/pygi-property.c

raw
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright (c) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "pygi-argument.h"
25 #include "pygi-basictype.h"
26 #include "pygi-fundamental.h"
27 #include "pygi-property.h"
28 #include "pygi-repository.h"
29 #include "pygi-type.h"
30 #include "pygi-value.h"
31
32 static GIPropertyInfo *
33 lookup_property_from_object_info (GIObjectInfo *info, const gchar *attr_name)
34 {
35 gssize n_infos;
36 gint i;
37
38 n_infos = gi_object_info_get_n_properties (info);
39 for (i = 0; i < n_infos; i++) {
40 GIPropertyInfo *property_info;
41
42 property_info = gi_object_info_get_property (info, i);
43 g_assert (info != NULL);
44
45 if (strcmp (attr_name,
46 gi_base_info_get_name (GI_BASE_INFO (property_info)))
47 == 0) {
48 return property_info;
49 }
50
51 gi_base_info_unref (property_info);
52 }
53
54 return NULL;
55 }
56
57 static GIPropertyInfo *
58 lookup_property_from_interface_info (GIInterfaceInfo *info,
59 const gchar *attr_name)
60 {
61 gssize n_infos;
62 gint i;
63
64 n_infos = gi_interface_info_get_n_properties (info);
65 for (i = 0; i < n_infos; i++) {
66 GIPropertyInfo *property_info;
67
68 property_info = gi_interface_info_get_property (info, i);
69 g_assert (info != NULL);
70
71 if (strcmp (attr_name,
72 gi_base_info_get_name (GI_BASE_INFO (property_info)))
73 == 0) {
74 return property_info;
75 }
76
77 gi_base_info_unref (property_info);
78 }
79
80 return NULL;
81 }
82
83 static GIPropertyInfo *
84 _pygi_lookup_property_from_g_type (GType g_type, const gchar *attr_name)
85 {
86 GIPropertyInfo *ret = NULL;
87 GIRepository *repository;
88 GIBaseInfo *info;
89
90 repository = pygi_repository_get_default ();
91 info = gi_repository_find_by_gtype (repository, g_type);
92 if (info == NULL) return NULL;
93
94 if (GI_IS_OBJECT_INFO (info))
95 ret =
96 lookup_property_from_object_info ((GIObjectInfo *)info, attr_name);
97 else if (GI_IS_INTERFACE_INFO (info))
98 ret = lookup_property_from_interface_info ((GIInterfaceInfo *)info,
99 attr_name);
100
101 gi_base_info_unref (info);
102
103 return ret;
104 }
105
106 PyObject *
107 pygi_call_do_get_property (PyObject *instance, GParamSpec *pspec)
108 {
109 PyObject *py_pspec;
110 PyObject *retval;
111
112 py_pspec = pygi_fundamental_new (pspec);
113 retval = PyObject_CallMethod (instance, "do_get_property", "O", py_pspec);
114 Py_DECREF (py_pspec);
115 return retval;
116 }
117
118 PyObject *
119 pygi_get_property_value (PyGObject *instance, GParamSpec *pspec)
120 {
121 GIPropertyInfo *property_info = NULL;
122 GValue value = {
123 0,
124 };
125 PyObject *py_value = NULL;
126 GType fundamental;
127
128 if (!(pspec->flags & G_PARAM_READABLE)) {
129 PyErr_Format (PyExc_TypeError, "property %s is not readable",
130 g_param_spec_get_name (pspec));
131 return NULL;
132 }
133
134 /* Fast path which calls the Python getter implementation directly.
135 * See: https://bugzilla.gnome.org/show_bug.cgi?id=723872 */
136 if (pyg_gtype_is_custom (pspec->owner_type)) {
137 return pygi_call_do_get_property ((PyObject *)instance, pspec);
138 }
139
140 Py_BEGIN_ALLOW_THREADS;
141 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
142 g_object_get_property (instance->obj, pspec->name, &value);
143 Py_END_ALLOW_THREADS;
144
145 // special case: unichar, which has a uint type
146 if (G_IS_PARAM_SPEC_UNICHAR (pspec)) {
147 py_value = pygi_gunichar_to_py (g_value_get_uint (&value));
148 goto out;
149 }
150
151 /* Fast path: basic types which don't need GI type info. */
152 fundamental = G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (&value));
153 py_value = pygi_value_to_py_basic_type (&value, fundamental);
154 if (py_value != NULL || PyErr_Occurred ()) {
155 goto out;
156 }
157
158 /* Attempt to marshal through GI.
159 * The owner_type of the pspec gives us the exact type that introduced the
160 * property, even if it is a parent class of the instance in question. */
161 property_info =
162 _pygi_lookup_property_from_g_type (pspec->owner_type, pspec->name);
163 if (property_info) {
164 GITypeInfo *type_info = gi_property_info_get_type_info (property_info);
165 GITransfer transfer =
166 gi_property_info_get_ownership_transfer (property_info);
167 GIArgument arg = _pygi_argument_from_g_value (&value, type_info);
168
169 py_value = pygi_argument_to_py (type_info, arg, transfer);
170
171 gi_base_info_unref (type_info);
172 gi_base_info_unref (property_info);
173
174 if (PyErr_Occurred ()) {
175 return NULL;
176 }
177 }
178
179 /* Fallback to GValue marshalling. */
180 if (py_value == NULL) {
181 py_value = pyg_value_to_pyobject (&value, TRUE);
182 }
183
184 out:
185 g_value_unset (&value);
186 return py_value;
187 }
188
189 PyObject *
190 pygi_get_property_value_by_name (PyGObject *self, gchar *param_name)
191 {
192 GParamSpec *pspec;
193
194 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (self->obj),
195 param_name);
196 if (!pspec) {
197 PyErr_Format (PyExc_TypeError,
198 "object of type `%s' does not have property `%s'",
199 g_type_name (G_OBJECT_TYPE (self->obj)), param_name);
200 return NULL;
201 }
202
203 return pygi_get_property_value (self, pspec);
204 }
205
206 static gint
207 pygi_set_gvalue_from_property_info (GValue *value,
208 GIPropertyInfo *property_info,
209 PyObject *py_value,
210 PyGIArgumentFromPyCleanupData *arg_cleanup)
211 {
212 GITypeInfo *type_info;
213 GIBaseInfo *interface = NULL;
214 GITypeTag type_tag;
215 GITransfer transfer;
216 GIArgument arg;
217 gint ret_value = -1;
218
219 type_info = gi_property_info_get_type_info (property_info);
220 transfer = gi_property_info_get_ownership_transfer (property_info);
221 arg = pygi_argument_from_py (type_info, py_value, transfer, arg_cleanup);
222
223 if (PyErr_Occurred ()) goto out;
224
225 type_tag = gi_type_info_get_tag (type_info);
226 switch (type_tag) {
227 case GI_TYPE_TAG_INTERFACE: {
228 interface = gi_type_info_get_interface (type_info);
229
230 switch (pygi_interface_type_tag (interface)) {
231 case PYGI_INTERFACE_TYPE_TAG_FLAGS:
232 g_value_set_flags (value, arg.v_uint);
233 break;
234 case PYGI_INTERFACE_TYPE_TAG_ENUM:
235 g_value_set_enum (value, arg.v_int);
236 break;
237 case PYGI_INTERFACE_TYPE_TAG_INTERFACE:
238 case PYGI_INTERFACE_TYPE_TAG_OBJECT:
239 if (arg.v_pointer == NULL || G_IS_OBJECT (arg.v_pointer)) {
240 g_value_set_object (value, arg.v_pointer);
241 } else if (!pygi_fundamental_set_value (value, arg.v_pointer)) {
242 PyErr_Format (
243 PyExc_NotImplementedError,
244 "Setting properties of type '%s' is not implemented",
245 g_type_name (G_VALUE_TYPE (value)));
246 goto out;
247 }
248 break;
249 case PYGI_INTERFACE_TYPE_TAG_STRUCT:
250 case PYGI_INTERFACE_TYPE_TAG_UNION: {
251 GType type = gi_registered_type_info_get_g_type (
252 GI_REGISTERED_TYPE_INFO (interface));
253
254 if (g_type_is_a (type, G_TYPE_BOXED)) {
255 g_value_set_boxed (value, arg.v_pointer);
256 } else if (g_type_is_a (type, G_TYPE_VARIANT)) {
257 g_value_set_variant (value, arg.v_pointer);
258 } else {
259 PyErr_Format (
260 PyExc_NotImplementedError,
261 "Setting properties of type '%s' is not implemented",
262 g_type_name (type));
263 goto out;
264 }
265 break;
266 }
267 case PYGI_INTERFACE_TYPE_TAG_CALLBACK:
268 PyErr_Format (PyExc_TypeError,
269 "Setting properties of type '%s' is not supported",
270 g_type_name (G_VALUE_TYPE (value)));
271 goto out;
272 default:
273 g_assert_not_reached ();
274 }
275 break;
276 }
277 case GI_TYPE_TAG_BOOLEAN:
278 g_value_set_boolean (value, arg.v_boolean);
279 break;
280 case GI_TYPE_TAG_INT8:
281 g_value_set_schar (value, arg.v_int8);
282 break;
283 case GI_TYPE_TAG_INT16:
284 case GI_TYPE_TAG_INT32:
285 if (G_VALUE_HOLDS_LONG (value))
286 g_value_set_long (value, arg.v_long);
287 else
288 g_value_set_int (value, arg.v_int);
289 break;
290 case GI_TYPE_TAG_INT64:
291 if (G_VALUE_HOLDS_LONG (value))
292 g_value_set_long (value, arg.v_long);
293 else
294 g_value_set_int64 (value, arg.v_int64);
295 break;
296 case GI_TYPE_TAG_UINT8:
297 g_value_set_uchar (value, arg.v_uint8);
298 break;
299 case GI_TYPE_TAG_UINT16:
300 case GI_TYPE_TAG_UINT32:
301 case GI_TYPE_TAG_UNICHAR:
302 if (G_VALUE_HOLDS_ULONG (value))
303 g_value_set_ulong (value, arg.v_ulong);
304 else
305 g_value_set_uint (value, arg.v_uint);
306 break;
307 case GI_TYPE_TAG_UINT64:
308 if (G_VALUE_HOLDS_ULONG (value))
309 g_value_set_ulong (value, arg.v_ulong);
310 else
311 g_value_set_uint64 (value, arg.v_uint64);
312 break;
313 case GI_TYPE_TAG_FLOAT:
314 g_value_set_float (value, arg.v_float);
315 break;
316 case GI_TYPE_TAG_DOUBLE:
317 g_value_set_double (value, arg.v_double);
318 break;
319 case GI_TYPE_TAG_GTYPE:
320 g_value_set_gtype (value, arg.v_size);
321 break;
322 case GI_TYPE_TAG_UTF8:
323 case GI_TYPE_TAG_FILENAME:
324 g_value_set_string (value, arg.v_string);
325 break;
326 case GI_TYPE_TAG_GHASH:
327 g_value_set_boxed (value, arg.v_pointer);
328 break;
329 case GI_TYPE_TAG_ARRAY:
330 case GI_TYPE_TAG_GLIST:
331 case GI_TYPE_TAG_GSLIST:
332 if (G_VALUE_HOLDS_BOXED (value))
333 g_value_set_boxed (value, arg.v_pointer);
334 else
335 g_value_set_pointer (value, arg.v_pointer);
336 break;
337 case GI_TYPE_TAG_VOID:
338 case GI_TYPE_TAG_ERROR:
339 PyErr_Format (
340 PyExc_NotImplementedError,
341 "Setting properties of type %s is not implemented",
342 gi_type_tag_to_string (gi_type_info_get_tag (type_info)));
343 goto out;
344 default:
345 g_assert_not_reached ();
346 }
347
348 ret_value = 0;
349
350 out:
351 if (interface != NULL) gi_base_info_unref (interface);
352 if (type_info != NULL) gi_base_info_unref (type_info);
353
354 return ret_value;
355 }
356
357 gint
358 pygi_set_gvalue_for_pspec (GValue *value, GParamSpec *pspec,
359 PyObject *py_value,
360 PyGIArgumentFromPyCleanupData *cleanup_data)
361 {
362 gint ret_value;
363
364 /* The owner_type of the pspec gives us the exact type that introduced the
365 * property, even if it is a parent class of the instance in question. */
366 GIPropertyInfo *property_info =
367 _pygi_lookup_property_from_g_type (pspec->owner_type, pspec->name);
368
369 /* Set from the GIPropertyInfo, we have introspection data that we can
370 * use here */
371 if (property_info != NULL) {
372 ret_value = pygi_set_gvalue_from_property_info (
373 value, property_info, py_value, cleanup_data);
374 } else {
375 /* We don't have introspection data, use the legacy path */
376 ret_value = pyg_param_gvalue_from_pyobject (value, py_value, pspec);
377 }
378
379 if (property_info) gi_base_info_unref (property_info);
380
381 return ret_value;
382 }
383
384 gint
385 pygi_set_property_value (PyGObject *instance, GParamSpec *pspec,
386 PyObject *py_value)
387 {
388 GValue value = { 0 };
389 PyGIArgumentFromPyCleanupData arg_cleanup = { 0 };
390 gint ret_value;
391
392 if (pspec->flags & G_PARAM_CONSTRUCT_ONLY) {
393 PyErr_Format (PyExc_TypeError,
394 "property '%s' can only be set in constructor",
395 pspec->name);
396 return -1;
397 }
398
399 if (!(pspec->flags & G_PARAM_WRITABLE)) {
400 PyErr_Format (PyExc_TypeError, "property '%s' is not writable",
401 pspec->name);
402 return -1;
403 }
404
405 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
406
407 // special case: unichar has an internal type uint
408 if (G_IS_PARAM_SPEC_UNICHAR (pspec)) {
409 gunichar u;
410
411 if (!pygi_gunichar_from_py (py_value, &u)) {
412 return -1;
413 }
414 g_value_set_uint (&value, u);
415 g_object_set_property (instance->obj, pspec->name, &value);
416 g_value_unset (&value);
417 return 0;
418 }
419
420 ret_value =
421 pygi_set_gvalue_for_pspec (&value, pspec, py_value, &arg_cleanup);
422
423 if (ret_value == 0) {
424 g_object_set_property (instance->obj, pspec->name, &value);
425 g_value_unset (&value);
426 } else {
427 /* If we already have an error set, don't override it,
428 * otherwise raise a TypError indcating that we couldn't
429 * set the property */
430 if (!PyErr_Occurred ()) {
431 PyObject *pvalue_str = PyObject_Repr (py_value);
432 PyErr_Format (PyExc_TypeError,
433 "could not convert %s to type '%s' when setting "
434 "property '%s.%s'",
435 PyUnicode_AsUTF8 (pvalue_str),
436 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
437 G_OBJECT_TYPE_NAME (instance->obj), pspec->name);
438 Py_DECREF (pvalue_str);
439 }
440 }
441
442 pygi_argument_from_py_cleanup (&arg_cleanup);
443
444 return ret_value;
445 }