diff options
Diffstat (limited to 'gi/pygi-marshal-from-py.c')
-rw-r--r-- | gi/pygi-marshal-from-py.c | 478 |
1 files changed, 0 insertions, 478 deletions
diff --git a/gi/pygi-marshal-from-py.c b/gi/pygi-marshal-from-py.c index e3b8ae8e..b143727c 100644 --- a/gi/pygi-marshal-from-py.c +++ b/gi/pygi-marshal-from-py.c @@ -243,484 +243,6 @@ _is_union_member (GIInterfaceInfo *interface_info, PyObject *py_arg) { } gboolean -_pygi_marshal_from_py_void (PyGIInvokeState *state, - PyGICallableCache *callable_cache, - PyGIArgCache *arg_cache, - PyObject *py_arg, - GIArgument *arg, - gpointer *cleanup_data) -{ - g_warn_if_fail (arg_cache->transfer == GI_TRANSFER_NOTHING); - - if (py_arg == Py_None) { - arg->v_pointer = NULL; - } else if (PYGLIB_CPointer_Check(py_arg)) { - arg->v_pointer = PYGLIB_CPointer_GetPointer (py_arg, NULL); - } else if (PYGLIB_PyLong_Check(py_arg) || PyLong_Check(py_arg)) { - arg->v_pointer = PyLong_AsVoidPtr (py_arg); - } else { - PyErr_SetString(PyExc_ValueError, - "Pointer arguments are restricted to integers, capsules, and None. " - "See: https://bugzilla.gnome.org/show_bug.cgi?id=683599"); - return FALSE; - } - - *cleanup_data = arg->v_pointer; - return TRUE; -} - -static gboolean -check_valid_double (double x, double min, double max) -{ - char buf[100]; - - if ((x < min || x > max) && x != INFINITY && x != -INFINITY && x != NAN) { - if (PyErr_Occurred()) - PyErr_Clear (); - - /* we need this as PyErr_Format() does not support float types */ - snprintf (buf, sizeof (buf), "%g not in range %g to %g", x, min, max); - PyErr_SetString (PyExc_OverflowError, buf); - return FALSE; - } - return TRUE; -} - -static gboolean -_pygi_py_arg_to_double (PyObject *py_arg, double *double_) -{ - PyObject *py_float; - - if (!PyNumber_Check (py_arg)) { - PyErr_Format (PyExc_TypeError, "Must be number, not %s", - py_arg->ob_type->tp_name); - return FALSE; - } - - py_float = PyNumber_Float (py_arg); - if (!py_float) - return FALSE; - - *double_ = PyFloat_AsDouble (py_float); - Py_DECREF (py_float); - - - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_float (PyObject *py_arg, - GIArgument *arg) -{ - double double_; - - if (!_pygi_py_arg_to_double (py_arg, &double_)) - return FALSE; - - if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXFLOAT, G_MAXFLOAT)) - return FALSE; - - arg->v_float = double_; - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_double (PyObject *py_arg, - GIArgument *arg) -{ - double double_; - - if (!_pygi_py_arg_to_double (py_arg, &double_)) - return FALSE; - - if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXDOUBLE, G_MAXDOUBLE)) - return FALSE; - - arg->v_double = double_; - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_unichar (PyObject *py_arg, - GIArgument *arg) -{ - Py_ssize_t size; - gchar *string_; - - if (py_arg == Py_None) { - arg->v_uint32 = 0; - return FALSE; - } - - if (PyUnicode_Check (py_arg)) { - PyObject *py_bytes; - - size = PyUnicode_GET_SIZE (py_arg); - py_bytes = PyUnicode_AsUTF8String (py_arg); - if (!py_bytes) - return FALSE; - - string_ = g_strdup(PYGLIB_PyBytes_AsString (py_bytes)); - Py_DECREF (py_bytes); - -#if PY_VERSION_HEX < 0x03000000 - } else if (PyString_Check (py_arg)) { - PyObject *pyuni = PyUnicode_FromEncodedObject (py_arg, "UTF-8", "strict"); - if (!pyuni) - return FALSE; - - size = PyUnicode_GET_SIZE (pyuni); - string_ = g_strdup (PyString_AsString(py_arg)); - Py_DECREF (pyuni); -#endif - } else { - PyErr_Format (PyExc_TypeError, "Must be string, not %s", - py_arg->ob_type->tp_name); - return FALSE; - } - - if (size != 1) { - PyErr_Format (PyExc_TypeError, "Must be a one character string, not %lld characters", - (long long) size); - g_free (string_); - return FALSE; - } - - arg->v_uint32 = g_utf8_get_char (string_); - g_free (string_); - - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_gtype (PyObject *py_arg, - GIArgument *arg) -{ - long type_ = pyg_type_from_object (py_arg); - - if (type_ == 0) { - PyErr_Format (PyExc_TypeError, "Must be gobject.GType, not %s", - py_arg->ob_type->tp_name); - return FALSE; - } - - arg->v_long = type_; - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_utf8 (PyObject *py_arg, - GIArgument *arg, - gpointer *cleanup_data) -{ - gchar *string_; - - if (py_arg == Py_None) { - arg->v_pointer = NULL; - return TRUE; - } - - if (PyUnicode_Check (py_arg)) { - PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg); - if (!pystr_obj) - return FALSE; - - string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj)); - Py_DECREF (pystr_obj); - } -#if PY_VERSION_HEX < 0x03000000 - else if (PyString_Check (py_arg)) { - string_ = g_strdup (PyString_AsString (py_arg)); - } -#endif - else { - PyErr_Format (PyExc_TypeError, "Must be string, not %s", - py_arg->ob_type->tp_name); - return FALSE; - } - - arg->v_string = string_; - *cleanup_data = arg->v_string; - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_filename (PyObject *py_arg, - GIArgument *arg, - gpointer *cleanup_data) -{ - gchar *string_; - GError *error = NULL; - - if (PyUnicode_Check (py_arg)) { - PyObject *pystr_obj = PyUnicode_AsUTF8String (py_arg); - if (!pystr_obj) - return FALSE; - - string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj)); - Py_DECREF (pystr_obj); - } -#if PY_VERSION_HEX < 0x03000000 - else if (PyString_Check (py_arg)) { - string_ = g_strdup (PyString_AsString (py_arg)); - } -#endif - else { - PyErr_Format (PyExc_TypeError, "Must be string, not %s", - py_arg->ob_type->tp_name); - return FALSE; - } - - arg->v_string = g_filename_from_utf8 (string_, -1, NULL, NULL, &error); - g_free (string_); - - if (arg->v_string == NULL) { - PyErr_SetString (PyExc_Exception, error->message); - g_error_free (error); - /* TODO: Convert the error to an exception. */ - return FALSE; - } - - *cleanup_data = arg->v_string; - return TRUE; -} - -static gboolean -_pygi_marshal_from_py_long (PyObject *object, /* in */ - GIArgument *arg, /* out */ - GITypeTag type_tag, - GITransfer transfer) -{ - PyObject *number; - - if (!PyNumber_Check (object)) { - PyErr_Format (PyExc_TypeError, "Must be number, not %s", - object->ob_type->tp_name); - return FALSE; - } - -#if PY_MAJOR_VERSION < 3 - { - PyObject *tmp = PyNumber_Int (object); - if (tmp) { - number = PyNumber_Long (tmp); - Py_DECREF (tmp); - } else { - number = PyNumber_Long (object); - } - } -#else - number = PyNumber_Long (object); -#endif - - if (number == NULL) { - PyErr_SetString (PyExc_TypeError, "expected int argument"); - return FALSE; - } - - switch (type_tag) { - case GI_TYPE_TAG_INT8: - { - long long_value = PyLong_AsLong (number); - if (PyErr_Occurred()) { - break; - } else if (long_value < G_MININT8 || long_value > G_MAXINT8) { - PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld", - long_value, (long)G_MININT8, (long)G_MAXINT8); - } else { - arg->v_int8 = long_value; - } - break; - } - - case GI_TYPE_TAG_UINT8: - { - long long_value = PyLong_AsLong (number); - if (PyErr_Occurred()) { - break; - } else if (long_value < 0 || long_value > G_MAXUINT8) { - PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld", - long_value, (long)0, (long)G_MAXUINT8); - } else { - arg->v_uint8 = long_value; - } - break; - } - - case GI_TYPE_TAG_INT16: - { - long long_value = PyLong_AsLong (number); - if (PyErr_Occurred()) { - break; - } else if (long_value < G_MININT16 || long_value > G_MAXINT16) { - PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld", - long_value, (long)G_MININT16, (long)G_MAXINT16); - } else { - arg->v_int16 = long_value; - } - break; - } - - case GI_TYPE_TAG_UINT16: - { - long long_value = PyLong_AsLong (number); - if (PyErr_Occurred()) { - break; - } else if (long_value < 0 || long_value > G_MAXUINT16) { - PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld", - long_value, (long)0, (long)G_MAXUINT16); - } else { - arg->v_uint16 = long_value; - } - break; - } - - case GI_TYPE_TAG_INT32: - { - long long_value = PyLong_AsLong (number); - if (PyErr_Occurred()) { - break; - } else if (long_value < G_MININT32 || long_value > G_MAXINT32) { - PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld", - long_value, (long)G_MININT32, (long)G_MAXINT32); - } else { - arg->v_int32 = long_value; - } - break; - } - - case GI_TYPE_TAG_UINT32: - { - PY_LONG_LONG long_value = PyLong_AsLongLong (number); - if (PyErr_Occurred()) { - break; - } else if (long_value < 0 || long_value > G_MAXUINT32) { - PyErr_Format (PyExc_OverflowError, "%lld not in range %ld to %lu", - long_value, (long)0, (unsigned long)G_MAXUINT32); - } else { - arg->v_uint32 = long_value; - } - break; - } - - case GI_TYPE_TAG_INT64: - { - /* Rely on Python overflow error and convert to ValueError for 64 bit values */ - arg->v_int64 = PyLong_AsLongLong (number); - break; - } - - case GI_TYPE_TAG_UINT64: - { - /* Rely on Python overflow error and convert to ValueError for 64 bit values */ - arg->v_uint64 = PyLong_AsUnsignedLongLong (number); - break; - } - - default: - g_assert_not_reached (); - } - - Py_DECREF (number); - - if (PyErr_Occurred()) - return FALSE; - return TRUE; -} - -gboolean -_pygi_marshal_from_py_basic_type (PyObject *object, /* in */ - GIArgument *arg, /* out */ - GITypeTag type_tag, - GITransfer transfer, - gpointer *cleanup_data /* out */) -{ - switch (type_tag) { - case GI_TYPE_TAG_VOID: - g_warn_if_fail (transfer == GI_TRANSFER_NOTHING); - if (object == Py_None) { - arg->v_pointer = NULL; - } else if (!PYGLIB_PyLong_Check(object) && !PyLong_Check(object)) { - PyErr_SetString(PyExc_TypeError, - "Pointer assignment is restricted to integer values. " - "See: https://bugzilla.gnome.org/show_bug.cgi?id=683599"); - } else { - arg->v_pointer = PyLong_AsVoidPtr (object); - *cleanup_data = arg->v_pointer; - } - break; - case GI_TYPE_TAG_INT8: - case GI_TYPE_TAG_UINT8: - if (PYGLIB_PyBytes_Check (object)) { - if (PYGLIB_PyBytes_Size (object) != 1) { - PyErr_Format (PyExc_TypeError, "Must be a single character"); - return FALSE; - } - if (type_tag == GI_TYPE_TAG_INT8) { - arg->v_int8 = (gint8)(PYGLIB_PyBytes_AsString (object)[0]); - } else { - arg->v_uint8 = (guint8)(PYGLIB_PyBytes_AsString (object)[0]); - } - } else { - return _pygi_marshal_from_py_long (object, arg, type_tag, transfer); - } - break; - case GI_TYPE_TAG_INT16: - case GI_TYPE_TAG_UINT16: - case GI_TYPE_TAG_INT32: - case GI_TYPE_TAG_UINT32: - case GI_TYPE_TAG_INT64: - case GI_TYPE_TAG_UINT64: - return _pygi_marshal_from_py_long (object, arg, type_tag, transfer); - - case GI_TYPE_TAG_BOOLEAN: - arg->v_boolean = PyObject_IsTrue (object); - break; - - case GI_TYPE_TAG_FLOAT: - return _pygi_marshal_from_py_float (object, arg); - - case GI_TYPE_TAG_DOUBLE: - return _pygi_marshal_from_py_double (object, arg); - - case GI_TYPE_TAG_GTYPE: - return _pygi_marshal_from_py_gtype (object, arg); - - case GI_TYPE_TAG_UNICHAR: - return _pygi_marshal_from_py_unichar (object, arg); - - case GI_TYPE_TAG_UTF8: - return _pygi_marshal_from_py_utf8 (object, arg, cleanup_data); - - case GI_TYPE_TAG_FILENAME: - return _pygi_marshal_from_py_filename (object, arg, cleanup_data); - - default: - return FALSE; - } - - if (PyErr_Occurred()) - return FALSE; - - return TRUE; -} - -gboolean -_pygi_marshal_from_py_basic_type_cache_adapter (PyGIInvokeState *state, - PyGICallableCache *callable_cache, - PyGIArgCache *arg_cache, - PyObject *py_arg, - GIArgument *arg, - gpointer *cleanup_data) -{ - return _pygi_marshal_from_py_basic_type (py_arg, - arg, - arg_cache->type_tag, - arg_cache->transfer, - cleanup_data); -} - -gboolean _pygi_marshal_from_py_array (PyGIInvokeState *state, PyGICallableCache *callable_cache, PyGIArgCache *arg_cache, |