[pygobject] [gi] when converting to UTF-8 accept Python Unicode objects as input (Python 2)



commit 2c25886bf6710568d0646f82dce4770faa44c40d
Author: John (J5) Palmieri <johnp redhat com>
Date:   Wed Nov 17 14:38:09 2010 -0500

    [gi] when converting to UTF-8 accept Python Unicode objects as input (Python 2)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=620579

 gi/pygi-argument.c          |   14 ++++++++++++--
 glib/pyglib-python-compat.h |    5 +++++
 tests/test_gi.py            |    3 +++
 3 files changed, 20 insertions(+), 2 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index b768e9e..ddd933b 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -370,7 +370,7 @@ check_number_release:
         }
         case GI_TYPE_TAG_UTF8:
         case GI_TYPE_TAG_FILENAME:
-            if (!PYGLIB_PyUnicode_Check (object)) {
+            if (!PYGLIB_PyBaseString_Check (object) ) {
                 PyErr_Format (PyExc_TypeError, "Must be string, not %s",
                               object->ob_type->tp_name);
                 retval = 0;
@@ -751,7 +751,17 @@ _pygi_argument_from_object (PyObject   *object,
                 break;
             }
 #if PY_VERSION_HEX < 0x03000000
-            string = g_strdup(PyString_AsString (object));
+            if (PyUnicode_Check(object)) {
+                 PyObject *pystr_obj = PyUnicode_AsUTF8String (object);
+                 
+                 if (!pystr_obj)
+                     break;
+
+                 string = g_strdup(PyString_AsString (pystr_obj));
+                 Py_DECREF(pystr_obj);
+            } else {
+                 string = g_strdup(PyString_AsString (object));
+            }
 #else
             {
                 PyObject *pybytes_obj = PyUnicode_AsUTF8String (object);
diff --git a/glib/pyglib-python-compat.h b/glib/pyglib-python-compat.h
index 5d7516e..4a52231 100644
--- a/glib/pyglib-python-compat.h
+++ b/glib/pyglib-python-compat.h
@@ -113,6 +113,9 @@ static int _pyglib_init_##modname(PyObject *module)
 /* Compilation on Python 2.x */
 #if PY_VERSION_HEX < 0x03000000
 #define RO READONLY
+
+#define PYGLIB_PyBaseString_Check(ob) (PyString_Check(ob) || PyUnicode_Check(ob))
+
 #define PYGLIB_PyUnicode_Check PyString_Check
 #define PYGLIB_PyUnicode_AsString PyString_AsString
 #define PYGLIB_PyUnicode_AsStringAndSize PyString_AsStringAndSize
@@ -206,6 +209,8 @@ PyTypeObject symbol = {                                 \
 	    return;                                         \
     PyDict_SetItemString(d, name, (PyObject *)&type);
 
+#define PYGLIB_PyBaseString_Check PyUnicode_Check
+
 #define PYGLIB_PyUnicode_Check PyUnicode_Check
 #define PYGLIB_PyUnicode_AsString _PyUnicode_AsString
 #define PYGLIB_PyUnicode_AsStringAndSize(obj, buf, size) \
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 50146a4..bff5bc7 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -14,6 +14,7 @@ from gi.repository import GIMarshallingTests
 
 if sys.version_info < (3, 0):
     CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
+    PY2_UNICODE_UTF8 = u"const â?¥ utf8"
 else:
     CONSTANT_UTF8 = "const â?¥ utf8"
 
@@ -615,6 +616,8 @@ class TestUtf8(unittest.TestCase):
 
     def test_utf8_none_in(self):
         GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
+        if sys.version_info < (3, 0):
+            GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
 
         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)



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