Commit 46653922 authored by Simon Feltman's avatar Simon Feltman
Browse files

Unify from Python int16 and int32 marshalers

Add PyNumber_Check to unified basic type marshaler.
Add bounds checking to unified int16 and int32 marshalers.
Replaced int16 and int32 cached marshalers with usage of
unified basic type marshaler.

https://bugzilla.gnome.org/show_bug.cgi?id=693405
parent 4b9c725a
......@@ -283,30 +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_int16_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_int16;
}
static void
_arg_cache_from_py_uint16_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_uint16;
}
static void
_arg_cache_from_py_int32_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_int32;
}
static void
_arg_cache_from_py_uint32_setup (PyGIArgCache *arg_cache)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_uint32;
}
static void
_arg_cache_from_py_int64_setup (PyGIArgCache *arg_cache)
{
......@@ -788,59 +764,16 @@ _arg_cache_new (GITypeInfo *type_info,
case GI_TYPE_TAG_BOOLEAN:
case GI_TYPE_TAG_INT8:
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_basic_type_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_INT16:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_int16_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_UINT16:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_uint16_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_INT32:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_int32_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_UINT32:
arg_cache = _arg_cache_alloc ();
if (arg_cache == NULL)
break;
if (direction == PYGI_DIRECTION_FROM_PYTHON || direction == PYGI_DIRECTION_BIDIRECTIONAL)
_arg_cache_from_py_uint32_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,168 +275,6 @@ _pygi_marshal_from_py_void (PyGIInvokeState *state,
return TRUE;
}
gboolean
_pygi_marshal_from_py_int16 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
PyObject *py_long;
long long_;
if (!PyNumber_Check (py_arg)) {
PyErr_Format (PyExc_TypeError, "Must be number, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
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_, -32768, 32767);
return FALSE;
}
if (long_ < -32768 || long_ > 32767) {
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, -32768, 32767);
return FALSE;
}
arg->v_int16 = long_;
return TRUE;
}
gboolean
_pygi_marshal_from_py_uint16 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
PyObject *py_long;
long long_;
if (!PyNumber_Check (py_arg)) {
PyErr_Format (PyExc_TypeError, "Must be number, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
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, 65535);
return FALSE;
}
if (long_ < 0 || long_ > 65535) {
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, 0, 65535);
return FALSE;
}
arg->v_uint16 = long_;
return TRUE;
}
gboolean
_pygi_marshal_from_py_int32 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
PyObject *py_long;
long long_;
if (!PyNumber_Check (py_arg)) {
PyErr_Format (PyExc_TypeError, "Must be number, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
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_, G_MININT32, G_MAXINT32);
return FALSE;
}
if (long_ < G_MININT32 || long_ > G_MAXINT32) {
PyErr_Format (PyExc_ValueError, "%ld not in range %d to %d", long_, G_MININT32, G_MAXINT32);
return FALSE;
}
arg->v_int32 = long_;
return TRUE;
}
gboolean
_pygi_marshal_from_py_uint32 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg)
{
PyObject *py_long;
long long long_;
if (!PyNumber_Check (py_arg)) {
PyErr_Format (PyExc_TypeError, "Must be number, not %s",
py_arg->ob_type->tp_name);
return FALSE;
}
py_long = PYGLIB_PyNumber_Long (py_arg);
if (!py_long)
return FALSE;
#if PY_VERSION_HEX < 0x03000000
if (PyInt_Check (py_long))
long_ = PyInt_AsLong (py_long);
else
#endif
long_ = PyLong_AsLongLong (py_long);
Py_DECREF (py_long);
if (PyErr_Occurred ()) {
PyErr_Clear ();
PyErr_Format (PyExc_ValueError, "%lld not in range %i to %u", long_, 0, G_MAXUINT32);
return FALSE;
}
if (long_ < 0 || long_ > G_MAXUINT32) {
PyErr_Format (PyExc_ValueError, "%lld not in range %i to %u", long_, 0, G_MAXUINT32);
return FALSE;
}
arg->v_uint32 = long_;
return TRUE;
}
gboolean
_pygi_marshal_from_py_int64 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
......@@ -823,6 +661,12 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
{
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);
......@@ -868,23 +712,55 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
}
case GI_TYPE_TAG_INT16:
arg->v_int16 = PyLong_AsLong (number);
{
long long_value = PyLong_AsLong (number);
if (long_value < G_MININT16 || long_value > G_MAXINT16) {
PyErr_Format (PyExc_ValueError, "%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_INT32:
arg->v_int32 = PyLong_AsLong (number);
case GI_TYPE_TAG_UINT16:
{
long long_value = PyLong_AsLong (number);
if (long_value < 0 || long_value > G_MAXUINT16) {
PyErr_Format (PyExc_ValueError, "%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_INT64:
arg->v_int64 = PyLong_AsLongLong (number);
case GI_TYPE_TAG_INT32:
{
long long_value = PyLong_AsLong (number);
if (long_value < G_MININT32 || long_value > G_MAXINT32) {
PyErr_Format (PyExc_ValueError, "%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_UINT16:
arg->v_uint16 = PyLong_AsLong (number);
case GI_TYPE_TAG_UINT32:
{
PY_LONG_LONG long_value = PyLong_AsLongLong (number);
if (long_value < 0 || long_value > G_MAXUINT32) {
PyErr_Format (PyExc_ValueError, "%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_UINT32:
arg->v_uint32 = PyLong_AsUnsignedLong (number);
case GI_TYPE_TAG_INT64:
arg->v_int64 = PyLong_AsLongLong (number);
break;
case GI_TYPE_TAG_UINT64:
......
......@@ -38,26 +38,6 @@ gboolean _pygi_marshal_from_py_void (PyGIInvokeState *state,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_int16 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_uint16 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_int32 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_uint32 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg);
gboolean _pygi_marshal_from_py_int64 (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
......
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