[pygobject] Unify from Python boolean, int8, and uint8 marshalers



commit 4b9c725a615fcf4a5e8d089d275d4586032d0d1f
Author: Simon Feltman <sfeltman src gnome org>
Date:   Tue Jul 23 00:27:14 2013 -0700

    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

 gi/pygi-cache.c           |   48 ++-------------
 gi/pygi-marshal-from-py.c |  146 ++++++++++----------------------------------
 gi/pygi-marshal-from-py.h |   20 ++-----
 3 files changed, 45 insertions(+), 169 deletions(-)
---
diff --git a/gi/pygi-cache.c b/gi/pygi-cache.c
index f9e26af..48b814b 100644
--- a/gi/pygi-cache.c
+++ b/gi/pygi-cache.c
@@ -260,6 +260,12 @@ _arg_cache_alloc (void)
 }
 
 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)
 {
     arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type;
@@ -278,24 +284,6 @@ _arg_cache_to_py_void_setup (PyGIArgCache *arg_cache)
 }
 
 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)
 {
     arg_cache->from_py_marshaller = _pygi_marshal_from_py_int16;
@@ -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);
diff --git a/gi/pygi-marshal-from-py.c b/gi/pygi-marshal-from-py.c
index 5f360bd..9e209a0 100644
--- a/gi/pygi-marshal-from-py.c
+++ b/gi/pygi-marshal-from-py.c
@@ -276,114 +276,6 @@ _pygi_marshal_from_py_void (PyGIInvokeState   *state,
 }
 
 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,
                              PyGIArgCache      *arg_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,
diff --git a/gi/pygi-marshal-from-py.h b/gi/pygi-marshal-from-py.h
index 99ee2b6..3975143 100644
--- a/gi/pygi-marshal-from-py.h
+++ b/gi/pygi-marshal-from-py.h
@@ -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*/


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