[pygobject] Unify from Python int16 and int32 marshalers



commit 46653922003c7d1d5d16f5cdb39b3faadf9aff27
Author: Simon Feltman <sfeltman src gnome org>
Date:   Tue Jul 23 11:03:14 2013 -0700

    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

 gi/pygi-cache.c           |   69 +--------------
 gi/pygi-marshal-from-py.c |  218 ++++++++++-----------------------------------
 gi/pygi-marshal-from-py.h |   20 ----
 3 files changed, 48 insertions(+), 259 deletions(-)
---
diff --git a/gi/pygi-cache.c b/gi/pygi-cache.c
index 48b814b..d3d5c46 100644
--- a/gi/pygi-cache.c
+++ b/gi/pygi-cache.c
@@ -284,30 +284,6 @@ _arg_cache_to_py_void_setup (PyGIArgCache *arg_cache)
 }
 
 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)
 {
     arg_cache->from_py_marshaller = _pygi_marshal_from_py_int64;
@@ -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);
diff --git a/gi/pygi-marshal-from-py.c b/gi/pygi-marshal-from-py.c
index 9e209a0..e8c9b60 100644
--- a/gi/pygi-marshal-from-py.c
+++ b/gi/pygi-marshal-from-py.c
@@ -276,168 +276,6 @@ _pygi_marshal_from_py_void (PyGIInvokeState   *state,
 }
 
 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,
                              PyGIArgCache      *arg_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:
diff --git a/gi/pygi-marshal-from-py.h b/gi/pygi-marshal-from-py.h
index 3975143..9c644d5 100644
--- a/gi/pygi-marshal-from-py.h
+++ b/gi/pygi-marshal-from-py.h
@@ -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,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]