[pygobject] handle strings correctly in gio



commit 18ee0db673c2fa42244ab85950bbf4840edb674b
Author: John (J5) Palmieri <johnp redhat com>
Date:   Thu Aug 12 12:16:31 2010 -0400

    handle strings correctly in gio

 gio/gappinfo.override |    5 +++++
 gio/pygio-utils.c     |   42 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 40 insertions(+), 7 deletions(-)
---
diff --git a/gio/gappinfo.override b/gio/gappinfo.override
index 414769b..7f09ce8 100644
--- a/gio/gappinfo.override
+++ b/gio/gappinfo.override
@@ -106,6 +106,11 @@ _wrap_g_app_info_launch_uris(PyGObject *self, PyObject *args, PyObject *kwargs)
     ret = g_app_info_launch_uris(G_APP_INFO(self->obj),
                                  file_list, ctx, &error);
 
+    /* in python 3 the C strings are not internal to the Unicode string object
+     * so we now strdup when adding element to the list and must free them here
+     */
+    g_list_foreach (file_list,
+                   (GFunc) g_free, NULL);
     g_list_free(file_list);
 
     if (pyg_error_check(&error))
diff --git a/gio/pygio-utils.c b/gio/pygio-utils.c
index be41453..f89c4b9 100644
--- a/gio/pygio-utils.c
+++ b/gio/pygio-utils.c
@@ -22,6 +22,7 @@
  */
 
 #include "pygio-utils.h"
+#include <pyglib-python-compat.h>
 
 /**
  * pygio_check_cancellable:
@@ -115,14 +116,28 @@ pygio_pylist_to_uri_glist(PyObject *pyfile_list)
 
     len = PySequence_Size(pyfile_list);
     for (i = 0; i < len; i++) {
-    item = PySequence_GetItem(pyfile_list, i);
-        if (!PyString_Check(item)) {
+        item = PySequence_GetItem(pyfile_list, i);
+        if (!PYGLIB_PyUnicode_Check(item)) {
             PyErr_SetString(PyExc_TypeError,
                             "files must be strings");
             g_list_free(file_list);
             return NULL;
         }
-        file_list = g_list_prepend(file_list, PyString_AsString(item));
+
+#if PY_VERSION_HEX < 0x03000000
+        file_list = g_list_prepend(file_list, g_strdup(PyString_AsString(item)));
+#else
+	{
+            PyObject *utf8_bytes_obj = PyUnicode_AsUTF8String (item);
+            if (!utf8_bytes_obj) {
+                g_list_free(file_list);
+                return NULL;
+            }
+            file_list = g_list_prepend(file_list, g_strdup(PyBytes_AsString(utf8_bytes_obj)));
+            Py_DECREF (utf8_bytes_obj);
+        }
+#endif
+
     }
     file_list = g_list_reverse(file_list);
 
@@ -144,9 +159,9 @@ strv_to_pylist (char **strv)
     len = strv ? g_strv_length (strv) : 0;
     list = PyList_New (len);
 
-    for (i = 0; i < len; i++)
-        PyList_SetItem (list, i, PyString_FromString (strv[i]));
-
+    for (i = 0; i < len; i++) {
+        PyList_SetItem (list, i, PYGLIB_PyUnicode_FromString (strv[i]));
+    }
     return list;
 }
 
@@ -191,7 +206,7 @@ pylist_to_strv (PyObject *list,
             return FALSE;
         }
 
-        if (!PyString_Check (item))
+        if (!PYGLIB_PyUnicode_Check (item))
         {
             Py_DECREF (item);
             g_strfreev (ret);
@@ -199,7 +214,20 @@ pylist_to_strv (PyObject *list,
             return FALSE;
         }
 
+#if PY_VERSION_HEX < 0x03000000
         ret[i] = g_strdup (PyString_AsString (item));
+#else
+	{
+            PyObject *utf8_bytes_obj = PyUnicode_AsUTF8String (item);
+            if (!utf8_bytes_obj) {
+                Py_DECREF (item);
+                g_strfreev (ret);
+                return FALSE;
+            }
+            ret[i] = g_strdup (PyBytes_AsString(utf8_bytes_obj));
+            Py_DECREF (utf8_bytes_obj);
+        }
+#endif
         Py_DECREF (item);
     }
 



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