[pygobject] fix static ABI for setting string gvalues from python objects



commit 0534eb0e843cdf09611143da184052f7e549e4dc
Author: John (J5) Palmieri <johnp redhat com>
Date:   Tue Mar 22 18:46:28 2011 -0400

    fix static ABI for setting string gvalues from python objects
    
     * the static bindings used to be able to set a string gvalue to any python
       object that implemented __str__, for instance when setting a treemodel column
     * this restores that code while still keeping unicode and python 3
       compatability

 gobject/pygtype.c        |   28 +++++++++++++++++++---------
 tests/test_properties.py |    8 ++++++++
 2 files changed, 27 insertions(+), 9 deletions(-)
---
diff --git a/gobject/pygtype.c b/gobject/pygtype.c
index 40d9b42..63ac1c6 100644
--- a/gobject/pygtype.c
+++ b/gobject/pygtype.c
@@ -891,17 +891,27 @@ pyg_value_from_pyobject(GValue *value, PyObject *obj)
     case G_TYPE_STRING:
 	if (obj == Py_None) {
 	    g_value_set_string(value, NULL);
+	} else {
+	    PyObject* tmp_str = PyObject_Str(obj);
+	    if (tmp_str == NULL) {
+	        PyErr_Clear();
+	        if (PyUnicode_Check(obj)) {
+	            tmp = PyUnicode_AsUTF8String(obj);
+	            g_value_set_string(value, PYGLIB_PyBytes_AsString(tmp));
+	            Py_DECREF(tmp);
+	        } else {
+	            return -1;
+	        }
+	    } else {
 #if PY_VERSION_HEX < 0x03000000
-	} else if (PyString_Check(obj)) {
-	    g_value_set_string(value, PyString_AsString(obj));
+	       g_value_set_string(value, PyString_AsString(tmp_str));
+#else
+	       tmp = PyUnicode_AsUTF8String(tmp_str);
+	       g_value_set_string(value, PyBytes_AsString(tmp));
+	       Py_DECREF(tmp);
 #endif
-	} else if (PyUnicode_Check(obj)) {
-	    tmp = PyUnicode_AsUTF8String(obj);
-	    g_value_set_string(value, PYGLIB_PyBytes_AsString(tmp));
-	    Py_DECREF(tmp);
-	} else {
-	    PyErr_Clear();
-	    return -1;
+	    }
+	    Py_XDECREF(tmp_str);
 	}
 	break;
     case G_TYPE_POINTER:
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 1499903..54afd11 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -88,6 +88,14 @@ class TestProperties(unittest.TestCase):
         obj.props.normal = UNICODE_UTF8
         self.assertEqual(obj.props.normal, TEST_UTF8)
 
+    def testIntToStr(self):
+        obj = new(PropertyObject, construct_only=1)
+        self.assertEqual(obj.props.construct_only, '1')
+        obj.set_property('construct', '2')
+        self.assertEqual(obj.props.construct, '2')
+        obj.props.normal = 3
+        self.assertEqual(obj.props.normal, '3')
+
     def testConstructOnly(self):
         obj = new(PropertyObject, construct_only="123")
         self.assertEqual(obj.props.construct_only, "123")



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