[pygobject] Unify Python float and double to GI marshaling code



commit 1ea654b4d34e0d119556b232796cd9370b2572f1
Author: Simon Feltman <sfeltman src gnome org>
Date:   Thu Mar 28 06:17:15 2013 -0700

    Unify Python float and double to GI marshaling code
    
    Change _pygi_argument_from_object to use the cachers marshalers
    (_pygi_marshal_from_py_float/double) directly instead of keeping a
    copy of the code.
    Refactor _pygi_marshal_from_py_float/double to use a common utility
    _pygi_py_arg_to_double for initial error checking and conversion.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=693405

 gi/pygi-argument.c        |   26 ++++----------------------
 gi/pygi-marshal-from-py.c |   42 ++++++++++++++++++++----------------------
 2 files changed, 24 insertions(+), 44 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 8868d4e..8cc467c 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -999,32 +999,14 @@ _pygi_argument_from_object (PyObject   *object,
         }
         case GI_TYPE_TAG_FLOAT:
         {
-            PyObject *float_;
-
-            float_ = PyNumber_Float (object);
-            if (float_ == NULL) {
-                PyErr_SetString (PyExc_TypeError, "expected float or int argument");
-                break;
-            }
-
-            arg.v_float = (float) PyFloat_AsDouble (float_);
-            Py_DECREF (float_);
-
+            _pygi_marshal_from_py_float (NULL, NULL, NULL,
+                                         object, &arg);
             break;
         }
         case GI_TYPE_TAG_DOUBLE:
         {
-            PyObject *float_;
-
-            float_ = PyNumber_Float (object);
-            if (float_ == NULL) {
-                PyErr_SetString (PyExc_TypeError, "expected float or int argument");
-                break;
-            }
-
-            arg.v_double = PyFloat_AsDouble (float_);
-            Py_DECREF (float_);
-
+            _pygi_marshal_from_py_double (NULL, NULL, NULL,
+                                          object, &arg);
             break;
         }
         case GI_TYPE_TAG_GTYPE:
diff --git a/gi/pygi-marshal-from-py.c b/gi/pygi-marshal-from-py.c
index debdfc1..5858e68 100644
--- a/gi/pygi-marshal-from-py.c
+++ b/gi/pygi-marshal-from-py.c
@@ -696,15 +696,10 @@ check_valid_double (double x, double min, double max)
     return TRUE;
 }
 
-gboolean
-_pygi_marshal_from_py_float (PyGIInvokeState   *state,
-                             PyGICallableCache *callable_cache,
-                             PyGIArgCache      *arg_cache,
-                             PyObject          *py_arg,
-                             GIArgument        *arg)
+static gboolean
+_pygi_py_arg_to_double (PyObject *py_arg, double *double_)
 {
     PyObject *py_float;
-    double double_;
 
     if (!PyNumber_Check (py_arg)) {
         PyErr_Format (PyExc_TypeError, "Must be number, not %s",
@@ -716,14 +711,29 @@ _pygi_marshal_from_py_float (PyGIInvokeState   *state,
     if (!py_float)
         return FALSE;
 
-    double_ = PyFloat_AsDouble (py_float);
+    *double_ = PyFloat_AsDouble (py_float);
     Py_DECREF (py_float);
 
+
+    return TRUE;
+}
+
+gboolean
+_pygi_marshal_from_py_float (PyGIInvokeState   *state,
+                             PyGICallableCache *callable_cache,
+                             PyGIArgCache      *arg_cache,
+                             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;
 }
 
@@ -734,27 +744,15 @@ _pygi_marshal_from_py_double (PyGIInvokeState   *state,
                               PyObject          *py_arg,
                               GIArgument        *arg)
 {
-    PyObject *py_float;
     double double_;
 
-    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)
+    if (!_pygi_py_arg_to_double (py_arg, &double_))
         return FALSE;
 
-    double_ = PyFloat_AsDouble (py_float);
-    Py_DECREF (py_float);
-
     if (PyErr_Occurred () || !check_valid_double (double_, -G_MAXDOUBLE, G_MAXDOUBLE))
         return FALSE;
 
     arg->v_double = double_;
-
     return TRUE;
 }
 


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