Commit 4b9c725a authored by Simon Feltman's avatar Simon Feltman
Browse files

Unify from Python boolean, int8, and uint8 marshalers

Replaced boolean, int8, and uint8 cached marshalers with usage of
unified basic type marshaler. Add bounds checking to unified int8
marshalers.

https://bugzilla.gnome.org/show_bug.cgi?id=693405
parent f517bfbc
......@@ -259,6 +259,12 @@ _arg_cache_alloc (void)
return g_slice_new0 (PyGIArgCache);
}
static void
_arg_cache_from_py_basic_type_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
}
static void
_arg_cache_to_py_basic_type_setup (PyGIArgCache *arg_cache)
{
......@@ -277,24 +283,6 @@ _arg_cache_to_py_void_setup (PyGIArgCache *arg_cache)
arg_cache->to_py_marshaller = _pygi_marshal_to_py_void;
}
static void
_arg_cache_from_py_boolean_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_boolean;
}
static void
_arg_cache_from_py_int8_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_int8;
}
static void
_arg_cache_from_py_uint8_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_uint8;
}
static void
_arg_cache_from_py_int16_setup (PyGIArgCache *arg_cache)
{
......@@ -798,36 +786,14 @@ _arg_cache_new (GITypeInfo *type_info,
break;
case GI_TYPE_TAG_BOOLEAN:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_boolean_setup (arg_cache);
if (direction == PYGI_DIRECTION_TO_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_to_py_basic_type_setup (arg_cache);
break;
case GI_TYPE_TAG_INT8:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_int8_setup (arg_cache);
if (direction == PYGI_DIRECTION_TO_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_to_py_basic_type_setup (arg_cache);
break;
case GI_TYPE_TAG_UINT8:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_uint8_setup (arg_cache);
_arg_cache_from_py_basic_type_setup (arg_cache);
if (direction == PYGI_DIRECTION_TO_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_to_py_basic_type_setup (arg_cache);
......
......@@ -275,114 +275,6 @@ _pygi_marshal_from_py_void (PyGIInvokeState *state,
return TRUE;
}
gboolean
_pygi_marshal_from_py_boolean (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
arg->v_boolean = PyObject_IsTrue (py_arg);
return TRUE;
}
gboolean
_pygi_marshal_from_py_int8 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
PyObject *py_long;
long long_;
if (PYGLIB_PyBytes_Check (py_arg)) {
if (PYGLIB_PyBytes_Size (py_arg) != 1) {
PyErr_Format (PyExc_TypeError, "Must be a single character");
return FALSE;
}
long_ = (char)(PYGLIB_PyBytes_AsString (py_arg)[0]);
} else if (PyNumber_Check (py_arg)) {
py_long = PYGLIB_PyNumber_Long (py_arg);
if (!py_long)
return FALSE;
long_ = PYGLIB_PyLong_AsLong (py_long);
Py_DECREF (py_long);
if (PyErr_Occurred ()) {
PyErr_Clear ();
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
return FALSE;
}
} else {
PyErr_Format (PyExc_TypeError, "Must be number or single byte string, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
if (long_ < -128 || long_ > 127) {
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -128, 127);
return FALSE;
}
arg->v_int8 = long_;
return TRUE;
}
gboolean
_pygi_marshal_from_py_uint8 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
unsigned long long_;
if (PYGLIB_PyBytes_Check (py_arg)) {
if (PYGLIB_PyBytes_Size (py_arg) != 1) {
PyErr_Format (PyExc_TypeError, "Must be a single character");
return FALSE;
}
long_ = (unsigned char)(PYGLIB_PyBytes_AsString (py_arg)[0]);
} else if (PyNumber_Check (py_arg)) {
PyObject *py_long;
py_long = PYGLIB_PyNumber_Long (py_arg);
if (!py_long)
return FALSE;
long_ = PYGLIB_PyLong_AsLong (py_long);
Py_DECREF (py_long);
if (PyErr_Occurred ()) {
PyErr_Clear();
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
return FALSE;
}
} else {
PyErr_Format (PyExc_TypeError, "Must be number or single byte string, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
if (long_ < 0 || long_ > 255) {
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 255);
return FALSE;
}
arg->v_uint8 = long_;
return TRUE;
}
gboolean
_pygi_marshal_from_py_int16 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
......@@ -952,8 +844,28 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
switch (type_tag) {
case GI_TYPE_TAG_INT8:
arg->v_int8 = PyLong_AsLong (number);
{
long long_value = PyLong_AsLong (number);
if (long_value < G_MININT8 || long_value > G_MAXINT8) {
PyErr_Format (PyExc_ValueError, "%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 (long_value < 0 || long_value > G_MAXUINT8) {
PyErr_Format (PyExc_ValueError, "%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:
arg->v_int16 = PyLong_AsLong (number);
......@@ -967,10 +879,6 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
arg->v_int64 = PyLong_AsLongLong (number);
break;
case GI_TYPE_TAG_UINT8:
arg->v_uint8 = PyLong_AsLong (number);
break;
case GI_TYPE_TAG_UINT16:
arg->v_uint16 = PyLong_AsLong (number);
break;
......@@ -1088,6 +996,18 @@ _pygi_marshal_from_py_basic_type (PyObject *object, /* in */
return TRUE;
}
gboolean
_pygi_marshal_from_py_basic_type_cache_adapter (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
return _pygi_marshal_from_py_basic_type (py_arg,
arg,
arg_cache->type_tag,
arg_cache->transfer);
}
gboolean
_pygi_marshal_from_py_array (PyGIInvokeState *state,
......
......@@ -38,21 +38,6 @@ gboolean _pygi_marshal_from_py_void (PyGIInvokeState *state,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_boolean (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_int8 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_uint8 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_int16 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
......@@ -189,6 +174,11 @@ gboolean _pygi_marshal_from_py_basic_type (PyObject *object, /* in */
GIArgument *arg, /* out */
GITypeTag type_tag,
GITransfer transfer);
gboolean _pygi_marshal_from_py_basic_type_cache_adapter (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean pygi_marshal_from_py_gobject (PyObject *py_arg, /*in*/
GIArgument *arg, /*out*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment