[pygobject] Check types in GBoxed assignments



commit 635a7d1b48d99ddd1ea123797c493b18b0cdfd45
Author: Marien Zwart <marien zwart gmail com>
Date:   Wed May 23 01:51:46 2012 +0200

    Check types in GBoxed assignments
    
    Check if the Python value is GBoxed instead of assuming it is.
    Without this, the following segfaults:
    
    from gi.repository import Soup
    
    msg = Soup.Message()
    msg.props.uri = 'http://www.gnome.org'
    
    as we assume the new property is a GBoxed while it is actually a
    string.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=676603
    
    Co-authored-by: Martin Pitt <martinpitt gnome org>
    Signed-off-by: Martin Pitt <martinpitt gnome org>

 gi/pygi-argument.c |   10 +++++++---
 tests/test_gi.py   |   19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index ec31d55..fb798e1 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -1112,9 +1112,13 @@ array_success:
                         result = pygi_struct_foreign_convert_to_g_argument (
                                      object, info, transfer, &arg);
                     } else if (g_type_is_a (type, G_TYPE_BOXED)) {
-                        arg.v_pointer = pyg_boxed_get (object, void);
-                        if (transfer == GI_TRANSFER_EVERYTHING) {
-                            arg.v_pointer = g_boxed_copy (type, arg.v_pointer);
+                        if (pyg_boxed_check (object, type)) {
+                            arg.v_pointer = pyg_boxed_get (object, void);
+                            if (transfer == GI_TRANSFER_EVERYTHING) {
+                                arg.v_pointer = g_boxed_copy (type, arg.v_pointer);
+                            }
+                        } else {
+                            PyErr_Format (PyExc_TypeError, "wrong boxed type");
                         }
                     } else if (g_type_is_a (type, G_TYPE_POINTER) || 
                                g_type_is_a (type, G_TYPE_VARIANT) || 
diff --git a/tests/test_gi.py b/tests/test_gi.py
index c0e1a4c..50b5a25 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -2190,3 +2190,22 @@ class TestPropertiesObject(unittest.TestCase):
 
         obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world'])
         self.assertEqual(obj.props.some_strv, ['hello', 'world'])
+
+    def test_boxed_struct(self):
+        self.assertEqual(self.obj.props.some_boxed_struct, None)
+
+        class GStrv(list):
+            __gtype__ = GObject.TYPE_STRV
+
+        struct1 = GIMarshallingTests.BoxedStruct()
+        struct1.long_ = 1
+
+        self.obj.props.some_boxed_struct = struct1
+        self.assertEqual(self.obj.props.some_boxed_struct.long_, 1)
+        self.assertEqual(self.obj.some_boxed_struct.long_, 1)
+
+        self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 1)
+        self.assertRaises(TypeError, setattr, self.obj.props, 'some_boxed_struct', 'foo')
+
+        obj = GIMarshallingTests.PropertiesObject(some_boxed_struct=struct1)
+        self.assertEqual(obj.props.some_boxed_struct.long_, 1)



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