[pygobject] for py3k we need to do some more processing to get bytes from a unicode string



commit c52f8ed3ae8cb66a03b5695e980770c3f467f755
Author: John (J5) Palmieri <johnp redhat com>
Date:   Wed Aug 11 16:04:48 2010 -0400

    for py3k we need to do some more processing to get bytes from a unicode string
    
    https://bugzilla.gnome.org/show_bug.cgi?id=615872

 gi/pygi-argument.c |   31 +++++++++++++++++++++++++++----
 1 files changed, 27 insertions(+), 4 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 007c15f..8c7c321 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -731,11 +731,20 @@ _pygi_argument_from_object (PyObject   *object,
                 arg.v_string = NULL;
                 break;
             }
+#if PY_VERSION_HEX < 0x03000000
+            string = g_strdup(PyString_AsString (object));
+#else
+            {
+                PyObject *pybytes_obj = PyUnicode_AsUTF8String (object);
+                if (!pybytes_obj)
+                    break;
 
-            string = PyString_AsString (object);
+                string = g_strdup(PyBytes_AsString (pybytes_obj));
+                Py_DECREF (pybytes_obj);
+            }
+#endif
+            arg.v_string = string;
 
-            /* Don't need to check for errors, since g_strdup is NULL-proof. */
-            arg.v_string = g_strdup (string);
             break;
         }
         case GI_TYPE_TAG_FILENAME:
@@ -743,12 +752,26 @@ _pygi_argument_from_object (PyObject   *object,
             GError *error = NULL;
             const gchar *string;
 
-            string = PyString_AsString (object);
+#if PY_VERSION_HEX < 0x03000000
+            string = g_strdup(PyString_AsString (object));
+#else
+            {
+                PyObject *pybytes_obj = PyUnicode_AsUTF8String (object);
+                if (!pybytes_obj)
+                    break;
+
+                string = g_strdup(PyBytes_AsString (pybytes_obj));
+                Py_DECREF (pybytes_obj);
+            }
+#endif
+
             if (string == NULL) {
                 break;
             }
 
             arg.v_string = g_filename_from_utf8 (string, -1, NULL, NULL, &error);
+            g_free(string);
+
             if (arg.v_string == NULL) {
                 PyErr_SetString (PyExc_Exception, error->message);
                 /* TODO: Convert the error to an exception. */



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