1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5 * Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
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.
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.
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/>.
22 #include <pyglib-python-compat.h>
25 #include "pygi-basictype.h"
26 #include "pygi-argument.h"
32 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
33 #define NAN (*(const float *) __nan)
37 #define INFINITY HUGE_VAL
44 * From Python Marshaling
48 _pygi_marshal_from_py_void (PyGIInvokeState *state,
49 PyGICallableCache *callable_cache,
50 PyGIArgCache *arg_cache,
53 gpointer *cleanup_data)
55 g_warn_if_fail (arg_cache->transfer == GI_TRANSFER_NOTHING);
57 if (py_arg == Py_None) {
58 arg->v_pointer = NULL;
59 } else if (PYGLIB_CPointer_Check(py_arg)) {
60 arg->v_pointer = PYGLIB_CPointer_GetPointer (py_arg, NULL);
61 } else if (PYGLIB_PyLong_Check(py_arg) || PyLong_Check(py_arg)) {
62 arg->v_pointer = PyLong_AsVoidPtr (py_arg);
64 PyErr_SetString(PyExc_ValueError,
65 "Pointer arguments are restricted to integers, capsules, and None. "
66 "See: https://bugzilla.gnome.org/show_bug.cgi?id=683599");
70 *cleanup_data = arg->v_pointer;
75 check_valid_double (double x, double min, double max)
79 if ((x < min || x > max) && x != INFINITY && x != -INFINITY && x != NAN) {
83 /* we need this as PyErr_Format() does not support float types */
84 snprintf (buf, sizeof (buf), "%g not in range %g to %g", x, min, max);
85 PyErr_SetString (PyExc_OverflowError, buf);
92 _pygi_py_arg_to_double (PyObject *py_arg, double *double_)
96 if (!PyNumber_Check (py_arg)) {
97 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
98 py_arg->ob_type->tp_name);
102 py_float = PyNumber_Float (py_arg);
106 *double_ = PyFloat_AsDouble (py_float);
107 Py_DECREF (py_float);
114 _pygi_marshal_from_py_float (PyObject *py_arg,
119 if (!_pygi_py_arg_to_double (py_arg, &double_))
122 if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXFLOAT, G_MAXFLOAT))
125 arg->v_float = double_;
130 _pygi_marshal_from_py_double (PyObject *py_arg,
135 if (!_pygi_py_arg_to_double (py_arg, &double_))
138 if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXDOUBLE, G_MAXDOUBLE))
141 arg->v_double = double_;
146 _pygi_marshal_from_py_unichar (PyObject *py_arg,
152 if (py_arg == Py_None) {
157 if (PyUnicode_Check (py_arg)) {
160 size = PyUnicode_GET_SIZE (py_arg);
161 py_bytes = PyUnicode_AsUTF8String (py_arg);
165 string_ = g_strdup(PYGLIB_PyBytes_AsString (py_bytes));
166 Py_DECREF (py_bytes);
168 #if PY_VERSION_HEX < 0x03000000
169 } else if (PyString_Check (py_arg)) {
170 PyObject *pyuni = PyUnicode_FromEncodedObject (py_arg, "UTF-8", "strict");
174 size = PyUnicode_GET_SIZE (pyuni);
175 string_ = g_strdup (PyString_AsString(py_arg));
179 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
180 py_arg->ob_type->tp_name);
185 PyErr_Format (PyExc_TypeError, "Must be a one character string, not %lld characters",
191 arg->v_uint32 = g_utf8_get_char (string_);
198 _pygi_marshal_from_py_gtype (PyObject *py_arg,
201 long type_ = pyg_type_from_object (py_arg);
204 PyErr_Format (PyExc_TypeError, "Must be gobject.GType, not %s",
205 py_arg->ob_type->tp_name);
214 _pygi_marshal_from_py_utf8 (PyObject *py_arg,
216 gpointer *cleanup_data)
220 if (py_arg == Py_None) {
221 arg->v_pointer = NULL;
225 if (PyUnicode_Check (py_arg)) {
226 PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg);
230 string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
231 Py_DECREF (pystr_obj);
233 #if PY_VERSION_HEX < 0x03000000
234 else if (PyString_Check (py_arg)) {
235 string_ = g_strdup (PyString_AsString (py_arg));
239 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
240 py_arg->ob_type->tp_name);
244 arg->v_string = string_;
245 *cleanup_data = arg->v_string;
249 G_GNUC_UNUSED static gboolean
250 _pygi_marshal_from_py_filename_unix (PyObject *py_arg,
252 gpointer *cleanup_data)
256 if (py_arg == Py_None) {
257 arg->v_pointer = NULL;
261 if (PYGLIB_PyBytes_Check (py_arg)) {
264 if (PYGLIB_PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
267 filename = g_strdup (buffer);
268 } else if (PyUnicode_Check (py_arg)) {
272 #if PY_VERSION_HEX < 0x03000000
273 bytes = PyUnicode_AsEncodedString (py_arg, Py_FileSystemDefaultEncoding,
276 bytes = PyUnicode_EncodeFSDefault (py_arg);
282 if (PYGLIB_PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
287 filename = g_strdup (buffer);
290 PyErr_Format (PyExc_TypeError, "Must be bytes, not %s",
291 py_arg->ob_type->tp_name);
295 arg->v_string = filename;
296 *cleanup_data = filename;
300 G_GNUC_UNUSED static gboolean
301 _pygi_marshal_from_py_filename_win32 (PyObject *py_arg,
303 gpointer *cleanup_data)
307 if (py_arg == Py_None) {
308 arg->v_pointer = NULL;
312 #if PY_VERSION_HEX < 0x03000000
313 if (PYGLIB_PyBytes_Check (py_arg)) {
316 if (PYGLIB_PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
319 filename = g_strdup (buffer);
320 } else if (PyUnicode_Check (py_arg)) {
324 bytes = PyUnicode_AsUTF8String (py_arg);
328 if (PYGLIB_PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
333 filename = g_strdup (buffer);
336 PyErr_Format (PyExc_TypeError, "Must be unicode, not %s",
337 py_arg->ob_type->tp_name);
341 if (PYGLIB_PyBytes_Check (py_arg)) {
346 if (PYGLIB_PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
349 uni_arg = PyUnicode_DecodeFSDefault (buffer);
352 result = _pygi_marshal_from_py_filename_win32 (uni_arg, arg, cleanup_data);
355 } else if (PyUnicode_Check (py_arg)) {
356 PyObject *bytes, *temp_uni;
359 /* The roundtrip merges lone surrogates, so we get the same output as
360 * with Py 2. Requires 3.4+ because of https://bugs.python.org/issue27971
361 * Separated lone surrogates can occur when concatenating two paths.
363 bytes = PyUnicode_AsEncodedString (py_arg, "utf-16-le", "surrogatepass");
366 temp_uni = PyUnicode_FromEncodedObject (bytes, "utf-16-le", "surrogatepass");
370 /* glib uses utf-8, so encode to that and allow surrogates so we can
371 * represent all possible path values
373 bytes = PyUnicode_AsEncodedString (temp_uni, "utf-8", "surrogatepass");
374 Py_DECREF (temp_uni);
378 if (PYGLIB_PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
383 filename = g_strdup (buffer);
386 PyErr_Format (PyExc_TypeError, "Must be str, not %s",
387 py_arg->ob_type->tp_name);
392 arg->v_string = filename;
393 *cleanup_data = filename;
398 _pygi_marshal_from_py_filename (PyObject *py_arg,
400 gpointer *cleanup_data)
403 return _pygi_marshal_from_py_filename_win32 (py_arg, arg, cleanup_data);
405 return _pygi_marshal_from_py_filename_unix (py_arg, arg, cleanup_data);
410 _pygi_marshal_from_py_long (PyObject *object, /* in */
411 GIArgument *arg, /* out */
417 if (!PyNumber_Check (object)) {
418 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
419 object->ob_type->tp_name);
423 #if PY_MAJOR_VERSION < 3
425 PyObject *tmp = PyNumber_Int (object);
427 number = PyNumber_Long (tmp);
430 number = PyNumber_Long (object);
434 number = PyNumber_Long (object);
437 if (number == NULL) {
438 PyErr_SetString (PyExc_TypeError, "expected int argument");
443 case GI_TYPE_TAG_INT8:
445 long long_value = PyLong_AsLong (number);
446 if (PyErr_Occurred()) {
448 } else if (long_value < G_MININT8 || long_value > G_MAXINT8) {
449 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
450 long_value, (long)G_MININT8, (long)G_MAXINT8);
452 arg->v_int8 = long_value;
457 case GI_TYPE_TAG_UINT8:
459 long long_value = PyLong_AsLong (number);
460 if (PyErr_Occurred()) {
462 } else if (long_value < 0 || long_value > G_MAXUINT8) {
463 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
464 long_value, (long)0, (long)G_MAXUINT8);
466 arg->v_uint8 = long_value;
471 case GI_TYPE_TAG_INT16:
473 long long_value = PyLong_AsLong (number);
474 if (PyErr_Occurred()) {
476 } else if (long_value < G_MININT16 || long_value > G_MAXINT16) {
477 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
478 long_value, (long)G_MININT16, (long)G_MAXINT16);
480 arg->v_int16 = long_value;
485 case GI_TYPE_TAG_UINT16:
487 long long_value = PyLong_AsLong (number);
488 if (PyErr_Occurred()) {
490 } else if (long_value < 0 || long_value > G_MAXUINT16) {
491 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
492 long_value, (long)0, (long)G_MAXUINT16);
494 arg->v_uint16 = long_value;
499 case GI_TYPE_TAG_INT32:
501 long long_value = PyLong_AsLong (number);
502 if (PyErr_Occurred()) {
504 } else if (long_value < G_MININT32 || long_value > G_MAXINT32) {
505 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
506 long_value, (long)G_MININT32, (long)G_MAXINT32);
508 arg->v_int32 = long_value;
513 case GI_TYPE_TAG_UINT32:
515 PY_LONG_LONG long_value = PyLong_AsLongLong (number);
516 if (PyErr_Occurred()) {
518 } else if (long_value < 0 || long_value > G_MAXUINT32) {
519 PyErr_Format (PyExc_OverflowError, "%lld not in range %ld to %lu",
520 long_value, (long)0, (unsigned long)G_MAXUINT32);
522 arg->v_uint32 = long_value;
527 case GI_TYPE_TAG_INT64:
529 /* Rely on Python overflow error and convert to ValueError for 64 bit values */
530 arg->v_int64 = PyLong_AsLongLong (number);
534 case GI_TYPE_TAG_UINT64:
536 /* Rely on Python overflow error and convert to ValueError for 64 bit values */
537 arg->v_uint64 = PyLong_AsUnsignedLongLong (number);
542 g_assert_not_reached ();
547 if (PyErr_Occurred())
553 _pygi_marshal_from_py_basic_type (PyObject *object, /* in */
554 GIArgument *arg, /* out */
557 gpointer *cleanup_data /* out */)
560 case GI_TYPE_TAG_VOID:
561 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
562 if (object == Py_None) {
563 arg->v_pointer = NULL;
564 } else if (!PYGLIB_PyLong_Check(object) && !PyLong_Check(object)) {
565 PyErr_SetString(PyExc_TypeError,
566 "Pointer assignment is restricted to integer values. "
567 "See: https://bugzilla.gnome.org/show_bug.cgi?id=683599");
569 arg->v_pointer = PyLong_AsVoidPtr (object);
570 *cleanup_data = arg->v_pointer;
573 case GI_TYPE_TAG_INT8:
574 case GI_TYPE_TAG_UINT8:
575 if (PYGLIB_PyBytes_Check (object)) {
576 if (PYGLIB_PyBytes_Size (object) != 1) {
577 PyErr_Format (PyExc_TypeError, "Must be a single character");
580 if (type_tag == GI_TYPE_TAG_INT8) {
581 arg->v_int8 = (gint8)(PYGLIB_PyBytes_AsString (object)[0]);
583 arg->v_uint8 = (guint8)(PYGLIB_PyBytes_AsString (object)[0]);
586 return _pygi_marshal_from_py_long (object, arg, type_tag, transfer);
589 case GI_TYPE_TAG_INT16:
590 case GI_TYPE_TAG_UINT16:
591 case GI_TYPE_TAG_INT32:
592 case GI_TYPE_TAG_UINT32:
593 case GI_TYPE_TAG_INT64:
594 case GI_TYPE_TAG_UINT64:
595 return _pygi_marshal_from_py_long (object, arg, type_tag, transfer);
597 case GI_TYPE_TAG_BOOLEAN:
598 arg->v_boolean = PyObject_IsTrue (object);
601 case GI_TYPE_TAG_FLOAT:
602 return _pygi_marshal_from_py_float (object, arg);
604 case GI_TYPE_TAG_DOUBLE:
605 return _pygi_marshal_from_py_double (object, arg);
607 case GI_TYPE_TAG_GTYPE:
608 return _pygi_marshal_from_py_gtype (object, arg);
610 case GI_TYPE_TAG_UNICHAR:
611 return _pygi_marshal_from_py_unichar (object, arg);
613 case GI_TYPE_TAG_UTF8:
614 return _pygi_marshal_from_py_utf8 (object, arg, cleanup_data);
616 case GI_TYPE_TAG_FILENAME:
617 return _pygi_marshal_from_py_filename (object, arg, cleanup_data);
623 if (PyErr_Occurred())
630 _pygi_marshal_from_py_basic_type_cache_adapter (PyGIInvokeState *state,
631 PyGICallableCache *callable_cache,
632 PyGIArgCache *arg_cache,
635 gpointer *cleanup_data)
637 return _pygi_marshal_from_py_basic_type (py_arg,
645 _pygi_marshal_cleanup_from_py_utf8 (PyGIInvokeState *state,
646 PyGIArgCache *arg_cache,
649 gboolean was_processed)
651 /* We strdup strings so free unless ownership is transferred to C. */
652 if (was_processed && arg_cache->transfer == GI_TRANSFER_NOTHING)
657 _arg_cache_from_py_void_setup (PyGIArgCache *arg_cache)
659 arg_cache->from_py_marshaller = _pygi_marshal_from_py_void;
664 _arg_cache_from_py_basic_type_setup (PyGIArgCache *arg_cache)
666 arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
670 _arg_cache_from_py_utf8_setup (PyGIArgCache *arg_cache,
673 arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
674 arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_utf8;
679 * To Python Marshaling
684 _pygi_marshal_to_py_void (PyGIInvokeState *state,
685 PyGICallableCache *callable_cache,
686 PyGIArgCache *arg_cache,
689 if (arg_cache->is_pointer) {
690 return PyLong_FromVoidPtr (arg->v_pointer);
696 _pygi_marshal_to_py_unichar (GIArgument *arg)
698 PyObject *py_obj = NULL;
700 /* Preserve the bidirectional mapping between 0 and "" */
701 if (arg->v_uint32 == 0) {
702 py_obj = PYGLIB_PyUnicode_FromString ("");
703 } else if (g_unichar_validate (arg->v_uint32)) {
707 bytes = g_unichar_to_utf8 (arg->v_uint32, utf8);
708 py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes);
710 /* TODO: Convert the error to an exception. */
711 PyErr_Format (PyExc_TypeError,
712 "Invalid unicode codepoint %" G_GUINT32_FORMAT,
720 _pygi_marshal_to_py_utf8 (GIArgument *arg)
722 PyObject *py_obj = NULL;
723 if (arg->v_string == NULL) {
727 py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
732 _pygi_marshal_to_py_filename (GIArgument *arg)
736 if (arg->v_string == NULL) {
740 #if PY_VERSION_HEX < 0x03000000
741 /* On PY2 we return str as is */
742 py_obj = PyString_FromString (arg->v_string);
745 py_obj = PyUnicode_DecodeUTF8 (arg->v_string, strlen(arg->v_string),
748 py_obj = PyUnicode_DecodeFSDefault (arg->v_string);
757 * _pygi_marshal_to_py_basic_type:
758 * @arg: The argument to convert to an object.
759 * @type_tag: Type tag for @arg
760 * @transfer: Transfer annotation
762 * Convert the given argument to a Python object. This function
763 * is restricted to simple types that only require the GITypeTag
764 * and GITransfer. For a more complete conversion routine, use:
765 * _pygi_argument_to_object.
767 * Returns: A PyObject representing @arg or NULL if it cannot convert
771 _pygi_marshal_to_py_basic_type (GIArgument *arg,
776 case GI_TYPE_TAG_BOOLEAN:
777 return PyBool_FromLong (arg->v_boolean);
779 case GI_TYPE_TAG_INT8:
780 return PYGLIB_PyLong_FromLong (arg->v_int8);
782 case GI_TYPE_TAG_UINT8:
783 return PYGLIB_PyLong_FromLong (arg->v_uint8);
785 case GI_TYPE_TAG_INT16:
786 return PYGLIB_PyLong_FromLong (arg->v_int16);
788 case GI_TYPE_TAG_UINT16:
789 return PYGLIB_PyLong_FromLong (arg->v_uint16);
791 case GI_TYPE_TAG_INT32:
792 return PYGLIB_PyLong_FromLong (arg->v_int32);
794 case GI_TYPE_TAG_UINT32:
795 return PyLong_FromLongLong (arg->v_uint32);
797 case GI_TYPE_TAG_INT64:
798 return PyLong_FromLongLong (arg->v_int64);
800 case GI_TYPE_TAG_UINT64:
801 return PyLong_FromUnsignedLongLong (arg->v_uint64);
803 case GI_TYPE_TAG_FLOAT:
804 return PyFloat_FromDouble (arg->v_float);
806 case GI_TYPE_TAG_DOUBLE:
807 return PyFloat_FromDouble (arg->v_double);
809 case GI_TYPE_TAG_GTYPE:
810 return pyg_type_wrapper_new ( (GType) arg->v_long);
812 case GI_TYPE_TAG_UNICHAR:
813 return _pygi_marshal_to_py_unichar (arg);
815 case GI_TYPE_TAG_UTF8:
816 return _pygi_marshal_to_py_utf8 (arg);
818 case GI_TYPE_TAG_FILENAME:
819 return _pygi_marshal_to_py_filename (arg);
828 _pygi_marshal_to_py_basic_type_cache_adapter (PyGIInvokeState *state,
829 PyGICallableCache *callable_cache,
830 PyGIArgCache *arg_cache,
833 return _pygi_marshal_to_py_basic_type (arg,
835 arg_cache->transfer);
839 _pygi_marshal_cleanup_to_py_utf8 (PyGIInvokeState *state,
840 PyGIArgCache *arg_cache,
843 gboolean was_processed)
845 /* Python copies the string so we need to free it
846 if the interface is transfering ownership,
847 whether or not it has been processed yet */
848 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
855 _arg_cache_to_py_basic_type_setup (PyGIArgCache *arg_cache)
857 arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type_cache_adapter;
861 _arg_cache_to_py_void_setup (PyGIArgCache *arg_cache)
863 arg_cache->to_py_marshaller = _pygi_marshal_to_py_void;
867 _arg_cache_to_py_utf8_setup (PyGIArgCache *arg_cache,
870 arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type_cache_adapter;
871 arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_utf8;
875 * Basic Type Interface
879 pygi_arg_basic_type_setup_from_info (PyGIArgCache *arg_cache,
880 GITypeInfo *type_info,
883 PyGIDirection direction)
885 GITypeTag type_tag = g_type_info_get_tag (type_info);
887 if (!pygi_arg_base_setup (arg_cache, type_info, arg_info, transfer, direction))
891 case GI_TYPE_TAG_VOID:
892 if (direction & PYGI_DIRECTION_FROM_PYTHON)
893 _arg_cache_from_py_void_setup (arg_cache);
895 if (direction & PYGI_DIRECTION_TO_PYTHON)
896 _arg_cache_to_py_void_setup (arg_cache);
899 case GI_TYPE_TAG_BOOLEAN:
900 case GI_TYPE_TAG_INT8:
901 case GI_TYPE_TAG_UINT8:
902 case GI_TYPE_TAG_INT16:
903 case GI_TYPE_TAG_UINT16:
904 case GI_TYPE_TAG_INT32:
905 case GI_TYPE_TAG_UINT32:
906 case GI_TYPE_TAG_INT64:
907 case GI_TYPE_TAG_UINT64:
908 case GI_TYPE_TAG_FLOAT:
909 case GI_TYPE_TAG_DOUBLE:
910 case GI_TYPE_TAG_UNICHAR:
911 case GI_TYPE_TAG_GTYPE:
912 if (direction & PYGI_DIRECTION_FROM_PYTHON)
913 _arg_cache_from_py_basic_type_setup (arg_cache);
915 if (direction & PYGI_DIRECTION_TO_PYTHON)
916 _arg_cache_to_py_basic_type_setup (arg_cache);
919 case GI_TYPE_TAG_UTF8:
920 case GI_TYPE_TAG_FILENAME:
921 if (direction & PYGI_DIRECTION_FROM_PYTHON)
922 _arg_cache_from_py_utf8_setup (arg_cache, transfer);
924 if (direction & PYGI_DIRECTION_TO_PYTHON)
925 _arg_cache_to_py_utf8_setup (arg_cache, transfer);
929 g_assert_not_reached ();
936 pygi_arg_basic_type_new_from_info (GITypeInfo *type_info,
939 PyGIDirection direction)
941 gboolean res = FALSE;
942 PyGIArgCache *arg_cache = pygi_arg_cache_alloc ();
943 if (arg_cache == NULL)
946 res = pygi_arg_basic_type_setup_from_info (arg_cache,
954 pygi_arg_cache_free (arg_cache);