From 7222a37a4b4955fe6c1dcc86e0eb798d653711e8 Mon Sep 17 00:00:00 2001 From: Simon Feltman Date: Wed, 15 Jan 2014 12:51:57 -0800 Subject: Fix reference sharing of gi sub-modules in Python 2 Ensure we add a new reference to sub-modules added to gi._gi. This may have caused GC errors upon exiting the Python process since a reference was shared by sys.modules and gi._gi. https://bugzilla.gnome.org/show_bug.cgi?id=722274 --- gi/gimodule.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gi/gimodule.c b/gi/gimodule.c index e0d9878b..ef3e2050 100644 --- a/gi/gimodule.c +++ b/gi/gimodule.c @@ -643,6 +643,14 @@ PYGLIB_MODULE_START(_gi, "_gi") if (_glib_module == NULL) { return PYGLIB_MODULE_ERROR_RETURN; } + /* In Python 2.x, pyglib_..._module_create returns a borrowed reference and + * PyModule_AddObject steals a reference. Ensure we don't share a reference + * between sys.modules and gi._gi._glib by incrementing the ref count here. + * Note that we don't add this to the PYGLIB_MODULE_START macro because that + * would cause a leak for the main module gi._gi */ + if (PY_MAJOR_VERSION < 3) { + Py_INCREF (_glib_module); + } PyModule_AddObject (module, "_glib", _glib_module); PyModule_AddStringConstant(module, "__package__", "gi._gi"); @@ -650,6 +658,9 @@ PYGLIB_MODULE_START(_gi, "_gi") if (_gobject_module == NULL) { return PYGLIB_MODULE_ERROR_RETURN; } + if (PY_MAJOR_VERSION < 3) { + Py_INCREF (_gobject_module); + } PyModule_AddObject (module, "_gobject", _gobject_module); PyModule_AddStringConstant(module, "__package__", "gi._gi"); -- cgit v1.2.1