[pygobject] Provide access to gpointer struct values



commit 4aeb27efc43e131de5d0bc0f60dca7c1d34c3d45
Author: CÃdric Krier <cedric krier b2ck com>
Date:   Fri Feb 10 09:04:18 2012 +0100

    Provide access to gpointer struct values
    
    https://bugzilla.gnome.org/show_bug.cgi?id=668356
    
    Signed-off-by: Martin Pitt <martin pitt ubuntu com>

 gi/pygi-argument.c       |    3 ++-
 gi/pygi-info.c           |   13 +++++++++++++
 tests/test_everything.py |   21 +++++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 9d99c35..fce91b4 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -1330,7 +1330,8 @@ _pygi_argument_to_object (GIArgument  *arg,
     type_tag = g_type_info_get_tag (type_info);
     switch (type_tag) {
         case GI_TYPE_TAG_VOID:
-            if (g_type_info_is_pointer (type_info)) {
+            if (g_type_info_is_pointer (type_info) &&
+                    (arg->v_pointer != NULL)) {
                 /* Raw Python objects are passed to void* args */
                 g_warn_if_fail (transfer == GI_TRANSFER_NOTHING);
                 object = arg->v_pointer;
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index bf3af58..d3adacc 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -1371,6 +1371,19 @@ _wrap_g_field_info_set_value (PyGIBaseInfo *self,
         }
 
         g_base_info_unref (info);
+    } else if (g_type_info_is_pointer (field_type_info)
+            && g_type_info_get_tag (field_type_info) == GI_TYPE_TAG_VOID) {
+        int offset;
+
+        offset = g_field_info_get_offset ((GIFieldInfo *) self->info);
+        value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_NOTHING);
+
+        Py_XDECREF(G_STRUCT_MEMBER (gpointer, pointer, offset));
+        G_STRUCT_MEMBER (gpointer, pointer, offset) = (gpointer)value.v_pointer;
+        Py_XINCREF(py_value);
+
+        retval = Py_None;
+        goto out;
     }
 
     value = _pygi_argument_from_object (py_value, field_type_info, GI_TRANSFER_EVERYTHING);
diff --git a/tests/test_everything.py b/tests/test_everything.py
index 82edfcf..f4f4bf9 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -8,6 +8,7 @@ import sys
 sys.path.insert(0, "../")
 from sys import getrefcount
 
+import copy
 import cairo
 
 from gi.repository import GObject
@@ -161,6 +162,26 @@ class TestEverything(unittest.TestCase):
     def test_ptrarray(self):
         self.assertEquals (Everything.test_garray_container_return(), ['regress'])
 
+    def test_struct_gpointer(self):
+        l1 = GLib.List()
+        self.assertEqual(l1.data, None)
+        init_refcount = getrefcount(l1)
+
+        l1.data = 'foo'
+        self.assertEqual(l1.data, 'foo')
+
+        l2 = l1
+        self.assertEqual(l1.data, l2.data)
+        self.assertEquals(getrefcount(l1), init_refcount+1)
+
+        l3 = copy.copy(l1)
+        l3.data = 'bar'
+        self.assertEqual(l1.data, 'foo')
+        self.assertEqual(l2.data, 'foo')
+        self.assertEqual(l3.data, 'bar')
+        self.assertEquals(getrefcount(l1), init_refcount+1)
+        self.assertEquals(getrefcount(l3), init_refcount)
+
 class TestNullableArgs(unittest.TestCase):
     def test_in_nullable_hash(self):
         Everything.test_ghash_null_in(None)



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