[pygobject] Use accessors for getting and setting PyGBoxed pointers



commit 92fe52243d819ffe91597744a6a1c2362a295bce
Author: Simon Feltman <sfeltman src gnome org>
Date:   Fri May 16 14:19:47 2014 -0700

    Use accessors for getting and setting PyGBoxed pointers
    
    Add pyg_boxed_get_ptr and pyg_boxed_set_ptr macros for getting and setting
    the boxed pointer field. This is preliminary cleanup work for supporting
    fundamental types.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=631901

 gi/pygboxed.c   |   20 ++++++++++----------
 gi/pygi-boxed.c |    6 +++---
 gi/pygobject.h  |    2 ++
 gi/pygtype.c    |    3 ++-
 4 files changed, 17 insertions(+), 14 deletions(-)
---
diff --git a/gi/pygboxed.c b/gi/pygboxed.c
index 5e579db..9faa652 100644
--- a/gi/pygboxed.c
+++ b/gi/pygboxed.c
@@ -37,9 +37,9 @@ PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);
 static void
 pyg_boxed_dealloc(PyGBoxed *self)
 {
-    if (self->free_on_dealloc && self->boxed) {
+    if (self->free_on_dealloc && pyg_boxed_get_ptr (self)) {
        PyGILState_STATE state = pyglib_gil_state_ensure();
-       g_boxed_free(self->gtype, self->boxed);
+       g_boxed_free (self->gtype, pyg_boxed_get_ptr (self));
        pyglib_gil_state_release(state);
     }
 
@@ -51,9 +51,9 @@ pyg_boxed_richcompare(PyObject *self, PyObject *other, int op)
 {
     if (Py_TYPE(self) == Py_TYPE(other) &&
         PyObject_IsInstance(self, (PyObject*)&PyGBoxed_Type))
-        return _pyglib_generic_ptr_richcompare(((PyGBoxed*)self)->boxed,
-                                               ((PyGBoxed*)other)->boxed,
-                                               op);
+        return _pyglib_generic_ptr_richcompare (pyg_boxed_get_ptr (self),
+                                                pyg_boxed_get_ptr (other),
+                                                op);
     else {
         Py_INCREF(Py_NotImplemented);
         return Py_NotImplemented;
@@ -64,7 +64,7 @@ pyg_boxed_richcompare(PyObject *self, PyObject *other, int op)
 static long
 pyg_boxed_hash(PyGBoxed *self)
 {
-    return (long)self->boxed;
+    return (long)pyg_boxed_get_ptr (self);
 }
 
 static PyObject *
@@ -73,7 +73,7 @@ pyg_boxed_repr(PyGBoxed *self)
     gchar buf[128];
 
     g_snprintf(buf, sizeof(buf), "<%s at 0x%lx>", g_type_name(self->gtype),
-              (long)self->boxed);
+              (long)pyg_boxed_get_ptr (self));
     return PYGLIB_PyUnicode_FromString(buf);
 }
 
@@ -85,7 +85,7 @@ pyg_boxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
     if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
        return -1;
 
-    self->boxed = NULL;
+    pyg_boxed_set_ptr (self, NULL);
     self->gtype = 0;
     self->free_on_dealloc = FALSE;
 
@@ -104,7 +104,7 @@ pyg_boxed_free(PyObject *op)
 static PyObject *
 pyg_boxed_copy(PyGBoxed *self)
 {
-    return pyg_boxed_new (self->gtype, self->boxed, TRUE, TRUE);
+    return pyg_boxed_new (self->gtype, pyg_boxed_get_ptr (self), TRUE, TRUE);
 }
 
 
@@ -206,7 +206,7 @@ pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
 
     if (copy_boxed)
        boxed = g_boxed_copy(boxed_type, boxed);
-    self->boxed = boxed;
+    pyg_boxed_set_ptr (self, boxed);
     self->gtype = boxed_type;
     self->free_on_dealloc = own_ref;
 
diff --git a/gi/pygi-boxed.c b/gi/pygi-boxed.c
index c52858b..b46cdcc 100644
--- a/gi/pygi-boxed.c
+++ b/gi/pygi-boxed.c
@@ -32,10 +32,10 @@ _boxed_dealloc (PyGIBoxed *self)
 
     if ( ( (PyGBoxed *) self)->free_on_dealloc) {
         if (self->slice_allocated) {
-            g_slice_free1 (self->size, ( (PyGBoxed *) self)->boxed);
+            g_slice_free1 (self->size, pyg_boxed_get_ptr (self));
         } else {
             g_type = pyg_type_from_object ( (PyObject *) self);
-            g_boxed_free (g_type, ( (PyGBoxed *) self)->boxed);
+            g_boxed_free (g_type, pyg_boxed_get_ptr (self));
         }
     }
 
@@ -158,8 +158,8 @@ _pygi_boxed_new (PyTypeObject *type,
     }
 
     ( (PyGBoxed *) self)->gtype = pyg_type_from_object ( (PyObject *) type);
-    ( (PyGBoxed *) self)->boxed = boxed;
     ( (PyGBoxed *) self)->free_on_dealloc = free_on_dealloc;
+    pyg_boxed_set_ptr (self, boxed);
     if (allocated_slice > 0) {
         self->size = allocated_slice;
         self->slice_allocated = TRUE;
diff --git a/gi/pygobject.h b/gi/pygobject.h
index 76b8b11..1bb87a7 100644
--- a/gi/pygobject.h
+++ b/gi/pygobject.h
@@ -57,6 +57,8 @@ typedef struct {
 } PyGBoxed;
 
 #define pyg_boxed_get(v,t)      ((t *)((PyGBoxed *)(v))->boxed)
+#define pyg_boxed_get_ptr(v)    (((PyGBoxed *)(v))->boxed)
+#define pyg_boxed_set_ptr(v,p)  (((PyGBoxed *)(v))->boxed = (gpointer)p)
 #define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == 
typecode)
 
 typedef struct {
diff --git a/gi/pygtype.c b/gi/pygtype.c
index 5957e5c..985e969 100644
--- a/gi/pygtype.c
+++ b/gi/pygtype.c
@@ -890,7 +890,8 @@ pyg_signal_class_closure_marshal(GClosure *closure,
            && item->ob_refcnt != 1) {
            PyGBoxed* boxed_item = (PyGBoxed*)item;
            if (!boxed_item->free_on_dealloc) {
-               boxed_item->boxed = g_boxed_copy(boxed_item->gtype, boxed_item->boxed);
+               gpointer boxed_ptr = pyg_boxed_get_ptr (boxed_item);
+               pyg_boxed_set_ptr (boxed_item, g_boxed_copy (boxed_item->gtype, boxed_ptr));
                boxed_item->free_on_dealloc = TRUE;
            }
        }


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