Debian Codegraph @

gi/pygi-basictype.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2011 John (J5) Palmieri <[email protected]>
5 * Copyright (C) 2014 Simon Feltman <[email protected]>
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
23 #include "pygi-argument.h"
24 #include "pygi-basictype.h"
25 #include "pygi-util.h"
26
27 static gboolean pygi_gint8_from_py (PyObject *object, gint8 *result);
28 static gboolean pygi_guint8_from_py (PyObject *object, guint8 *result);
29
30 #if defined(G_OS_WIN32)
31 #include <float.h>
32 static gboolean
33 pygi_isfinite (gdouble value)
34 {
35 return _finite (value);
36 }
37 #else
38 #include <math.h>
39 static gboolean
40 pygi_isfinite (gdouble value)
41 {
42 return isfinite (value);
43 }
44 #endif
45
46 gboolean
47 pygi_gpointer_from_py (PyObject *py_arg, gpointer *result)
48 {
49 void *temp;
50
51 if (Py_IsNone (py_arg)) {
52 *result = NULL;
53 return TRUE;
54 } else if (PyCapsule_CheckExact (py_arg)) {
55 temp = PyCapsule_GetPointer (py_arg, NULL);
56 if (temp == NULL) return FALSE;
57 *result = temp;
58 return TRUE;
59 } else if (PyLong_Check (py_arg)) {
60 temp = PyLong_AsVoidPtr (py_arg);
61 if (PyErr_Occurred ()) return FALSE;
62 *result = temp;
63 return TRUE;
64 } else {
65 PyErr_SetString (
66 PyExc_ValueError,
67 "Pointer arguments are restricted to integers, capsules, and "
68 "None. "
69 "See: https://bugzilla.gnome.org/show_bug.cgi?id=683599");
70 return FALSE;
71 }
72 }
73
74 PyObject *
75 pygi_gsize_to_py (gsize value)
76 {
77 return PyLong_FromSize_t (value);
78 }
79
80 PyObject *
81 pygi_gssize_to_py (gssize value)
82 {
83 return PyLong_FromSsize_t (value);
84 }
85
86 static PyObject *
87 base_float_checks (PyObject *object)
88 {
89 if (!PyNumber_Check (object)) {
90 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
91 Py_TYPE (object)->tp_name);
92 return NULL;
93 }
94
95 return PyNumber_Float (object);
96 }
97
98 gboolean
99 pygi_gdouble_from_py (PyObject *py_arg, gdouble *result)
100 {
101 PyObject *py_float;
102 gdouble temp;
103
104 py_float = base_float_checks (py_arg);
105 if (py_float == NULL) return FALSE;
106
107 temp = PyFloat_AsDouble (py_float);
108 Py_DECREF (py_float);
109
110 if (PyErr_Occurred ()) return FALSE;
111
112 *result = temp;
113
114 return TRUE;
115 }
116
117 PyObject *
118 pygi_gdouble_to_py (gdouble value)
119 {
120 return PyFloat_FromDouble (value);
121 }
122
123 gboolean
124 pygi_gfloat_from_py (PyObject *py_arg, gfloat *result)
125 {
126 gdouble double_;
127 PyObject *py_float;
128
129 py_float = base_float_checks (py_arg);
130 if (py_float == NULL) return FALSE;
131
132 double_ = PyFloat_AsDouble (py_float);
133 if (PyErr_Occurred ()) {
134 Py_DECREF (py_float);
135 return FALSE;
136 }
137
138 if (pygi_isfinite (double_)
139 && (double_ < -G_MAXFLOAT || double_ > G_MAXFLOAT)) {
140 PyObject *min, *max;
141
142 min = pygi_gfloat_to_py (-G_MAXFLOAT);
143 max = pygi_gfloat_to_py (G_MAXFLOAT);
144 PyErr_Format (PyExc_OverflowError, "%S not in range %S to %S",
145 py_float, min, max);
146 Py_DECREF (min);
147 Py_DECREF (max);
148 Py_DECREF (py_float);
149 return FALSE;
150 }
151
152 Py_DECREF (py_float);
153 *result = (gfloat)double_;
154
155 return TRUE;
156 }
157
158 PyObject *
159 pygi_gfloat_to_py (gfloat value)
160 {
161 return PyFloat_FromDouble (value);
162 }
163
164 gboolean
165 pygi_gunichar_from_py (PyObject *py_arg, gunichar *result)
166 {
167 Py_ssize_t size;
168 gchar *string_;
169
170 if (Py_IsNone (py_arg)) {
171 *result = 0;
172 return FALSE;
173 }
174
175 if (PyUnicode_Check (py_arg)) {
176 PyObject *py_bytes;
177
178 size = PyUnicode_GET_LENGTH (py_arg);
179 py_bytes = PyUnicode_AsUTF8String (py_arg);
180 if (!py_bytes) return FALSE;
181
182 string_ = g_strdup (PyBytes_AsString (py_bytes));
183 Py_DECREF (py_bytes);
184 } else {
185 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
186 Py_TYPE (py_arg)->tp_name);
187 return FALSE;
188 }
189
190 if (size != 1) {
191 PyErr_Format (PyExc_TypeError,
192 "Must be a one character string, not %lld characters",
193 (long long)size);
194 g_free (string_);
195 return FALSE;
196 }
197
198 *result = g_utf8_get_char (string_);
199 g_free (string_);
200
201 return TRUE;
202 }
203
204 PyObject *
205 pygi_gunichar_to_py (gunichar value)
206 {
207 PyObject *py_obj = NULL;
208
209 /* Preserve the bidirectional mapping between 0 and "" */
210 if (value == 0) {
211 py_obj = PyUnicode_FromString ("");
212 } else if (g_unichar_validate (value)) {
213 gchar utf8[6];
214 gint bytes;
215
216 bytes = g_unichar_to_utf8 (value, utf8);
217 py_obj = PyUnicode_FromStringAndSize ((char *)utf8, bytes);
218 } else {
219 /* TODO: Convert the error to an exception. */
220 PyErr_Format (PyExc_TypeError,
221 "Invalid unicode codepoint %" G_GUINT32_FORMAT, value);
222 }
223
224 return py_obj;
225 }
226
227 static gboolean
228 pygi_gtype_from_py (PyObject *py_arg, GType *type)
229 {
230 GType temp = pyg_type_from_object (py_arg);
231
232 if (temp == 0) {
233 if (!PyErr_Occurred ()) {
234 PyErr_SetString (PyExc_ValueError, "Invalid GType");
235 return FALSE;
236 }
237 PyErr_Format (PyExc_TypeError, "Must be GObject.GType, not %s",
238 Py_TYPE (py_arg)->tp_name);
239 return FALSE;
240 }
241
242 *type = temp;
243
244 return TRUE;
245 }
246
247 gboolean
248 pygi_utf8_from_py (PyObject *py_arg, gchar **result)
249 {
250 gchar *string_;
251
252 if (Py_IsNone (py_arg)) {
253 *result = NULL;
254 return TRUE;
255 }
256
257 if (PyUnicode_Check (py_arg)) {
258 PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
259 if (!pystr_obj) return FALSE;
260
261 string_ = g_strdup (PyBytes_AsString (pystr_obj));
262 Py_DECREF (pystr_obj);
263 } else {
264 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
265 Py_TYPE (py_arg)->tp_name);
266 return FALSE;
267 }
268
269 *result = string_;
270 return TRUE;
271 }
272
273 G_GNUC_UNUSED
274 static gboolean
275 filename_from_py_unix (PyObject *py_arg, gchar **result)
276 {
277 gchar *filename;
278
279 if (Py_IsNone (py_arg)) {
280 *result = NULL;
281 return TRUE;
282 }
283
284 if (PyBytes_Check (py_arg)) {
285 char *buffer;
286
287 if (PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
288 return FALSE;
289
290 filename = g_strdup (buffer);
291 } else if (PyUnicode_Check (py_arg)) {
292 PyObject *bytes;
293 char *buffer;
294
295 bytes = PyUnicode_EncodeFSDefault (py_arg);
296
297 if (!bytes) return FALSE;
298
299 if (PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
300 Py_DECREF (bytes);
301 return FALSE;
302 }
303
304 filename = g_strdup (buffer);
305 Py_DECREF (bytes);
306 } else {
307 PyErr_Format (PyExc_TypeError, "Must be bytes, not %s",
308 Py_TYPE (py_arg)->tp_name);
309 return FALSE;
310 }
311
312 *result = filename;
313 return TRUE;
314 }
315
316 G_GNUC_UNUSED
317 static gboolean
318 filename_from_py_win32 (PyObject *py_arg, gchar **result)
319 {
320 gchar *filename;
321
322 if (Py_IsNone (py_arg)) {
323 *result = NULL;
324 return TRUE;
325 }
326
327 if (PyBytes_Check (py_arg)) {
328 PyObject *uni_arg;
329 gboolean temp_result;
330 char *buffer;
331
332 if (PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
333 return FALSE;
334
335 uni_arg = PyUnicode_DecodeFSDefault (buffer);
336 if (!uni_arg) return FALSE;
337 temp_result = filename_from_py_win32 (uni_arg, result);
338 Py_DECREF (uni_arg);
339 return temp_result;
340 } else if (PyUnicode_Check (py_arg)) {
341 PyObject *bytes, *temp_uni;
342 char *buffer;
343
344 /* The roundtrip merges lone surrogates, so we get the same output as
345 * with Py 2. Requires 3.4+ because of https://bugs.python.org/issue27971
346 * Separated lone surrogates can occur when concatenating two paths.
347 */
348 bytes =
349 PyUnicode_AsEncodedString (py_arg, "utf-16-le", "surrogatepass");
350 if (!bytes) return FALSE;
351 temp_uni =
352 PyUnicode_FromEncodedObject (bytes, "utf-16-le", "surrogatepass");
353 Py_DECREF (bytes);
354 if (!temp_uni) return FALSE;
355 /* glib uses utf-8, so encode to that and allow surrogates so we can
356 * represent all possible path values
357 */
358 bytes = PyUnicode_AsEncodedString (temp_uni, "utf-8", "surrogatepass");
359 Py_DECREF (temp_uni);
360 if (!bytes) return FALSE;
361
362 if (PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
363 Py_DECREF (bytes);
364 return FALSE;
365 }
366
367 filename = g_strdup (buffer);
368 Py_DECREF (bytes);
369 } else {
370 PyErr_Format (PyExc_TypeError, "Must be str, not %s",
371 Py_TYPE (py_arg)->tp_name);
372 return FALSE;
373 }
374
375 *result = filename;
376 return TRUE;
377 }
378
379 static gboolean
380 pygi_filename_from_py (PyObject *py_arg, gchar **result)
381 {
382 #ifdef G_OS_WIN32
383 return filename_from_py_win32 (py_arg, result);
384 #else
385 return filename_from_py_unix (py_arg, result);
386 #endif
387 }
388
389 static PyObject *
390 base_number_checks (PyObject *object)
391 {
392 PyObject *number;
393
394 if (!PyNumber_Check (object)) {
395 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
396 Py_TYPE (object)->tp_name);
397 return NULL;
398 }
399
400 number = PyNumber_Long (object);
401
402 if (number == NULL) {
403 PyErr_SetString (PyExc_TypeError, "expected int argument");
404 return NULL;
405 }
406
407 return number;
408 }
409
410 gboolean
411 pygi_gboolean_from_py (PyObject *object, gboolean *result)
412 {
413 int value = PyObject_IsTrue (object);
414 if (value == -1) return FALSE;
415 *result = (gboolean)value;
416 return TRUE;
417 }
418
419 PyObject *
420 pygi_gboolean_to_py (gboolean value)
421 {
422 return PyBool_FromLong (value);
423 }
424
425 /* A super set of pygi_gint8_from_py (also handles unicode) */
426 gboolean
427 pygi_gschar_from_py (PyObject *object, gint8 *result)
428 {
429 if (PyUnicode_Check (object)) {
430 gunichar uni;
431 PyObject *temp;
432 gboolean status;
433
434 if (!pygi_gunichar_from_py (object, &uni)) return FALSE;
435
436 temp = pygi_guint32_to_py (uni);
437 status = pygi_gint8_from_py (temp, result);
438 Py_DECREF (temp);
439 return status;
440 } else {
441 /* pygi_gint8_from_py handles numbers and bytes */
442 return pygi_gint8_from_py (object, result);
443 }
444
445 return FALSE;
446 }
447
448 /* A super set of pygi_guint8_from_py (also handles unicode) */
449 gboolean
450 pygi_guchar_from_py (PyObject *object, guchar *result)
451 {
452 if (PyUnicode_Check (object)) {
453 gunichar uni;
454 PyObject *temp;
455 gboolean status;
456 gint8 codepoint;
457
458 if (!pygi_gunichar_from_py (object, &uni)) return FALSE;
459
460 temp = pygi_guint32_to_py (uni);
461 status = pygi_gint8_from_py (temp, &codepoint);
462 Py_DECREF (temp);
463 if (status) *result = (guchar)codepoint;
464 return status;
465 } else {
466 /* pygi_guint8_from_py handles numbers and bytes */
467 return pygi_guint8_from_py (object, result);
468 }
469 }
470
471 gboolean
472 pygi_gint_from_py (PyObject *object, gint *result)
473 {
474 long long_value;
475 PyObject *number;
476
477 number = base_number_checks (object);
478 if (number == NULL) return FALSE;
479
480 long_value = PyLong_AsLong (number);
481 if (PyErr_Occurred ()) {
482 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
483 Py_DECREF (number);
484 return FALSE;
485 } else if (long_value < G_MININT || long_value > G_MAXINT) {
486 goto overflow;
487 }
488
489 Py_DECREF (number);
490 *result = (gint)long_value;
491 return TRUE;
492
493 overflow:
494 PyErr_Clear ();
495 PyErr_Format (PyExc_OverflowError, "%S not in range %d to %d", number,
496 (int)G_MININT, (int)G_MAXINT);
497 Py_DECREF (number);
498 return FALSE;
499 }
500
501 PyObject *
502 pygi_gint_to_py (gint value)
503 {
504 return PyLong_FromLong (value);
505 }
506
507 gboolean
508 pygi_guint_from_py (PyObject *object, guint *result)
509 {
510 unsigned long long_value;
511 PyObject *number;
512
513 number = base_number_checks (object);
514 if (number == NULL) return FALSE;
515
516 long_value = PyLong_AsUnsignedLong (number);
517 if (PyErr_Occurred ()) {
518 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
519 Py_DECREF (number);
520 return FALSE;
521 } else if (long_value > G_MAXUINT) {
522 goto overflow;
523 }
524
525 Py_DECREF (number);
526 *result = (gint)long_value;
527 return TRUE;
528
529 overflow:
530 PyErr_Clear ();
531 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %lu", number,
532 (long)0, (unsigned long)G_MAXUINT);
533 Py_DECREF (number);
534 return FALSE;
535 }
536
537 PyObject *
538 pygi_guint_to_py (guint value)
539 {
540 #if (G_MAXUINT <= LONG_MAX)
541 return PyLong_FromLong ((long)value);
542 #else
543 if (value <= LONG_MAX) return PyLong_FromLong ((long)value);
544 return PyLong_FromUnsignedLong (value);
545 #endif
546 }
547
548 gboolean
549 pygi_glong_from_py (PyObject *object, glong *result)
550 {
551 long long_value;
552 PyObject *number;
553
554 number = base_number_checks (object);
555 if (number == NULL) return FALSE;
556
557 long_value = PyLong_AsLong (number);
558 if (long_value == -1 && PyErr_Occurred ()) {
559 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
560 Py_DECREF (number);
561 return FALSE;
562 }
563
564 Py_DECREF (number);
565 *result = (glong)long_value;
566 return TRUE;
567
568 overflow:
569 PyErr_Clear ();
570 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %ld", number,
571 (long)G_MINLONG, (long)G_MAXLONG);
572 Py_DECREF (number);
573 return FALSE;
574 }
575
576 PyObject *
577 pygi_glong_to_py (glong value)
578 {
579 return PyLong_FromLong (value);
580 }
581
582 gboolean
583 pygi_gulong_from_py (PyObject *object, gulong *result)
584 {
585 unsigned long long_value;
586 PyObject *number;
587
588 number = base_number_checks (object);
589 if (number == NULL) return FALSE;
590
591 long_value = PyLong_AsUnsignedLong (number);
592 if (PyErr_Occurred ()) {
593 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
594 Py_DECREF (number);
595 return FALSE;
596 }
597
598 Py_DECREF (number);
599 *result = (gulong)long_value;
600 return TRUE;
601
602 overflow:
603 PyErr_Clear ();
604 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %lu", number,
605 (long)0, (unsigned long)G_MAXULONG);
606 Py_DECREF (number);
607 return FALSE;
608 }
609
610 PyObject *
611 pygi_gulong_to_py (gulong value)
612 {
613 if (value <= LONG_MAX)
614 return PyLong_FromLong ((long)value);
615 else
616 return PyLong_FromUnsignedLong (value);
617 }
618
619 static gboolean
620 pygi_gint8_from_py_converted (PyObject *object, gint8 *result)
621 {
622 long long_value;
623 PyObject *number;
624
625 if (PyBytes_Check (object)) {
626 if (PyBytes_Size (object) != 1) {
627 PyErr_Format (PyExc_TypeError, "Must be a single character");
628 return FALSE;
629 }
630
631 *result = (gint8)(PyBytes_AsString (object)[0]);
632 return TRUE;
633 }
634
635 number = base_number_checks (object);
636 if (number == NULL) return FALSE;
637
638 long_value = PyLong_AsLong (number);
639 if (long_value == -1 && PyErr_Occurred ()) {
640 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
641 Py_DECREF (number);
642 return FALSE;
643 } else if (long_value < G_MININT8 || long_value > G_MAXINT8) {
644 goto overflow;
645 }
646
647 Py_DECREF (number);
648 *result = (gint8)long_value;
649 return TRUE;
650
651 overflow:
652 PyErr_Clear ();
653 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %ld", number,
654 (long)G_MININT8, (long)G_MAXINT8);
655 Py_DECREF (number);
656 return FALSE;
657 }
658
659 static gboolean
660 pygi_gint8_from_py (PyObject *object, gint8 *result)
661 {
662 /* If we pass unicode, convert it to a bytes representation
663 * and then continue. */
664 if (PyUnicode_Check (object)) {
665 PyObject *bytes_object = PyUnicode_AsUTF8String (object);
666
667 if (!bytes_object) {
668 PyErr_Format (PyExc_TypeError,
669 "Could not convert unicode string to bytes");
670 return FALSE;
671 }
672
673 gboolean ret = pygi_gint8_from_py_converted (bytes_object, result);
674 Py_DECREF (bytes_object);
675
676 return ret;
677 }
678
679 return pygi_gint8_from_py_converted (object, result);
680 }
681
682 static PyObject *
683 pygi_gint8_to_py (gint8 value)
684 {
685 return PyLong_FromLong (value);
686 }
687
688 static gboolean
689 pygi_guint8_from_py_converted (PyObject *object, guint8 *result)
690 {
691 long long_value;
692 PyObject *number;
693
694 if (PyBytes_Check (object)) {
695 if (PyBytes_Size (object) != 1) {
696 PyErr_Format (PyExc_TypeError, "Must be a single character");
697 return FALSE;
698 }
699
700 *result = (guint8)(PyBytes_AsString (object)[0]);
701 return TRUE;
702 }
703
704 number = base_number_checks (object);
705 if (number == NULL) return FALSE;
706
707 long_value = PyLong_AsLong (number);
708 if (long_value == -1 && PyErr_Occurred ()) {
709 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
710 Py_DECREF (number);
711 return FALSE;
712 } else if (long_value < 0 || long_value > G_MAXUINT8) {
713 goto overflow;
714 }
715
716 Py_DECREF (number);
717 *result = (guint8)long_value;
718 return TRUE;
719
720 overflow:
721 PyErr_Clear ();
722 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %ld", number,
723 (long)0, (long)G_MAXUINT8);
724 Py_DECREF (number);
725 return FALSE;
726 }
727
728 static gboolean
729 pygi_guint8_from_py (PyObject *object, guint8 *result)
730 {
731 /* If we pass unicode, convert it to a bytes representation
732 * and then continue */
733 if (PyUnicode_Check (object)) {
734 PyObject *bytes_object = PyUnicode_AsUTF8String (object);
735
736 if (!bytes_object) {
737 PyErr_Format (PyExc_TypeError,
738 "Could not convert unicode string to bytes");
739 return FALSE;
740 }
741
742 gboolean ret = pygi_guint8_from_py_converted (bytes_object, result);
743 Py_DECREF (bytes_object);
744
745 return ret;
746 }
747
748 return pygi_guint8_from_py_converted (object, result);
749 }
750
751 static PyObject *
752 pygi_guint8_to_py (guint8 value)
753 {
754 return PyLong_FromLong (value);
755 }
756
757 static gboolean
758 pygi_gint16_from_py (PyObject *object, gint16 *result)
759 {
760 long long_value;
761 PyObject *number;
762
763 number = base_number_checks (object);
764 if (number == NULL) return FALSE;
765
766 long_value = PyLong_AsLong (number);
767 if (long_value == -1 && PyErr_Occurred ()) {
768 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
769 Py_DECREF (number);
770 return FALSE;
771 } else if (long_value < G_MININT16 || long_value > G_MAXINT16) {
772 goto overflow;
773 }
774
775 Py_DECREF (number);
776 *result = (gint16)long_value;
777 return TRUE;
778
779 overflow:
780 PyErr_Clear ();
781 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %ld", number,
782 (long)G_MININT16, (long)G_MAXINT16);
783 Py_DECREF (number);
784 return FALSE;
785 }
786
787 static PyObject *
788 pygi_gint16_to_py (gint16 value)
789 {
790 return PyLong_FromLong (value);
791 }
792
793 static gboolean
794 pygi_guint16_from_py (PyObject *object, guint16 *result)
795 {
796 long long_value;
797 PyObject *number;
798
799 number = base_number_checks (object);
800 if (number == NULL) return FALSE;
801
802 long_value = PyLong_AsLong (number);
803 if (long_value == -1 && PyErr_Occurred ()) {
804 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
805 Py_DECREF (number);
806 return FALSE;
807 } else if (long_value < 0 || long_value > G_MAXUINT16) {
808 goto overflow;
809 }
810
811 Py_DECREF (number);
812 *result = (guint16)long_value;
813 return TRUE;
814
815 overflow:
816 PyErr_Clear ();
817 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %ld", number,
818 (long)0, (long)G_MAXUINT16);
819 Py_DECREF (number);
820 return FALSE;
821 }
822
823 static PyObject *
824 pygi_guint16_to_py (guint16 value)
825 {
826 return PyLong_FromLong (value);
827 }
828
829 static gboolean
830 pygi_gint32_from_py (PyObject *object, gint32 *result)
831 {
832 long long_value;
833 PyObject *number;
834
835 number = base_number_checks (object);
836 if (number == NULL) return FALSE;
837
838 long_value = PyLong_AsLong (number);
839 if (long_value == -1 && PyErr_Occurred ()) {
840 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
841 Py_DECREF (number);
842 return FALSE;
843 } else if (long_value < G_MININT32 || long_value > G_MAXINT32) {
844 goto overflow;
845 }
846
847 Py_DECREF (number);
848 *result = (gint32)long_value;
849 return TRUE;
850
851 overflow:
852 PyErr_Clear ();
853 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %ld", number,
854 (long)G_MININT32, (long)G_MAXINT32);
855 Py_DECREF (number);
856 return FALSE;
857 }
858
859 static PyObject *
860 pygi_gint32_to_py (gint32 value)
861 {
862 return PyLong_FromLong (value);
863 }
864
865 static gboolean
866 pygi_guint32_from_py (PyObject *object, guint32 *result)
867 {
868 long long long_value;
869 PyObject *number;
870
871 number = base_number_checks (object);
872 if (number == NULL) return FALSE;
873
874 long_value = PyLong_AsLongLong (number);
875 if (PyErr_Occurred ()) {
876 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
877 Py_DECREF (number);
878 return FALSE;
879 } else if (long_value < 0 || long_value > G_MAXUINT32) {
880 goto overflow;
881 }
882
883 Py_DECREF (number);
884 *result = (guint32)long_value;
885 return TRUE;
886
887 overflow:
888 PyErr_Clear ();
889 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %lu", number,
890 (long)0, (unsigned long)G_MAXUINT32);
891 Py_DECREF (number);
892 return FALSE;
893 }
894
895 PyObject *
896 pygi_guint32_to_py (guint32 value)
897 {
898 #if (G_MAXUINT <= LONG_MAX)
899 return PyLong_FromLong (value);
900 #else
901 if (value <= LONG_MAX)
902 return PyLong_FromLong ((long)value);
903 else
904 return PyLong_FromLongLong (value);
905 #endif
906 }
907
908 gboolean
909 pygi_gint64_from_py (PyObject *object, gint64 *result)
910 {
911 long long long_value;
912 PyObject *number, *min, *max;
913
914 number = base_number_checks (object);
915 if (number == NULL) return FALSE;
916
917 long_value = PyLong_AsLongLong (number);
918 if (PyErr_Occurred ()) {
919 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
920 Py_DECREF (number);
921 return FALSE;
922 } else if (long_value < G_MININT64 || long_value > G_MAXINT64) {
923 goto overflow;
924 }
925
926 Py_DECREF (number);
927 *result = (gint64)long_value;
928 return TRUE;
929
930 overflow:
931 PyErr_Clear ();
932 min = pygi_gint64_to_py (G_MININT64);
933 max = pygi_gint64_to_py (G_MAXINT64);
934 PyErr_Format (PyExc_OverflowError, "%S not in range %S to %S", number, min,
935 max);
936 Py_DECREF (number);
937 Py_DECREF (min);
938 Py_DECREF (max);
939 return FALSE;
940 }
941
942 PyObject *
943 pygi_gint64_to_py (gint64 value)
944 {
945 if (LONG_MIN <= value && value <= LONG_MAX)
946 return PyLong_FromLong ((long)value);
947 else
948 return PyLong_FromLongLong (value);
949 }
950
951 gboolean
952 pygi_guint64_from_py (PyObject *object, guint64 *result)
953 {
954 unsigned long long long_value;
955 PyObject *number, *max;
956
957 number = base_number_checks (object);
958 if (number == NULL) return FALSE;
959
960 long_value = PyLong_AsUnsignedLongLong (number);
961 if (PyErr_Occurred ()) {
962 if (PyErr_ExceptionMatches (PyExc_OverflowError)) goto overflow;
963 Py_DECREF (number);
964 return FALSE;
965 } else if (long_value > G_MAXUINT64) {
966 goto overflow;
967 }
968
969 Py_DECREF (number);
970 *result = (guint64)long_value;
971 return TRUE;
972
973 overflow:
974 PyErr_Clear ();
975 max = pygi_guint64_to_py (G_MAXUINT64);
976 PyErr_Format (PyExc_OverflowError, "%S not in range %ld to %S", number,
977 (long)0, max);
978 Py_DECREF (number);
979 Py_DECREF (max);
980 return FALSE;
981 }
982
983 PyObject *
984 pygi_guint64_to_py (guint64 value)
985 {
986 if (value <= LONG_MAX)
987 return PyLong_FromLong ((long)value);
988 else
989 return PyLong_FromUnsignedLongLong (value);
990 }
991
992 GIArgument
993 pygi_marshal_from_py_basic_type (PyObject *object, /* in */
994 GITypeTag type_tag, GITransfer transfer,
995 gpointer *cleanup_data /* out */)
996 {
997 GIArgument arg = PYGI_ARG_INIT;
998 gboolean converted = FALSE;
999
1000 switch (type_tag) {
1001 case GI_TYPE_TAG_VOID:
1002 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
1003 if (pygi_gpointer_from_py (object, &(arg.v_pointer))) {
1004 *cleanup_data = arg.v_pointer;
1005 converted = TRUE;
1006 }
1007 break;
1008
1009 case GI_TYPE_TAG_INT8:
1010 converted = pygi_gint8_from_py (object, &(arg.v_int8));
1011 break;
1012 case GI_TYPE_TAG_UINT8:
1013 converted = pygi_guint8_from_py (object, &(arg.v_uint8));
1014 break;
1015 case GI_TYPE_TAG_INT16:
1016 converted = pygi_gint16_from_py (object, &(arg.v_int16));
1017 break;
1018 case GI_TYPE_TAG_UINT16:
1019 converted = pygi_guint16_from_py (object, &(arg.v_uint16));
1020 break;
1021 case GI_TYPE_TAG_INT32:
1022 converted = pygi_gint32_from_py (object, &(arg.v_int32));
1023 break;
1024 case GI_TYPE_TAG_UINT32:
1025 converted = pygi_guint32_from_py (object, &(arg.v_uint32));
1026 break;
1027 case GI_TYPE_TAG_INT64:
1028 converted = pygi_gint64_from_py (object, &(arg.v_int64));
1029 break;
1030 case GI_TYPE_TAG_UINT64:
1031 converted = pygi_guint64_from_py (object, &(arg.v_uint64));
1032 break;
1033 case GI_TYPE_TAG_BOOLEAN:
1034 converted = pygi_gboolean_from_py (object, &(arg.v_boolean));
1035 break;
1036 case GI_TYPE_TAG_FLOAT:
1037 converted = pygi_gfloat_from_py (object, &(arg.v_float));
1038 break;
1039 case GI_TYPE_TAG_DOUBLE:
1040 converted = pygi_gdouble_from_py (object, &(arg.v_double));
1041 break;
1042 case GI_TYPE_TAG_GTYPE:
1043 converted = pygi_gtype_from_py (object, &(arg.v_size));
1044 break;
1045 case GI_TYPE_TAG_UNICHAR:
1046 converted = pygi_gunichar_from_py (object, &(arg.v_uint32));
1047 break;
1048 case GI_TYPE_TAG_UTF8:
1049 if (pygi_utf8_from_py (object, &(arg.v_string))) {
1050 *cleanup_data = arg.v_string;
1051 converted = TRUE;
1052 }
1053 break;
1054
1055 case GI_TYPE_TAG_FILENAME:
1056 if (pygi_filename_from_py (object, &(arg.v_string))) {
1057 *cleanup_data = arg.v_string;
1058 converted = TRUE;
1059 }
1060 break;
1061
1062 case GI_TYPE_TAG_ARRAY:
1063 case GI_TYPE_TAG_INTERFACE:
1064 case GI_TYPE_TAG_GLIST:
1065 case GI_TYPE_TAG_GSLIST:
1066 case GI_TYPE_TAG_GHASH:
1067 case GI_TYPE_TAG_ERROR:
1068 PyErr_Format (PyExc_TypeError, "Type tag %s (%d) not supported",
1069 gi_type_tag_to_string (type_tag), type_tag);
1070 break;
1071 default:
1072 g_assert_not_reached ();
1073 }
1074
1075 if (!converted && !PyErr_Occurred ()) {
1076 PyErr_Format (PyExc_TypeError,
1077 "Object %R Could not be converted type tag %s", object,
1078 gi_type_tag_to_string (type_tag));
1079 }
1080
1081 return arg;
1082 }
1083
1084 PyObject *
1085 pygi_utf8_to_py (gchar *value)
1086 {
1087 if (value == NULL) {
1088 Py_RETURN_NONE;
1089 }
1090
1091 return PyUnicode_FromString (value);
1092 }
1093
1094 PyObject *
1095 pygi_filename_to_py (gchar *value)
1096 {
1097 PyObject *py_obj;
1098
1099 if (value == NULL) {
1100 Py_RETURN_NONE;
1101 }
1102
1103 #ifdef G_OS_WIN32
1104 py_obj = PyUnicode_DecodeUTF8 (value, strlen (value), "surrogatepass");
1105 #else
1106 py_obj = PyUnicode_DecodeFSDefault (value);
1107 #endif
1108
1109 return py_obj;
1110 }
1111
1112 /**
1113 * pygi_marshal_to_py_basic_type:
1114 * @arg: The argument to convert to an object.
1115 * @type_tag: Type tag for @arg
1116 * @transfer: Transfer annotation
1117 *
1118 * Convert the given argument to a Python object. This function
1119 * is restricted to simple types that only require the GITypeTag
1120 * and GITransfer. For a more complete conversion routine, use:
1121 * pygi_argument_to_py.
1122 *
1123 * Returns: A PyObject representing @arg or NULL if it cannot convert
1124 * the argument.
1125 */
1126 PyObject *
1127 pygi_marshal_to_py_basic_type (GIArgument arg, GITypeTag type_tag,
1128 GITransfer transfer)
1129 {
1130 switch (type_tag) {
1131 case GI_TYPE_TAG_BOOLEAN:
1132 return pygi_gboolean_to_py (arg.v_boolean);
1133
1134 case GI_TYPE_TAG_INT8:
1135 return pygi_gint8_to_py (arg.v_int8);
1136
1137 case GI_TYPE_TAG_UINT8:
1138 return pygi_guint8_to_py (arg.v_uint8);
1139
1140 case GI_TYPE_TAG_INT16:
1141 return pygi_gint16_to_py (arg.v_int16);
1142
1143 case GI_TYPE_TAG_UINT16:
1144 return pygi_guint16_to_py (arg.v_uint16);
1145
1146 case GI_TYPE_TAG_INT32:
1147 return pygi_gint32_to_py (arg.v_int32);
1148
1149 case GI_TYPE_TAG_UINT32:
1150 return pygi_guint32_to_py (arg.v_uint32);
1151
1152 case GI_TYPE_TAG_INT64:
1153 return pygi_gint64_to_py (arg.v_int64);
1154
1155 case GI_TYPE_TAG_UINT64:
1156 return pygi_guint64_to_py (arg.v_uint64);
1157
1158 case GI_TYPE_TAG_FLOAT:
1159 return pygi_gfloat_to_py (arg.v_float);
1160
1161 case GI_TYPE_TAG_DOUBLE:
1162 return pygi_gdouble_to_py (arg.v_double);
1163
1164 case GI_TYPE_TAG_GTYPE:
1165 return pyg_type_wrapper_new ((GType)arg.v_size);
1166
1167 case GI_TYPE_TAG_UNICHAR:
1168 return pygi_gunichar_to_py (arg.v_uint32);
1169
1170 case GI_TYPE_TAG_UTF8:
1171 return pygi_utf8_to_py (arg.v_string);
1172
1173 case GI_TYPE_TAG_FILENAME:
1174 return pygi_filename_to_py (arg.v_string);
1175
1176 case GI_TYPE_TAG_VOID:
1177 case GI_TYPE_TAG_ARRAY:
1178 case GI_TYPE_TAG_INTERFACE:
1179 case GI_TYPE_TAG_GLIST:
1180 case GI_TYPE_TAG_GSLIST:
1181 case GI_TYPE_TAG_GHASH:
1182 case GI_TYPE_TAG_ERROR:
1183 PyErr_Format (PyExc_TypeError, "Type tag %d not supported", type_tag);
1184 return NULL;
1185 default:
1186 g_assert_not_reached ();
1187 }
1188 }