Debian Codegraph @

gi/pygi-value.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "pygi-value.h"
19
20 #include "pygboxed.h"
21 #include "pygenum.h"
22 #include "pygflags.h"
23 #include "pygi-argument.h"
24 #include "pygi-basictype.h"
25 #include "pygi-fundamental.h"
26 #include "pygi-repository.h"
27 #include "pygi-struct.h"
28 #include "pygi-type.h"
29 #include "pygobject-object.h"
30 #include "pygpointer.h"
31
32
33 /* glib 2.62 has started to print warnings for these which can't be disabled selectively, so just copy them here */
34 #define PYGI_TYPE_VALUE_ARRAY (g_value_array_get_type ())
35 #define PYGI_IS_PARAM_SPEC_VALUE_ARRAY(pspec) \
36 (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), PYGI_TYPE_VALUE_ARRAY))
37 #define PYGI_PARAM_SPEC_VALUE_ARRAY(pspec) \
38 (G_TYPE_CHECK_INSTANCE_CAST ((pspec), g_param_spec_types[18], \
39 GParamSpecValueArray))
40
41 GIArgument
42 _pygi_argument_from_g_value (const GValue *value, GITypeInfo *type_info)
43 {
44 GIArgument arg = PYGI_ARG_INIT;
45
46 GITypeTag type_tag = gi_type_info_get_tag (type_info);
47
48 /* For the long handling: long can be equivalent to
49 int32 or int64, depending on the architecture, but
50 gi doesn't tell us (and same for ulong)
51 */
52 switch (type_tag) {
53 case GI_TYPE_TAG_BOOLEAN:
54 arg.v_boolean = g_value_get_boolean (value);
55 break;
56 case GI_TYPE_TAG_INT8:
57 arg.v_int8 = g_value_get_schar (value);
58 break;
59 case GI_TYPE_TAG_INT16:
60 case GI_TYPE_TAG_INT32:
61 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_LONG))
62 arg.v_int32 = (gint32)g_value_get_long (value);
63 else
64 arg.v_int32 = (gint32)g_value_get_int (value);
65 break;
66 case GI_TYPE_TAG_INT64:
67 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_LONG))
68 arg.v_int64 = g_value_get_long (value);
69 else
70 arg.v_int64 = g_value_get_int64 (value);
71 break;
72 case GI_TYPE_TAG_UINT8:
73 arg.v_uint8 = g_value_get_uchar (value);
74 break;
75 case GI_TYPE_TAG_UINT16:
76 case GI_TYPE_TAG_UINT32:
77 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_ULONG))
78 arg.v_uint32 = (guint32)g_value_get_ulong (value);
79 else
80 arg.v_uint32 = (guint32)g_value_get_uint (value);
81 break;
82 case GI_TYPE_TAG_UINT64:
83 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_ULONG))
84 arg.v_uint64 = g_value_get_ulong (value);
85 else
86 arg.v_uint64 = g_value_get_uint64 (value);
87 break;
88 case GI_TYPE_TAG_UNICHAR:
89 arg.v_uint32 = g_value_get_schar (value);
90 break;
91 case GI_TYPE_TAG_FLOAT:
92 arg.v_float = g_value_get_float (value);
93 break;
94 case GI_TYPE_TAG_DOUBLE:
95 arg.v_double = g_value_get_double (value);
96 break;
97 case GI_TYPE_TAG_GTYPE:
98 arg.v_size = g_value_get_gtype (value);
99 break;
100 case GI_TYPE_TAG_UTF8:
101 case GI_TYPE_TAG_FILENAME:
102 /* Callers are responsible for ensuring the GValue stays alive
103 * long enough for the string to be copied. */
104 arg.v_string = (char *)g_value_get_string (value);
105 break;
106 case GI_TYPE_TAG_GLIST:
107 case GI_TYPE_TAG_GSLIST:
108 case GI_TYPE_TAG_ARRAY:
109 case GI_TYPE_TAG_GHASH:
110 if (G_VALUE_HOLDS_BOXED (value))
111 arg.v_pointer = g_value_get_boxed (value);
112 else
113 /* e. g. GSettings::change-event */
114 arg.v_pointer = g_value_get_pointer (value);
115 break;
116 case GI_TYPE_TAG_INTERFACE: {
117 GIBaseInfo *info;
118
119 info = gi_type_info_get_interface (type_info);
120
121 switch (pygi_interface_type_tag (info)) {
122 case PYGI_INTERFACE_TYPE_TAG_FLAGS:
123 arg.v_uint = g_value_get_flags (value);
124 break;
125 case PYGI_INTERFACE_TYPE_TAG_ENUM:
126 arg.v_int = g_value_get_enum (value);
127 break;
128 case PYGI_INTERFACE_TYPE_TAG_OBJECT:
129 case PYGI_INTERFACE_TYPE_TAG_INTERFACE:
130 if (G_VALUE_HOLDS_PARAM (value))
131 arg.v_pointer = g_value_get_param (value);
132 else if (G_VALUE_HOLDS_OBJECT (value))
133 arg.v_pointer = g_value_get_object (value);
134 else
135 arg.v_pointer = pygi_fundamental_from_value (value);
136 break;
137 case PYGI_INTERFACE_TYPE_TAG_STRUCT:
138 case PYGI_INTERFACE_TYPE_TAG_UNION:
139 if (G_VALUE_HOLDS (value, G_TYPE_BOXED)) {
140 arg.v_pointer = g_value_get_boxed (value);
141 } else if (G_VALUE_HOLDS (value, G_TYPE_VARIANT)) {
142 arg.v_pointer = g_value_get_variant (value);
143 } else if (G_VALUE_HOLDS (value, G_TYPE_POINTER)) {
144 arg.v_pointer = g_value_get_pointer (value);
145 } else {
146 PyErr_Format (
147 PyExc_NotImplementedError,
148 "Converting GValue's of type '%s' is not implemented.",
149 g_type_name (G_VALUE_TYPE (value)));
150 }
151 break;
152 case PYGI_INTERFACE_TYPE_TAG_CALLBACK:
153 PyErr_Format (PyExc_TypeError,
154 "Converting GValue's of type '%s' is not supported.",
155 g_type_name (G_TYPE_FROM_INSTANCE (info)));
156 break;
157 default:
158 g_assert_not_reached ();
159 }
160
161 gi_base_info_unref (info);
162
163 break;
164 }
165 case GI_TYPE_TAG_ERROR:
166 arg.v_pointer = g_value_get_boxed (value);
167 break;
168 case GI_TYPE_TAG_VOID:
169 arg.v_pointer = g_value_get_pointer (value);
170 break;
171 default:
172 g_assert_not_reached ();
173 }
174
175 return arg;
176 }
177
178
179 /* Ignore g_value_array deprecations. Although they are deprecated,
180 * we still need to support the marshaling of them in PyGObject.
181 */
182 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
183
184 static int
185 pyg_value_array_from_pyobject (GValue *value, PyObject *obj,
186 const GParamSpecValueArray *pspec)
187 {
188 Py_ssize_t seq_len;
189 GValueArray *value_array;
190 guint len, i;
191
192 seq_len = PySequence_Length (obj);
193 if (seq_len == -1) {
194 PyErr_Clear ();
195 return -1;
196 }
197 len = (guint)seq_len;
198
199 if (pspec && pspec->fixed_n_elements > 0 && len != pspec->fixed_n_elements)
200 return -1;
201
202 value_array = g_value_array_new (len);
203
204 for (i = 0; i < len; ++i) {
205 PyObject *item = PySequence_GetItem (obj, i);
206 GType type;
207
208 if (!item) {
209 PyErr_Clear ();
210 g_value_array_free (value_array);
211 return -1;
212 }
213
214 if (pspec && pspec->element_spec)
215 type = G_PARAM_SPEC_VALUE_TYPE (pspec->element_spec);
216 else if (Py_IsNone (item))
217 type = G_TYPE_POINTER; /* store None as NULL */
218 else {
219 type = pyg_type_from_object ((PyObject *)Py_TYPE (item));
220 if (!type) {
221 PyErr_Clear ();
222 g_value_array_free (value_array);
223 Py_DECREF (item);
224 return -1;
225 }
226 }
227
228 if (type == G_TYPE_VALUE) {
229 const GValue *item_value = pyg_boxed_get (item, GValue);
230 g_value_array_append (value_array, item_value);
231 } else {
232 GValue item_value = {
233 0,
234 };
235 int status;
236
237 g_value_init (&item_value, type);
238 status = (pspec && pspec->element_spec)
239 ? pyg_param_gvalue_from_pyobject (&item_value, item,
240 pspec->element_spec)
241 : pyg_value_from_pyobject (&item_value, item);
242 Py_DECREF (item);
243
244 if (status == -1) {
245 g_value_array_free (value_array);
246 g_value_unset (&item_value);
247 return -1;
248 }
249 g_value_array_append (value_array, &item_value);
250 g_value_unset (&item_value);
251 }
252 }
253
254 g_value_take_boxed (value, value_array);
255 return 0;
256 }
257
258 G_GNUC_END_IGNORE_DEPRECATIONS
259
260 static int
261 pyg_array_from_pyobject (GValue *value, PyObject *obj)
262 {
263 Py_ssize_t len, i;
264 GArray *array;
265
266 len = PySequence_Length (obj);
267 if (len < 0 || (size_t)len > UINT_MAX) {
268 PyErr_Clear ();
269 return -1;
270 }
271
272 array = g_array_sized_new (FALSE, TRUE, sizeof (GValue), (guint)len);
273 g_array_set_clear_func (array, (GDestroyNotify)g_value_unset);
274
275 for (i = 0; i < len; ++i) {
276 PyObject *item = PySequence_GetItem (obj, i);
277 GType type;
278 GValue item_value = {
279 0,
280 };
281 int status;
282
283 if (!item) {
284 PyErr_Clear ();
285 g_array_free (array, TRUE);
286 return -1;
287 }
288
289 if (Py_IsNone (item))
290 type = G_TYPE_POINTER; /* store None as NULL */
291 else {
292 type = pyg_type_from_object ((PyObject *)Py_TYPE (item));
293 if (!type) {
294 PyErr_Clear ();
295 g_array_free (array, TRUE);
296 Py_DECREF (item);
297 return -1;
298 }
299 }
300
301 g_value_init (&item_value, type);
302 status = pyg_value_from_pyobject (&item_value, item);
303 Py_DECREF (item);
304
305 if (status == -1) {
306 g_array_free (array, TRUE);
307 g_value_unset (&item_value);
308 return -1;
309 }
310
311 g_array_append_val (array, item_value);
312 }
313
314 g_value_take_boxed (value, array);
315 return 0;
316 }
317
318 /**
319 * pyg_value_from_pyobject_with_error:
320 * @value: the GValue object to store the converted value in.
321 * @obj: the Python object to convert.
322 *
323 * This function converts a Python object and stores the result in a
324 * GValue. The GValue must be initialised in advance with
325 * g_value_init(). If the Python object can't be converted to the
326 * type of the GValue, then an error is returned.
327 *
328 * Returns: 0 on success, -1 on error.
329 */
330 int
331 pyg_value_from_pyobject_with_error (GValue *value, PyObject *obj)
332 {
333 GType value_type = G_VALUE_TYPE (value);
334
335 switch (G_TYPE_FUNDAMENTAL (value_type)) {
336 case G_TYPE_INTERFACE:
337 /* we only handle interface types that have a GObject prereq */
338 if (g_type_is_a (value_type, G_TYPE_OBJECT)) {
339 if (Py_IsNone (obj))
340 g_value_set_object (value, NULL);
341 else {
342 if (!PyObject_TypeCheck (obj, &PyGObject_Type)) {
343 PyErr_SetString (PyExc_TypeError, "GObject is required");
344 return -1;
345 }
346 if (!G_TYPE_CHECK_INSTANCE_TYPE (pygobject_get (obj),
347 value_type)) {
348 PyErr_SetString (PyExc_TypeError,
349 "Invalid GObject type for assignment");
350 return -1;
351 }
352 g_value_set_object (value, pygobject_get (obj));
353 }
354 } else {
355 PyErr_SetString (PyExc_TypeError, "Unsupported conversion");
356 return -1;
357 }
358 break;
359 case G_TYPE_CHAR: {
360 gint8 temp;
361 if (pygi_gschar_from_py (obj, &temp)) {
362 g_value_set_schar (value, temp);
363 return 0;
364 } else
365 return -1;
366 }
367 case G_TYPE_UCHAR: {
368 guchar temp;
369 if (pygi_guchar_from_py (obj, &temp)) {
370 g_value_set_uchar (value, temp);
371 return 0;
372 } else
373 return -1;
374 }
375 case G_TYPE_BOOLEAN: {
376 gboolean temp;
377 if (pygi_gboolean_from_py (obj, &temp)) {
378 g_value_set_boolean (value, temp);
379 return 0;
380 } else
381 return -1;
382 }
383 case G_TYPE_INT: {
384 gint temp;
385 if (pygi_gint_from_py (obj, &temp)) {
386 g_value_set_int (value, temp);
387 return 0;
388 } else
389 return -1;
390 }
391 case G_TYPE_UINT: {
392 guint temp;
393 if (pygi_guint_from_py (obj, &temp)) {
394 g_value_set_uint (value, temp);
395 return 0;
396 } else
397 return -1;
398 }
399 case G_TYPE_LONG: {
400 glong temp;
401 if (pygi_glong_from_py (obj, &temp)) {
402 g_value_set_long (value, temp);
403 return 0;
404 } else
405 return -1;
406 }
407 case G_TYPE_ULONG: {
408 gulong temp;
409 if (pygi_gulong_from_py (obj, &temp)) {
410 g_value_set_ulong (value, temp);
411 return 0;
412 } else
413 return -1;
414 }
415 case G_TYPE_INT64: {
416 gint64 temp;
417 if (pygi_gint64_from_py (obj, &temp)) {
418 g_value_set_int64 (value, temp);
419 return 0;
420 } else
421 return -1;
422 }
423 case G_TYPE_UINT64: {
424 guint64 temp;
425 if (pygi_guint64_from_py (obj, &temp)) {
426 g_value_set_uint64 (value, temp);
427 return 0;
428 } else
429 return -1;
430 }
431 case G_TYPE_ENUM: {
432 gint val = 0;
433 if (pyg_enum_get_value (G_VALUE_TYPE (value), obj, &val) < 0) {
434 return -1;
435 }
436 g_value_set_enum (value, val);
437 break;
438 }
439 case G_TYPE_FLAGS: {
440 guint val = 0;
441 if (pyg_flags_get_value (G_VALUE_TYPE (value), obj, &val) < 0) {
442 return -1;
443 }
444 g_value_set_flags (value, val);
445 return 0;
446 }
447 case G_TYPE_FLOAT: {
448 gfloat temp;
449 if (pygi_gfloat_from_py (obj, &temp)) {
450 g_value_set_float (value, temp);
451 return 0;
452 } else
453 return -1;
454 }
455 case G_TYPE_DOUBLE: {
456 gdouble temp;
457 if (pygi_gdouble_from_py (obj, &temp)) {
458 g_value_set_double (value, temp);
459 return 0;
460 } else
461 return -1;
462 }
463 case G_TYPE_STRING: {
464 gchar *temp;
465 if (pygi_utf8_from_py (obj, &temp)) {
466 g_value_take_string (value, temp);
467 return 0;
468 } else {
469 /* also allows setting anything implementing __str__ */
470 PyObject *str;
471 PyErr_Clear ();
472 str = PyObject_Str (obj);
473 if (str == NULL) return -1;
474 if (pygi_utf8_from_py (str, &temp)) {
475 Py_DECREF (str);
476 g_value_take_string (value, temp);
477 return 0;
478 }
479 Py_DECREF (str);
480 return -1;
481 }
482 }
483 case G_TYPE_POINTER:
484 if (Py_IsNone (obj))
485 g_value_set_pointer (value, NULL);
486 else if (PyObject_TypeCheck (obj, &PyGPointer_Type)
487 && G_VALUE_HOLDS (value, ((PyGPointer *)obj)->gtype))
488 g_value_set_pointer (value, pyg_pointer_get (obj, gpointer));
489 else if (PyCapsule_CheckExact (obj))
490 g_value_set_pointer (value, PyCapsule_GetPointer (obj, NULL));
491 else if (G_VALUE_HOLDS_GTYPE (value))
492 g_value_set_gtype (value, pyg_type_from_object (obj));
493 else {
494 PyErr_SetString (PyExc_TypeError, "Expected pointer");
495 return -1;
496 }
497 break;
498 case G_TYPE_BOXED: {
499 PyGTypeMarshal *bm;
500 gboolean holds_value_array;
501
502 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
503 holds_value_array = G_VALUE_HOLDS (value, PYGI_TYPE_VALUE_ARRAY);
504 G_GNUC_END_IGNORE_DEPRECATIONS
505
506 if (Py_IsNone (obj))
507 g_value_set_boxed (value, NULL);
508 else if (G_VALUE_HOLDS (value, PY_TYPE_OBJECT))
509 g_value_set_boxed (value, obj);
510 else if (PyObject_TypeCheck (obj, &PyGBoxed_Type)
511 && G_VALUE_HOLDS (value, ((PyGBoxed *)obj)->gtype))
512 g_value_set_boxed (value, pyg_boxed_get (obj, gpointer));
513 else if (G_VALUE_HOLDS (value, G_TYPE_VALUE)) {
514 GType type;
515 GValue *n_value;
516
517 type = pyg_type_from_object ((PyObject *)Py_TYPE (obj));
518 if (G_UNLIKELY (!type)) {
519 return -1;
520 }
521 n_value = g_new0 (GValue, 1);
522 g_value_init (n_value, type);
523 g_value_take_boxed (value, n_value);
524 return pyg_value_from_pyobject_with_error (n_value, obj);
525 } else if (PySequence_Check (obj) && holds_value_array)
526 return pyg_value_array_from_pyobject (value, obj, NULL);
527
528 else if (PySequence_Check (obj) && G_VALUE_HOLDS (value, G_TYPE_ARRAY))
529 return pyg_array_from_pyobject (value, obj);
530 else if (PyUnicode_Check (obj)
531 && G_VALUE_HOLDS (value, G_TYPE_GSTRING)) {
532 GString *string;
533 char *buffer;
534 Py_ssize_t len;
535 buffer = PyUnicode_AsUTF8AndSize (obj, &len);
536 if (buffer == NULL) return -1;
537 string = g_string_new_len (buffer, len);
538 g_value_set_boxed (value, string);
539 g_string_free (string, TRUE);
540 break;
541 } else if ((bm = pyg_type_lookup (G_VALUE_TYPE (value))) != NULL)
542 return bm->tovalue (value, obj);
543 else if (PyCapsule_CheckExact (obj))
544 g_value_set_boxed (value, PyCapsule_GetPointer (obj, NULL));
545 else {
546 PyErr_SetString (PyExc_TypeError, "Expected Boxed");
547 return -1;
548 }
549 break;
550 }
551 case G_TYPE_OBJECT:
552 if (Py_IsNone (obj)) {
553 g_value_set_object (value, NULL);
554 } else if (PyObject_TypeCheck (obj, &PyGObject_Type)
555 && G_TYPE_CHECK_INSTANCE_TYPE (pygobject_get (obj),
556 G_VALUE_TYPE (value))) {
557 g_value_set_object (value, pygobject_get (obj));
558 } else {
559 PyErr_SetString (PyExc_TypeError, "Expected GObject");
560 return -1;
561 }
562 break;
563 case G_TYPE_VARIANT: {
564 if (Py_IsNone (obj))
565 g_value_set_variant (value, NULL);
566 else if (pyg_type_from_object_strict (obj, FALSE) == G_TYPE_VARIANT)
567 g_value_set_variant (value, pyg_boxed_get (obj, GVariant));
568 else {
569 PyErr_SetString (PyExc_TypeError, "Expected Variant");
570 return -1;
571 }
572 break;
573 }
574 default: {
575 PyGTypeMarshal *bm;
576 if ((bm = pyg_type_lookup (G_VALUE_TYPE (value))) != NULL) {
577 return bm->tovalue (value, obj);
578 } else {
579 GIRepository *repository;
580 GIBaseInfo *info;
581 GIObjectInfoSetValueFunction set_value_func = NULL;
582
583 if (!PyObject_TypeCheck (obj, &PyGIFundamental_Type)) {
584 PyErr_SetString (PyExc_TypeError,
585 "Fundamental type is required");
586 return -1;
587 }
588 if (!G_TYPE_CHECK_INSTANCE_TYPE (pygi_fundamental_get (obj),
589 value_type)) {
590 PyErr_SetString (PyExc_TypeError,
591 "Invalid fundamental type for assignment");
592 return -1;
593 }
594
595 repository = pygi_repository_get_default ();
596 info = gi_repository_find_by_gtype (repository, value_type);
597
598 if (info && GI_IS_OBJECT_INFO (info)) {
599 set_value_func =
600 gi_object_info_get_set_value_function_pointer (
601 (GIObjectInfo *)info);
602 if (set_value_func) {
603 set_value_func (value, pygi_fundamental_get (obj));
604 } else {
605 PyErr_SetString (
606 PyExc_TypeError,
607 "No set-value function for fundamental type");
608 }
609 } else {
610 PyErr_SetString (PyExc_TypeError, "Unknown value type");
611 }
612
613 if (info) gi_base_info_unref (info);
614 }
615 break;
616 }
617 }
618
619 /* If an error occurred, unset the GValue but don't clear the Python error. */
620 if (PyErr_Occurred ()) {
621 g_value_unset (value);
622 return -1;
623 }
624
625 return 0;
626 }
627
628 /**
629 * pyg_value_from_pyobject:
630 * @value: the GValue object to store the converted value in.
631 * @obj: the Python object to convert.
632 *
633 * Same basic function as pyg_value_from_pyobject_with_error but clears
634 * any Python errors before returning.
635 *
636 * Returns: 0 on success, -1 on error.
637 */
638 int
639 pyg_value_from_pyobject (GValue *value, PyObject *obj)
640 {
641 int res = pyg_value_from_pyobject_with_error (value, obj);
642
643 if (PyErr_Occurred ()) {
644 PyErr_Clear ();
645 return -1;
646 }
647 return res;
648 }
649
650 /**
651 * pygi_value_to_py_basic_type:
652 * @value: the GValue object.
653 *
654 * This function creates/returns a Python wrapper object that
655 * represents the GValue passed as an argument limited to supporting basic types
656 * like ints, bools, and strings.
657 *
658 * Returns: a PyObject representing the value. NULL if value could not be wrapped.
659 * A error may have been set if translation failed.
660 */
661 PyObject *
662 pygi_value_to_py_basic_type (const GValue *value, GType fundamental)
663 {
664 switch (fundamental) {
665 case G_TYPE_CHAR:
666 return PyLong_FromLong (g_value_get_schar (value));
667 case G_TYPE_UCHAR:
668 return PyLong_FromLong (g_value_get_uchar (value));
669 case G_TYPE_BOOLEAN:
670 return pygi_gboolean_to_py (g_value_get_boolean (value));
671 case G_TYPE_INT:
672 return pygi_gint_to_py (g_value_get_int (value));
673 case G_TYPE_UINT:
674 return pygi_guint_to_py (g_value_get_uint (value));
675 case G_TYPE_LONG:
676 return pygi_glong_to_py (g_value_get_long (value));
677 case G_TYPE_ULONG:
678 return pygi_gulong_to_py (g_value_get_ulong (value));
679 case G_TYPE_INT64:
680 return pygi_gint64_to_py (g_value_get_int64 (value));
681 case G_TYPE_UINT64:
682 return pygi_guint64_to_py (g_value_get_uint64 (value));
683 case G_TYPE_ENUM:
684 return pyg_enum_from_gtype (G_VALUE_TYPE (value),
685 g_value_get_enum (value));
686 case G_TYPE_FLAGS:
687 return pyg_flags_from_gtype (G_VALUE_TYPE (value),
688 g_value_get_flags (value));
689 case G_TYPE_FLOAT:
690 return pygi_gfloat_to_py (g_value_get_float (value));
691 case G_TYPE_DOUBLE:
692 return pygi_gdouble_to_py (g_value_get_double (value));
693 case G_TYPE_STRING:
694 return pygi_utf8_to_py (g_value_get_string (value));
695 default:
696 return NULL;
697 }
698 }
699
700 /**
701 * value_to_py_structured_type:
702 * @value: the GValue object.
703 * @copy_boxed: true if boxed values should be copied.
704 *
705 * This function creates/returns a Python wrapper object that
706 * represents the GValue passed as an argument.
707 *
708 * Returns: a PyObject representing the value or NULL and sets an error;
709 */
710 static PyObject *
711 value_to_py_structured_type (const GValue *value, GType fundamental,
712 gboolean copy_boxed)
713 {
714 const gchar *type_name;
715
716 switch (fundamental) {
717 case G_TYPE_INTERFACE:
718 if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_OBJECT))
719 return pygobject_new (g_value_get_object (value));
720 else
721 break;
722
723 case G_TYPE_POINTER:
724 if (G_VALUE_HOLDS_GTYPE (value))
725 return pyg_type_wrapper_new (g_value_get_gtype (value));
726 else
727 return pyg_pointer_new (G_VALUE_TYPE (value),
728 g_value_get_pointer (value));
729 case G_TYPE_BOXED: {
730 PyGTypeMarshal *bm;
731 gboolean holds_value_array;
732
733 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
734 holds_value_array = G_VALUE_HOLDS (value, PYGI_TYPE_VALUE_ARRAY);
735 G_GNUC_END_IGNORE_DEPRECATIONS
736
737 if (G_VALUE_HOLDS (value, PY_TYPE_OBJECT)) {
738 PyObject *ret = (PyObject *)g_value_dup_boxed (value);
739 if (ret == NULL) {
740 Py_RETURN_NONE;
741 }
742 return ret;
743 } else if (G_VALUE_HOLDS (value, G_TYPE_VALUE)) {
744 GValue *n_value = g_value_get_boxed (value);
745 return pyg_value_to_pyobject (n_value, copy_boxed);
746 } else if (holds_value_array) {
747 GValueArray *array = (GValueArray *)g_value_get_boxed (value);
748 Py_ssize_t n_values = array ? array->n_values : 0;
749 PyObject *ret = PyList_New (n_values);
750 int i;
751 for (i = 0; i < n_values; ++i)
752 PyList_SET_ITEM (
753 ret, i,
754 pyg_value_to_pyobject (array->values + i, copy_boxed));
755 return ret;
756 } else if (G_VALUE_HOLDS (value, G_TYPE_GSTRING)) {
757 GString *string = (GString *)g_value_get_boxed (value);
758 PyObject *ret =
759 PyUnicode_FromStringAndSize (string->str, string->len);
760 return ret;
761 }
762 bm = pyg_type_lookup (G_VALUE_TYPE (value));
763 if (bm) {
764 return bm->fromvalue (value);
765 } else {
766 if (copy_boxed)
767 return pygi_gboxed_new (G_VALUE_TYPE (value),
768 g_value_get_boxed (value), TRUE, TRUE);
769 else
770 return pygi_gboxed_new (G_VALUE_TYPE (value),
771 g_value_get_boxed (value), FALSE,
772 FALSE);
773 }
774 }
775 case G_TYPE_OBJECT:
776 return pygobject_new (g_value_get_object (value));
777 case G_TYPE_VARIANT: {
778 GVariant *v = g_value_get_variant (value);
779 if (v == NULL) {
780 Py_RETURN_NONE;
781 }
782 return pygi_struct_new_from_g_type (G_TYPE_VARIANT, g_variant_ref (v),
783 FALSE);
784 }
785 case G_TYPE_INVALID:
786 PyErr_SetString (PyExc_TypeError, "Invalid type");
787 return NULL;
788 default: {
789 PyGTypeMarshal *bm;
790 GIRepository *repository;
791 GIBaseInfo *info;
792 GIObjectInfoGetValueFunction get_value_func = NULL;
793
794 if ((bm = pyg_type_lookup (G_VALUE_TYPE (value))))
795 return bm->fromvalue (value);
796
797 repository = pygi_repository_get_default ();
798 info = gi_repository_find_by_gtype (repository, fundamental);
799
800 if (info == NULL) break;
801
802 if (GI_IS_OBJECT_INFO (info))
803 get_value_func = gi_object_info_get_get_value_function_pointer (
804 (GIObjectInfo *)info);
805
806 gi_base_info_unref (info);
807
808 if (get_value_func)
809 return pygi_fundamental_new (get_value_func (value));
810
811 break;
812 }
813 }
814
815 type_name = g_type_name (G_VALUE_TYPE (value));
816 if (type_name == NULL) {
817 type_name = "(null)";
818 }
819 PyErr_Format (PyExc_TypeError, "unknown type %s", type_name);
820 return NULL;
821 }
822
823
824 /**
825 * pyg_value_to_pyobject:
826 * @value: the GValue object.
827 * @copy_boxed: true if boxed values should be copied.
828 *
829 * This function creates/returns a Python wrapper object that
830 * represents the GValue passed as an argument.
831 *
832 * Returns: a PyObject representing the value or %NULL and sets an exception.
833 */
834 PyObject *
835 pyg_value_to_pyobject (const GValue *value, gboolean copy_boxed)
836 {
837 PyObject *pyobj;
838 GType fundamental = G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value));
839
840 /* HACK: special case char and uchar to return PyBytes intstead of integers
841 * in the general case. Property access will skip this by calling
842 * pygi_value_to_py_basic_type() directly.
843 * See: https://bugzilla.gnome.org/show_bug.cgi?id=733893 */
844 if (fundamental == G_TYPE_CHAR) {
845 gint8 val = g_value_get_schar (value);
846 return PyUnicode_FromStringAndSize ((char *)&val, 1);
847 } else if (fundamental == G_TYPE_UCHAR) {
848 guint8 val = g_value_get_uchar (value);
849 return PyBytes_FromStringAndSize ((char *)&val, 1);
850 }
851
852 pyobj = pygi_value_to_py_basic_type (value, fundamental);
853 if (pyobj != NULL || PyErr_Occurred ()) return pyobj;
854
855 pyobj = value_to_py_structured_type (value, fundamental, copy_boxed);
856 return pyobj;
857 }
858
859
860 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
861 int
862 pyg_param_gvalue_from_pyobject (GValue *value, PyObject *py_obj,
863 const GParamSpec *pspec)
864 {
865 if (G_IS_PARAM_SPEC_UNICHAR (pspec)) {
866 gunichar u;
867
868 if (!pygi_gunichar_from_py (py_obj, &u)) {
869 PyErr_Clear ();
870 return -1;
871 }
872 g_value_set_uint (value, u);
873 return 0;
874 } else if (PYGI_IS_PARAM_SPEC_VALUE_ARRAY (pspec)) {
875 return pyg_value_array_from_pyobject (
876 value, py_obj, PYGI_PARAM_SPEC_VALUE_ARRAY (pspec));
877 } else {
878 return pyg_value_from_pyobject (value, py_obj);
879 }
880 }
881
882 G_GNUC_END_IGNORE_DEPRECATIONS
883
884 PyObject *
885 pyg__gvalue_get (PyObject *module, PyObject *pygvalue)
886 {
887 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
888 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
889 return NULL;
890 }
891
892 return pyg_value_to_pyobject (pyg_boxed_get (pygvalue, GValue),
893 /*copy_boxed=*/TRUE);
894 }
895
896 PyObject *
897 pyg__gvalue_get_type (PyObject *module, PyObject *pygvalue)
898 {
899 GValue *value;
900 GType type;
901
902 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
903 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
904 return NULL;
905 }
906
907 value = pyg_boxed_get (pygvalue, GValue);
908 type = G_VALUE_TYPE (value);
909 return pyg_type_wrapper_new (type);
910 }
911
912 PyObject *
913 pyg__gvalue_set (PyObject *module, PyObject *args)
914 {
915 PyObject *pygvalue;
916 PyObject *pyobject;
917
918 if (!PyArg_ParseTuple (args, "OO:_gi._gvalue_set", &pygvalue, &pyobject))
919 return NULL;
920
921 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
922 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
923 return NULL;
924 }
925
926 if (pyg_value_from_pyobject_with_error (pyg_boxed_get (pygvalue, GValue),
927 pyobject)
928 == -1)
929 return NULL;
930
931 Py_RETURN_NONE;
932 }