[pygobject] some more p3k PyString and PyInt eradication in GI



commit 1efa2b12913b194d433c17014bc1077271a6ca32
Author: John (J5) Palmieri <johnp redhat com>
Date:   Mon Aug 16 13:51:05 2010 -0400

    some more p3k PyString and PyInt eradication in GI
    
    * add the glib dir to the includes list in the build
    * make sure we include the compat macros
    * add GLIB_PyBytes_FromString to compat macros
    * add GLIB_PyNumber_Long to compat macros
    * use RichCompare instead of Compare
    
    https://bugzilla.gnome.org/show_bug.cgi?id=615872

 gi/Makefile.am              |    2 +-
 gi/pygi-argument.c          |   96 ++++++++++++++++++++++++++-----------------
 gi/pygi-boxed.c             |    1 +
 gi/pygi-info.c              |   13 ++++--
 gi/pygi-private.h           |   23 ++++++++++
 gi/pygi-repository.c        |    8 ++-
 gi/pygi-struct.c            |    1 +
 glib/pyglib-python-compat.h |    6 +++
 8 files changed, 103 insertions(+), 47 deletions(-)
---
diff --git a/gi/Makefile.am b/gi/Makefile.am
index 1606a95..adad413 100644
--- a/gi/Makefile.am
+++ b/gi/Makefile.am
@@ -7,7 +7,7 @@ SUBDIRS = \
 	repository \
 	overrides
 
-INCLUDES = -I$(top_srcdir)/gobject
+INCLUDES = -I$(top_srcdir)/gobject -I$(top_srcdir)/glib
 
 pygidir = $(pkgpyexecdir)/gi
 pygi_PYTHON = \
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 8c7c321..b66ea1e 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -28,7 +28,7 @@
 
 #include <datetime.h>
 #include <pygobject.h>
-
+#include <pyglib-python-compat.h>
 
 static void
 _pygi_g_type_tag_py_bounds (GITypeTag   type_tag,
@@ -37,29 +37,29 @@ _pygi_g_type_tag_py_bounds (GITypeTag   type_tag,
 {
     switch (type_tag) {
         case GI_TYPE_TAG_INT8:
-            *lower = PyInt_FromLong (-128);
-            *upper = PyInt_FromLong (127);
+            *lower = PYGLIB_PyLong_FromLong (-128);
+            *upper = PYGLIB_PyLong_FromLong (127);
             break;
         case GI_TYPE_TAG_UINT8:
-            *upper = PyInt_FromLong (255);
-            *lower = PyInt_FromLong (0);
+            *upper = PYGLIB_PyLong_FromLong (255);
+            *lower = PYGLIB_PyLong_FromLong (0);
             break;
         case GI_TYPE_TAG_INT16:
-            *lower = PyInt_FromLong (-32768);
-            *upper = PyInt_FromLong (32767);
+            *lower = PYGLIB_PyLong_FromLong (-32768);
+            *upper = PYGLIB_PyLong_FromLong (32767);
             break;
         case GI_TYPE_TAG_UINT16:
-            *upper = PyInt_FromLong (65535);
-            *lower = PyInt_FromLong (0);
+            *upper = PYGLIB_PyLong_FromLong (65535);
+            *lower = PYGLIB_PyLong_FromLong (0);
             break;
         case GI_TYPE_TAG_INT32:
-            *lower = PyInt_FromLong (G_MININT32);
-            *upper = PyInt_FromLong (G_MAXINT32);
+            *lower = PYGLIB_PyLong_FromLong (G_MININT32);
+            *upper = PYGLIB_PyLong_FromLong (G_MAXINT32);
             break;
         case GI_TYPE_TAG_UINT32:
             /* Note: On 32-bit archs, this number doesn't fit in a long. */
             *upper = PyLong_FromLongLong (G_MAXUINT32);
-            *lower = PyInt_FromLong (0);
+            *lower = PYGLIB_PyLong_FromLong (0);
             break;
         case GI_TYPE_TAG_INT64:
             /* Note: On 32-bit archs, these numbers don't fit in a long. */
@@ -68,7 +68,7 @@ _pygi_g_type_tag_py_bounds (GITypeTag   type_tag,
             break;
         case GI_TYPE_TAG_UINT64:
             *upper = PyLong_FromUnsignedLongLong (G_MAXUINT64);
-            *lower = PyInt_FromLong (0);
+            *lower = PYGLIB_PyLong_FromLong (0);
             break;
         case GI_TYPE_TAG_FLOAT:
             *upper = PyFloat_FromDouble (G_MAXFLOAT);
@@ -204,7 +204,7 @@ _pygi_g_type_info_check_object (GITypeInfo *type_info,
             if (type_tag == GI_TYPE_TAG_FLOAT || type_tag == GI_TYPE_TAG_DOUBLE) {
                 number = PyNumber_Float (object);
             } else {
-                number = PyNumber_Int (object);
+                number = PYGLIB_PyNumber_Long (object);
             }
 
             _pygi_g_type_tag_py_bounds (type_tag, &lower, &upper);
@@ -215,8 +215,8 @@ _pygi_g_type_info_check_object (GITypeInfo *type_info,
             }
 
             /* Check bounds */
-            if (PyObject_Compare (lower, number) > 0
-                    || PyObject_Compare (upper, number) < 0) {
+            if (PyObject_RichCompareBool (lower, number, Py_GT)
+                    || PyObject_RichCompareBool (upper, number, Py_LT)) {
                 PyObject *lower_str;
                 PyObject *upper_str;
 
@@ -232,10 +232,30 @@ _pygi_g_type_info_check_object (GITypeInfo *type_info,
                     goto check_number_error_release;
                 }
 
+#if PY_VERSION_HEX < 0x03000000
                 PyErr_Format (PyExc_ValueError, "Must range from %s to %s",
                               PyString_AS_STRING (lower_str),
                               PyString_AS_STRING (upper_str));
+#else
+                {
+                    PyObject *lower_pybytes_obj = PyUnicode_AsUTF8String (lower_str);
+                    if (!lower_pybytes_obj)
+                        goto utf8_fail;
+
+                    PyObject *upper_pybytes_obj = PyUnicode_AsUTF8String (upper_str);                    
+                    if (!upper_pybytes_obj) {
+                        Py_DECREF(lower_pybytes_obj);
+                        goto utf8_fail;
+                    }
 
+                    PyErr_Format (PyExc_ValueError, "Must range from %s to %s",
+                                  PyBytes_AsString (lower_pybytes_obj),
+                                  PyBytes_AsString (upper_pybytes_obj));
+                    Py_DECREF (lower_pybytes_obj);
+                    Py_DECREF (upper_pybytes_obj);
+                }
+utf8_fail:
+#endif
                 retval = 0;
 
 check_number_error_release:
@@ -260,7 +280,7 @@ check_number_release:
         }
         case GI_TYPE_TAG_UTF8:
         case GI_TYPE_TAG_FILENAME:
-            if (!PyString_Check (object)) {
+            if (!PYGLIB_PyUnicode_Check (object)) {
                 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
                               object->ob_type->tp_name);
                 retval = 0;
@@ -344,11 +364,11 @@ check_number_release:
                 case GI_INFO_TYPE_ENUM:
                     retval = 0;
                     if (PyNumber_Check (object)) {
-                        PyObject *number = PyNumber_Int (object);
+                        PyObject *number = PYGLIB_PyNumber_Long (object);
                         if (number == NULL)
                             PyErr_Clear();
                         else {
-                            glong value = PyInt_AsLong (number);
+                            glong value = PYGLIB_PyLong_AsLong (number);
                             int i;
                             for (i = 0; i < g_enum_info_get_n_values (info); i++) {
                                 GIValueInfo *value_info = g_enum_info_get_value (info, i);
@@ -367,11 +387,11 @@ check_number_release:
                 case GI_INFO_TYPE_FLAGS:
                     if (PyNumber_Check (object)) {
                         /* Accept 0 as a valid flag value */
-                        PyObject *number = PyNumber_Int (object);
+                        PyObject *number = PYGLIB_PyNumber_Long (object);
                         if (number == NULL)
                             PyErr_Clear();
                         else {
-                            long value = PyInt_AsLong (number);
+                            long value = PYGLIB_PyLong_AsLong (number);
                             if (value == 0)
                                 break;
                             else if (value == -1)
@@ -633,12 +653,12 @@ _pygi_argument_from_object (PyObject   *object,
         {
             PyObject *int_;
 
-            int_ = PyNumber_Int (object);
+            int_ = PYGLIB_PyNumber_Long (object);
             if (int_ == NULL) {
                 break;
             }
 
-            arg.v_long = PyInt_AsLong (int_);
+            arg.v_long = PYGLIB_PyLong_AsLong (int_);
 
             Py_DECREF (int_);
 
@@ -650,13 +670,13 @@ _pygi_argument_from_object (PyObject   *object,
             PyObject *number;
             guint64 value;
 
-            number = PyNumber_Int (object);
+            number = PYGLIB_PyNumber_Long (object);
             if (number == NULL) {
                 break;
             }
 
-            if (PyInt_Check (number)) {
-                value = PyInt_AS_LONG (number);
+            if (PYGLIB_PyLong_Check (number)) {
+                value = PYGLIB_PyLong_AS_LONG (number);
             } else {
                 value = PyLong_AsUnsignedLongLong (number);
             }
@@ -672,13 +692,13 @@ _pygi_argument_from_object (PyObject   *object,
             PyObject *number;
             gint64 value;
 
-            number = PyNumber_Int (object);
+            number = PYGLIB_PyNumber_Long (object);
             if (number == NULL) {
                 break;
             }
 
-            if (PyInt_Check (number)) {
-                value = PyInt_AS_LONG (number);
+            if (PYGLIB_PyLong_Check (number)) {
+                value = PYGLIB_PyLong_AS_LONG (number);
             } else {
                 value = PyLong_AsLongLong (number);
             }
@@ -934,12 +954,12 @@ array_item_error:
                 {
                     PyObject *int_;
 
-                    int_ = PyNumber_Int (object);
+                    int_ = PYGLIB_PyNumber_Long (object);
                     if (int_ == NULL) {
                         break;
                     }
 
-                    arg.v_long = PyInt_AsLong (int_);
+                    arg.v_long = PYGLIB_PyLong_AsLong (int_);
 
                     Py_DECREF (int_);
 
@@ -1169,27 +1189,27 @@ _pygi_argument_to_object (GArgument  *arg,
         }
         case GI_TYPE_TAG_INT8:
         {
-            object = PyInt_FromLong (arg->v_int8);
+            object = PYGLIB_PyLong_FromLong (arg->v_int8);
             break;
         }
         case GI_TYPE_TAG_UINT8:
         {
-            object = PyInt_FromLong (arg->v_uint8);
+            object = PYGLIB_PyLong_FromLong (arg->v_uint8);
             break;
         }
         case GI_TYPE_TAG_INT16:
         {
-            object = PyInt_FromLong (arg->v_int16);
+            object = PYGLIB_PyLong_FromLong (arg->v_int16);
             break;
         }
         case GI_TYPE_TAG_UINT16:
         {
-            object = PyInt_FromLong (arg->v_uint16);
+            object = PYGLIB_PyLong_FromLong (arg->v_uint16);
             break;
         }
         case GI_TYPE_TAG_INT32:
         {
-            object = PyInt_FromLong (arg->v_int32);
+            object = PYGLIB_PyLong_FromLong (arg->v_int32);
             break;
         }
         case GI_TYPE_TAG_UINT32:
@@ -1229,7 +1249,7 @@ _pygi_argument_to_object (GArgument  *arg,
                 break;
             }
 
-            object = PyString_FromString (arg->v_string);
+            object = PYGLIB_PyUnicode_FromString (arg->v_string);
             break;
         case GI_TYPE_TAG_FILENAME:
         {
@@ -1249,7 +1269,7 @@ _pygi_argument_to_object (GArgument  *arg,
                 break;
             }
 
-            object = PyString_FromString (string);
+            object = PYGLIB_PyUnicode_FromString (string);
 
             g_free (string);
 
diff --git a/gi/pygi-boxed.c b/gi/pygi-boxed.c
index 66a2585..5edd25e 100644
--- a/gi/pygi-boxed.c
+++ b/gi/pygi-boxed.c
@@ -25,6 +25,7 @@
 
 #include <pygobject.h>
 #include <girepository.h>
+#include <pyglib-python-compat.h>
 
 static void
 _boxed_dealloc (PyGIBoxed *self)
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index 7888ada..1a42784 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -24,6 +24,7 @@
 #include "pygi-private.h"
 
 #include <pygobject.h>
+#include <pyglib-python-compat.h>
 
 #define _PyGI_DEFINE_INFO_TYPE(name, cname, base) \
 static PyMethodDef _Py##cname##_methods[]; \
@@ -88,8 +89,10 @@ _base_info_traverse (PyGIBaseInfo *self,
 static PyObject *
 _base_info_repr (PyGIBaseInfo *self)
 {
-    return PyString_FromFormat ("<%s object (%s) at 0x%p>",
-                                self->ob_type->tp_name, g_base_info_get_name (self->info), (void *) self);
+    return PYGLIB_PyUnicode_FromFormat ("<%s object (%s) at 0x%p>",
+                                        self->ob_type->tp_name, 
+                                        g_base_info_get_name (self->info), 
+                                        (void *) self);
 }
 
 static PyMethodDef _PyGIBaseInfo_methods[];
@@ -130,13 +133,13 @@ PyTypeObject PyGIBaseInfo_Type = {
 static PyObject *
 _wrap_g_base_info_get_name (PyGIBaseInfo *self)
 {
-    return PyString_FromString (g_base_info_get_name (self->info));
+    return PYGLIB_PyUnicode_FromString (g_base_info_get_name (self->info));
 }
 
 static PyObject *
 _wrap_g_base_info_get_namespace (PyGIBaseInfo *self)
 {
-    return PyString_FromString (g_base_info_get_namespace (self->info));
+    return PYGLIB_PyUnicode_FromString (g_base_info_get_namespace (self->info));
 }
 
 static PyObject *
@@ -1066,7 +1069,7 @@ _wrap_g_value_info_get_value (PyGIBaseInfo *self)
 
     value = g_value_info_get_value ( (GIValueInfo *) self->info);
 
-    return PyInt_FromLong (value);
+    return PYGLIB_PyLong_FromLong (value);
 }
 
 
diff --git a/gi/pygi-private.h b/gi/pygi-private.h
index 9435c9b..3a14bc3 100644
--- a/gi/pygi-private.h
+++ b/gi/pygi-private.h
@@ -31,6 +31,27 @@
 #include "pygi-property.h"
 
 G_BEGIN_DECLS
+#if PY_VERSION_HEX >= 0x03000000
+
+#define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
+    PyObject *py_error_prefix; \
+    py_error_prefix = PyUnicode_FromFormat(format, ## __VA_ARGS__); \
+    if (py_error_prefix != NULL) { \
+        PyObject *py_error_type, *py_error_value, *py_error_traceback; \
+        PyErr_Fetch(&py_error_type, &py_error_value, &py_error_traceback); \
+        if (PyUnicode_Check(py_error_value)) { \
+            PyObject *new; \
+            new = PyUnicode_Concat(py_error_prefix, py_error_value); \
+            Py_DECREF(py_error_value); \
+            if (new != NULL) { \
+                py_error_value = new; \
+            } \
+        } \
+        PyErr_Restore(py_error_type, py_error_value, py_error_traceback); \
+    } \
+} G_STMT_END
+
+#else
 
 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
     PyObject *py_error_prefix; \
@@ -48,6 +69,8 @@ G_BEGIN_DECLS
     } \
 } G_STMT_END
 
+#endif
+
 /* Redefine g_array_index because we want it to return the i-th element, casted
  * to the type t, of the array a, and not the i-th element of the array a
  * casted to the type t. */
diff --git a/gi/pygi-repository.c b/gi/pygi-repository.c
index fbe8ff5..79727ed 100644
--- a/gi/pygi-repository.c
+++ b/gi/pygi-repository.c
@@ -23,6 +23,8 @@
 
 #include "pygi-private.h"
 
+#include <pyglib-python-compat.h>
+
 PyObject *PyGIRepositoryError;
 
 static PyMethodDef _PyGIRepository_methods[];
@@ -78,7 +80,7 @@ _wrap_g_irepository_enumerate_versions (PyGIRepository *self,
     ret = PyList_New(0);
     for (item = versions; item; item = item->next) {
         char *version = item->data;
-        PyObject *py_version = PyString_FromString (version);
+        PyObject *py_version = PYGLIB_PyUnicode_FromString (version);
         PyList_Append(ret, py_version);
         Py_DECREF(py_version);
         g_free (version);
@@ -236,7 +238,7 @@ _wrap_g_irepository_get_typelib_path (PyGIRepository *self,
         return NULL;
     }
 
-    return PyString_FromString (typelib_path);
+    return PYGLIB_PyBytes_FromString (typelib_path);
 }
 
 static PyObject *
@@ -259,7 +261,7 @@ _wrap_g_irepository_get_version (PyGIRepository *self,
         return NULL;
     }
 
-    return PyString_FromString (version);
+    return PYGLIB_PyUnicode_FromString (version);
 }
 
 static PyMethodDef _PyGIRepository_methods[] = {
diff --git a/gi/pygi-struct.c b/gi/pygi-struct.c
index ea56890..ffdf501 100644
--- a/gi/pygi-struct.c
+++ b/gi/pygi-struct.c
@@ -25,6 +25,7 @@
 
 #include <pygobject.h>
 #include <girepository.h>
+#include <pyglib-python-compat.h>
 
 static void
 _struct_dealloc (PyGIStruct *self)
diff --git a/glib/pyglib-python-compat.h b/glib/pyglib-python-compat.h
index c695973..bb7bcad 100644
--- a/glib/pyglib-python-compat.h
+++ b/glib/pyglib-python-compat.h
@@ -95,6 +95,7 @@ static int _pyglib_init_##modname(PyObject *module)
 #define PYGLIB_PyUnicode_GET_SIZE PyString_GET_SIZE
 #define PYGLIB_PyUnicode_Type PyString_Type
 
+#define PYGLIB_PyBytes_FromString PyString_FromString
 #define PYGLIB_PyBytes_FromStringAndSize PyString_FromStringAndSize
 #define PYGLIB_PyBytes_Resize _PyString_Resize
 #define PYGLIB_PyBytes_AsString PyString_AsString
@@ -111,6 +112,8 @@ static int _pyglib_init_##modname(PyObject *module)
 #define PYGLIB_PyLong_AS_LONG PyInt_AS_LONG
 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
 
+#define PYGLIB_PyNumber_Long PyNumber_Int
+
 #ifndef PyVarObject_HEAD_INIT
 #define PyVarObject_HEAD_INIT(base, size) \
   PyObject_HEAD_INIT(base) \
@@ -192,12 +195,15 @@ PyTypeObject symbol = {                                 \
 #define PYGLIB_PyLongObject PyLongObject
 #define PYGLIB_PyLong_Type PyLong_Type
 
+#define PYGLIB_PyBytes_FromString PyBytes_FromString
 #define PYGLIB_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
 #define PYGLIB_PyBytes_Resize(o, len) _PyBytes_Resize(o, len)
 #define PYGLIB_PyBytes_AsString PyBytes_AsString
 #define PYGLIB_PyBytes_Size PyBytes_Size
 #define PYGLIB_PyBytes_Check PyBytes_Check
 
+#define PYGLIB_PyNumber_Long PyNumber_Long
+
 #endif
 
 #endif /* __PYGLIB_PYTHON_COMPAT_H__ */



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