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>
24 #include "pygi-basictype.h"
25 #include "pygi-argument.h"
26 #include "pygi-private.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;
250 _pygi_marshal_from_py_filename (PyObject *py_arg,
252 gpointer *cleanup_data)
255 GError *error = NULL;
256 PyObject *tmp = NULL;
258 if (PyUnicode_Check (py_arg)) {
259 tmp = PyUnicode_AsUTF8String (py_arg);
263 string_ = PYGLIB_PyBytes_AsString (tmp);
265 #if PY_VERSION_HEX < 0x03000000
266 else if (PyString_Check (py_arg)) {
267 string_ = PyString_AsString (py_arg);
271 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
272 py_arg->ob_type->tp_name);
276 arg->v_string = g_filename_from_utf8 (string_, -1, NULL, NULL, &error);
279 if (arg->v_string == NULL) {
280 PyErr_SetString (PyExc_Exception, error->message);
281 g_error_free (error);
282 /* TODO: Convert the error to an exception. */
286 *cleanup_data = arg->v_string;
291 _pygi_marshal_from_py_long (PyObject *object, /* in */
292 GIArgument *arg, /* out */
298 if (!PyNumber_Check (object)) {
299 PyErr_Format (PyExc_TypeError, "Must be number, not %s",
300 object->ob_type->tp_name);
304 #if PY_MAJOR_VERSION < 3
306 PyObject *tmp = PyNumber_Int (object);
308 number = PyNumber_Long (tmp);
311 number = PyNumber_Long (object);
315 number = PyNumber_Long (object);
318 if (number == NULL) {
319 PyErr_SetString (PyExc_TypeError, "expected int argument");
324 case GI_TYPE_TAG_INT8:
326 long long_value = PyLong_AsLong (number);
327 if (PyErr_Occurred()) {
329 } else if (long_value < G_MININT8 || long_value > G_MAXINT8) {
330 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
331 long_value, (long)G_MININT8, (long)G_MAXINT8);
333 arg->v_int8 = long_value;
338 case GI_TYPE_TAG_UINT8:
340 long long_value = PyLong_AsLong (number);
341 if (PyErr_Occurred()) {
343 } else if (long_value < 0 || long_value > G_MAXUINT8) {
344 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
345 long_value, (long)0, (long)G_MAXUINT8);
347 arg->v_uint8 = long_value;
352 case GI_TYPE_TAG_INT16:
354 long long_value = PyLong_AsLong (number);
355 if (PyErr_Occurred()) {
357 } else if (long_value < G_MININT16 || long_value > G_MAXINT16) {
358 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
359 long_value, (long)G_MININT16, (long)G_MAXINT16);
361 arg->v_int16 = long_value;
366 case GI_TYPE_TAG_UINT16:
368 long long_value = PyLong_AsLong (number);
369 if (PyErr_Occurred()) {
371 } else if (long_value < 0 || long_value > G_MAXUINT16) {
372 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
373 long_value, (long)0, (long)G_MAXUINT16);
375 arg->v_uint16 = long_value;
380 case GI_TYPE_TAG_INT32:
382 long long_value = PyLong_AsLong (number);
383 if (PyErr_Occurred()) {
385 } else if (long_value < G_MININT32 || long_value > G_MAXINT32) {
386 PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
387 long_value, (long)G_MININT32, (long)G_MAXINT32);
389 arg->v_int32 = long_value;
394 case GI_TYPE_TAG_UINT32:
396 PY_LONG_LONG long_value = PyLong_AsLongLong (number);
397 if (PyErr_Occurred()) {
399 } else if (long_value < 0 || long_value > G_MAXUINT32) {
400 PyErr_Format (PyExc_OverflowError, "%lld not in range %ld to %lu",
401 long_value, (long)0, (unsigned long)G_MAXUINT32);
403 arg->v_uint32 = long_value;
408 case GI_TYPE_TAG_INT64:
410 /* Rely on Python overflow error and convert to ValueError for 64 bit values */
411 arg->v_int64 = PyLong_AsLongLong (number);
415 case GI_TYPE_TAG_UINT64:
417 /* Rely on Python overflow error and convert to ValueError for 64 bit values */
418 arg->v_uint64 = PyLong_AsUnsignedLongLong (number);
423 g_assert_not_reached ();
428 if (PyErr_Occurred())
434 _pygi_marshal_from_py_basic_type (PyObject *object, /* in */
435 GIArgument *arg, /* out */
438 gpointer *cleanup_data /* out */)
441 case GI_TYPE_TAG_VOID:
442 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
443 if (object == Py_None) {
444 arg->v_pointer = NULL;
445 } else if (!PYGLIB_PyLong_Check(object) && !PyLong_Check(object)) {
446 PyErr_SetString(PyExc_TypeError,
447 "Pointer assignment is restricted to integer values. "
448 "See: https://bugzilla.gnome.org/show_bug.cgi?id=683599");
450 arg->v_pointer = PyLong_AsVoidPtr (object);
451 *cleanup_data = arg->v_pointer;
454 case GI_TYPE_TAG_INT8:
455 case GI_TYPE_TAG_UINT8:
456 if (PYGLIB_PyBytes_Check (object)) {
457 if (PYGLIB_PyBytes_Size (object) != 1) {
458 PyErr_Format (PyExc_TypeError, "Must be a single character");
461 if (type_tag == GI_TYPE_TAG_INT8) {
462 arg->v_int8 = (gint8)(PYGLIB_PyBytes_AsString (object)[0]);
464 arg->v_uint8 = (guint8)(PYGLIB_PyBytes_AsString (object)[0]);
467 return _pygi_marshal_from_py_long (object, arg, type_tag, transfer);
470 case GI_TYPE_TAG_INT16:
471 case GI_TYPE_TAG_UINT16:
472 case GI_TYPE_TAG_INT32:
473 case GI_TYPE_TAG_UINT32:
474 case GI_TYPE_TAG_INT64:
475 case GI_TYPE_TAG_UINT64:
476 return _pygi_marshal_from_py_long (object, arg, type_tag, transfer);
478 case GI_TYPE_TAG_BOOLEAN:
479 arg->v_boolean = PyObject_IsTrue (object);
482 case GI_TYPE_TAG_FLOAT:
483 return _pygi_marshal_from_py_float (object, arg);
485 case GI_TYPE_TAG_DOUBLE:
486 return _pygi_marshal_from_py_double (object, arg);
488 case GI_TYPE_TAG_GTYPE:
489 return _pygi_marshal_from_py_gtype (object, arg);
491 case GI_TYPE_TAG_UNICHAR:
492 return _pygi_marshal_from_py_unichar (object, arg);
494 case GI_TYPE_TAG_UTF8:
495 return _pygi_marshal_from_py_utf8 (object, arg, cleanup_data);
497 case GI_TYPE_TAG_FILENAME:
498 return _pygi_marshal_from_py_filename (object, arg, cleanup_data);
504 if (PyErr_Occurred())
511 _pygi_marshal_from_py_basic_type_cache_adapter (PyGIInvokeState *state,
512 PyGICallableCache *callable_cache,
513 PyGIArgCache *arg_cache,
516 gpointer *cleanup_data)
518 return _pygi_marshal_from_py_basic_type (py_arg,
526 _pygi_marshal_cleanup_from_py_utf8 (PyGIInvokeState *state,
527 PyGIArgCache *arg_cache,
530 gboolean was_processed)
532 /* We strdup strings so free unless ownership is transferred to C. */
533 if (was_processed && arg_cache->transfer == GI_TRANSFER_NOTHING)
538 _arg_cache_from_py_void_setup (PyGIArgCache *arg_cache)
540 arg_cache->from_py_marshaller = _pygi_marshal_from_py_void;
545 _arg_cache_from_py_basic_type_setup (PyGIArgCache *arg_cache)
547 arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
551 _arg_cache_from_py_utf8_setup (PyGIArgCache *arg_cache,
554 arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
555 arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_utf8;
560 * To Python Marshaling
565 _pygi_marshal_to_py_void (PyGIInvokeState *state,
566 PyGICallableCache *callable_cache,
567 PyGIArgCache *arg_cache,
570 if (arg_cache->is_pointer) {
571 return PyLong_FromVoidPtr (arg->v_pointer);
577 _pygi_marshal_to_py_unichar (GIArgument *arg)
579 PyObject *py_obj = NULL;
581 /* Preserve the bidirectional mapping between 0 and "" */
582 if (arg->v_uint32 == 0) {
583 py_obj = PYGLIB_PyUnicode_FromString ("");
584 } else if (g_unichar_validate (arg->v_uint32)) {
588 bytes = g_unichar_to_utf8 (arg->v_uint32, utf8);
589 py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes);
591 /* TODO: Convert the error to an exception. */
592 PyErr_Format (PyExc_TypeError,
593 "Invalid unicode codepoint %" G_GUINT32_FORMAT,
601 _pygi_marshal_to_py_utf8 (GIArgument *arg)
603 PyObject *py_obj = NULL;
604 if (arg->v_string == NULL) {
608 py_obj = PYGLIB_PyUnicode_FromString (arg->v_string);
613 _pygi_marshal_to_py_filename (GIArgument *arg)
615 gchar *string = NULL;
616 PyObject *py_obj = NULL;
617 GError *error = NULL;
619 if (arg->v_string == NULL) {
623 string = g_filename_to_utf8 (arg->v_string, -1, NULL, NULL, &error);
624 if (string == NULL) {
625 PyErr_SetString (PyExc_Exception, error->message);
626 /* TODO: Convert the error to an exception. */
630 py_obj = PYGLIB_PyUnicode_FromString (string);
638 * _pygi_marshal_to_py_basic_type:
639 * @arg: The argument to convert to an object.
640 * @type_tag: Type tag for @arg
641 * @transfer: Transfer annotation
643 * Convert the given argument to a Python object. This function
644 * is restricted to simple types that only require the GITypeTag
645 * and GITransfer. For a more complete conversion routine, use:
646 * _pygi_argument_to_object.
648 * Returns: A PyObject representing @arg or NULL if it cannot convert
652 _pygi_marshal_to_py_basic_type (GIArgument *arg,
657 case GI_TYPE_TAG_BOOLEAN:
658 return PyBool_FromLong (arg->v_boolean);
660 case GI_TYPE_TAG_INT8:
661 return PYGLIB_PyLong_FromLong (arg->v_int8);
663 case GI_TYPE_TAG_UINT8:
664 return PYGLIB_PyLong_FromLong (arg->v_uint8);
666 case GI_TYPE_TAG_INT16:
667 return PYGLIB_PyLong_FromLong (arg->v_int16);
669 case GI_TYPE_TAG_UINT16:
670 return PYGLIB_PyLong_FromLong (arg->v_uint16);
672 case GI_TYPE_TAG_INT32:
673 return PYGLIB_PyLong_FromLong (arg->v_int32);
675 case GI_TYPE_TAG_UINT32:
676 return PyLong_FromLongLong (arg->v_uint32);
678 case GI_TYPE_TAG_INT64:
679 return PyLong_FromLongLong (arg->v_int64);
681 case GI_TYPE_TAG_UINT64:
682 return PyLong_FromUnsignedLongLong (arg->v_uint64);
684 case GI_TYPE_TAG_FLOAT:
685 return PyFloat_FromDouble (arg->v_float);
687 case GI_TYPE_TAG_DOUBLE:
688 return PyFloat_FromDouble (arg->v_double);
690 case GI_TYPE_TAG_GTYPE:
691 return pyg_type_wrapper_new ( (GType) arg->v_long);
693 case GI_TYPE_TAG_UNICHAR:
694 return _pygi_marshal_to_py_unichar (arg);
696 case GI_TYPE_TAG_UTF8:
697 return _pygi_marshal_to_py_utf8 (arg);
699 case GI_TYPE_TAG_FILENAME:
700 return _pygi_marshal_to_py_filename (arg);
709 _pygi_marshal_to_py_basic_type_cache_adapter (PyGIInvokeState *state,
710 PyGICallableCache *callable_cache,
711 PyGIArgCache *arg_cache,
714 return _pygi_marshal_to_py_basic_type (arg,
716 arg_cache->transfer);
720 _pygi_marshal_cleanup_to_py_utf8 (PyGIInvokeState *state,
721 PyGIArgCache *arg_cache,
724 gboolean was_processed)
726 /* Python copies the string so we need to free it
727 if the interface is transfering ownership,
728 whether or not it has been processed yet */
729 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
736 _arg_cache_to_py_basic_type_setup (PyGIArgCache *arg_cache)
738 arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type_cache_adapter;
742 _arg_cache_to_py_void_setup (PyGIArgCache *arg_cache)
744 arg_cache->to_py_marshaller = _pygi_marshal_to_py_void;
748 _arg_cache_to_py_utf8_setup (PyGIArgCache *arg_cache,
751 arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type_cache_adapter;
752 arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_utf8;
756 * Basic Type Interface
760 pygi_arg_basic_type_setup_from_info (PyGIArgCache *arg_cache,
761 GITypeInfo *type_info,
764 PyGIDirection direction)
766 GITypeTag type_tag = g_type_info_get_tag (type_info);
768 if (!pygi_arg_base_setup (arg_cache, type_info, arg_info, transfer, direction))
772 case GI_TYPE_TAG_VOID:
773 if (direction & PYGI_DIRECTION_FROM_PYTHON)
774 _arg_cache_from_py_void_setup (arg_cache);
776 if (direction & PYGI_DIRECTION_TO_PYTHON)
777 _arg_cache_to_py_void_setup (arg_cache);
780 case GI_TYPE_TAG_BOOLEAN:
781 case GI_TYPE_TAG_INT8:
782 case GI_TYPE_TAG_UINT8:
783 case GI_TYPE_TAG_INT16:
784 case GI_TYPE_TAG_UINT16:
785 case GI_TYPE_TAG_INT32:
786 case GI_TYPE_TAG_UINT32:
787 case GI_TYPE_TAG_INT64:
788 case GI_TYPE_TAG_UINT64:
789 case GI_TYPE_TAG_FLOAT:
790 case GI_TYPE_TAG_DOUBLE:
791 case GI_TYPE_TAG_UNICHAR:
792 case GI_TYPE_TAG_GTYPE:
793 if (direction & PYGI_DIRECTION_FROM_PYTHON)
794 _arg_cache_from_py_basic_type_setup (arg_cache);
796 if (direction & PYGI_DIRECTION_TO_PYTHON)
797 _arg_cache_to_py_basic_type_setup (arg_cache);
800 case GI_TYPE_TAG_UTF8:
801 case GI_TYPE_TAG_FILENAME:
802 if (direction & PYGI_DIRECTION_FROM_PYTHON)
803 _arg_cache_from_py_utf8_setup (arg_cache, transfer);
805 if (direction & PYGI_DIRECTION_TO_PYTHON)
806 _arg_cache_to_py_utf8_setup (arg_cache, transfer);
810 g_assert_not_reached ();
817 pygi_arg_basic_type_new_from_info (GITypeInfo *type_info,
820 PyGIDirection direction)
822 gboolean res = FALSE;
823 PyGIArgCache *arg_cache = pygi_arg_cache_alloc ();
824 if (arg_cache == NULL)
827 res = pygi_arg_basic_type_setup_from_info (arg_cache,
835 pygi_arg_cache_free (arg_cache);