[pygobject/pygi-py3k: 5/5] Fix test cases by decoding to correctly sized type



commit dedf0c380d1fa551ce9d7fd544b9973202ba1b05
Author: John (J5) Palmieri <johnp redhat com>
Date:   Fri Apr 16 10:58:09 2010 -0400

    Fix test cases by decoding to correctly sized type
    
     * Make work on both Python 3.0 and 2.x by using the Long object instead of
       Int.  Since we are decoding to a C type and this code is internal
       we can assume UnsignedLongLong python type for unsigned values
       to avoid issues where we were mistakenly decoding large values into
       a long.

 gi/pygi-argument.c |   22 +++++++++++++++-------
 1 files changed, 15 insertions(+), 7 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 8ba42ba..c6b62a0 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -686,15 +686,22 @@ _pygi_argument_from_object (PyObject   *object,
         case GI_TYPE_TAG_LONG:
         case GI_TYPE_TAG_SSIZE:
         {
+            /* These types will all fit in long on 32 and 64bit archs */
             PyObject *int_;
+            long value;
 
             int_ = PyNumber_Long(object);
             if (int_ == NULL) {
                 break;
             }
 
-            arg.v_long = PyLong_AsLong(int_);
+            value = PyLong_AsLong(int_);
+            if (value == -1 && PyErr_Occurred()) {
+                Py_DECREF(int_);
+                break;
+            }
 
+            arg.v_long = value;
             Py_DECREF(int_);
 
             break;
@@ -705,6 +712,7 @@ _pygi_argument_from_object (PyObject   *object,
         case GI_TYPE_TAG_ULONG:
         case GI_TYPE_TAG_SIZE:
         {
+            /* all of these types fit in a long long type on all archs */
             PyObject *number;
             guint64 value;
 
@@ -712,13 +720,13 @@ _pygi_argument_from_object (PyObject   *object,
             if (number == NULL) {
                 break;
             }
-
-            if (PyLong_Check(number)) {
-                value = PyLong_AsLong(number);
-            } else {
-                value = PyLong_AsUnsignedLongLong(number);
+           
+            value = PyLong_AsUnsignedLongLong(number);
+            if (value == -1 && PyErr_Occurred()) {
+                Py_DECREF(number);
+                break;
             }
-
+           
             arg.v_uint64 = value;
 
             Py_DECREF(number);



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