Commit 96f14989 authored by Steve Frécinaux's avatar Steve Frécinaux Committed by Martin Pitt
Browse files

Fix segfault when accessing __grefcount__ before creating the GObject

When creating a new instance using Type() and trying to access
__grefcount__ before calling the subclass's __init__ function, there
used to be a segmentation fault because we were trying to access the
not yet created object. Now raise a proper exception instead.

Co-authored-by: default avatarMartin Pitt <>
parent 24cc09a7
......@@ -2058,6 +2058,10 @@ pygobject_get_dict(PyGObject *self, void *closure)
static PyObject *
pygobject_get_refcount(PyGObject *self, void *closure)
if (self->obj == NULL) {
PyErr_Format(PyExc_TypeError, "GObject instance is not yet created");
return NULL;
return PYGLIB_PyLong_FromLong(self->obj->ref_count);
......@@ -154,6 +154,17 @@ class TestReferenceCounting(unittest.TestCase):
self.assertEquals(obj.__grefcount__, 1)
def testUninitializedObject(self):
class Obj(GObject.GObject):
def __init__(self):
x = self.__grefcount__
super(Obj, self).__init__()
assert x >= 0 # quiesce pyflakes
# Accessing __grefcount__ before the object is initialized is wrong.
# Ensure we get a proper exception instead of a crash.
self.assertRaises(TypeError, Obj)
class A(GObject.GObject):
def __init__(self):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment