[pygobject] Raise RuntimeError in case an uninitilialized GObject.Object is marshaled



commit 81625ce4c0164dcb3409471fc38168147af4026a
Author: Christoph Reiter <creiter src gnome org>
Date:   Sun Mar 26 16:23:25 2017 +0200

    Raise RuntimeError in case an uninitilialized GObject.Object is marshaled
    
    One common case where this can happen is when subclassing a GObject.Object
    without chaining up __init__ and then calling a method.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=730908

 gi/pygi-object.c      |    6 ++++++
 tests/test_gobject.py |    6 ++++++
 2 files changed, 12 insertions(+), 0 deletions(-)
---
diff --git a/gi/pygi-object.c b/gi/pygi-object.c
index 35a2790..8fd8ee0 100644
--- a/gi/pygi-object.c
+++ b/gi/pygi-object.c
@@ -58,6 +58,12 @@ _pygi_marshal_from_py_gobject (PyObject *py_arg, /*in*/
     }
 
     gobj = pygobject_get (py_arg);
+    if (gobj == NULL) {
+        PyErr_Format(PyExc_RuntimeError, "object at %p of type %s is not initialized",
+                     py_arg, Py_TYPE(py_arg)->tp_name);
+        return FALSE;
+    }
+
     if (transfer == GI_TRANSFER_EVERYTHING) {
         /* For transfer everything, add a new ref that the callee will take ownership of.
          * Pythons existing ref to the GObject will be managed with the PyGObject wrapper.
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index 39a277b..19ef03c 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -16,6 +16,12 @@ import testhelper
 
 
 class TestGObjectAPI(unittest.TestCase):
+
+    def test_call_method_uninitialized_instance(self):
+        obj = GObject.Object.__new__(GObject.Object)
+        with self.assertRaisesRegex(RuntimeError, '.*is not initialized'):
+            obj.notify("foo")
+
     def test_gobject_inheritance(self):
         # GObject.Object is a class hierarchy as follows:
         # overrides.Object -> introspection.Object -> static.GObject


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