From d8109d9de81a776794965d4cc1093ca03bebd6af Mon Sep 17 00:00:00 2001 From: John Ehresman Date: Sun, 11 Apr 2010 22:14:39 -0400 Subject: Fix reference leak when using repr in exception strings --- gobject/gobjectmodule.c | 20 +++++++++++++------ gobject/pygobject.c | 51 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 21 deletions(-) (limited to 'gobject') diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index 73bfc3c..d431d6a 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -126,6 +126,8 @@ pyg_type_from_name (PyObject *self, PyObject *args) { const gchar *name; GType type; + PyObject *repr; + const char *repr_ptr; #if 0 if (PyErr_Warn(PyExc_DeprecationWarning, "gobject.type_from_name is deprecated; " @@ -137,9 +139,11 @@ pyg_type_from_name (PyObject *self, PyObject *args) type = _pyg_type_from_name(name); if (type != 0) return pyg_type_wrapper_new(type); + repr = PyObject_Repr((PyObject*)self); + repr_ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_RuntimeError, "%s: unknown type name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (repr_ptr ? repr_ptr : ""), name); + Py_CLEAR(repr); return NULL; } @@ -1921,9 +1925,11 @@ pyg_add_emission_hook(PyGObject *self, PyObject *args) } if (!g_signal_parse_name(name, gtype, &sigid, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char *repr_ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (repr_ptr ? repr_ptr : ""), name); + Py_CLEAR(repr); return NULL; } extra_args = PySequence_GetSlice(args, 3, len); @@ -1960,9 +1966,11 @@ pyg_remove_emission_hook(PyGObject *self, PyObject *args) } if (!g_signal_parse_name(name, gtype, &signal_id, NULL, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char *repr_ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (repr_ptr ? repr_ptr : ""), name); + Py_CLEAR(repr); return NULL; } diff --git a/gobject/pygobject.c b/gobject/pygobject.c index e14cf81..28a2a37 100644 --- a/gobject/pygobject.c +++ b/gobject/pygobject.c @@ -1468,9 +1468,11 @@ pygobject_connect(PyGObject *self, PyObject *args) if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj), &sigid, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char* ptr = (repr ? _PyUnicode_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (ptr ? ptr : ""), name); + Py_CLEAR(repr); return NULL; } extra_args = PySequence_GetSlice(args, 2, len); @@ -1517,9 +1519,11 @@ pygobject_connect_after(PyGObject *self, PyObject *args) if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj), &sigid, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char* ptr = (repr ? _PyUnicode_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (ptr ? ptr : ""), name); + Py_CLEAR(repr); return NULL; } extra_args = PySequence_GetSlice(args, 2, len); @@ -1566,9 +1570,11 @@ pygobject_connect_object(PyGObject *self, PyObject *args) if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj), &sigid, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char* ptr = (repr ? _PyUnicode_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (ptr ? ptr : ""), name); + Py_CLEAR(repr); return NULL; } extra_args = PySequence_GetSlice(args, 3, len); @@ -1615,9 +1621,11 @@ pygobject_connect_object_after(PyGObject *self, PyObject *args) if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj), &sigid, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char* ptr = (repr ? _PyUnicode_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (ptr ? ptr : ""), name); + Py_CLEAR(repr); return NULL; } extra_args = PySequence_GetSlice(args, 3, len); @@ -1714,9 +1722,11 @@ pygobject_emit(PyGObject *self, PyObject *args) if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj), &signal_id, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char* ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - name); + (ptr ? ptr : ""), name); + Py_CLEAR(repr); return NULL; } g_signal_query(signal_id, &query); @@ -1790,9 +1800,11 @@ pygobject_stop_emission(PyGObject *self, PyObject *args) if (!g_signal_parse_name(signal, G_OBJECT_TYPE(self->obj), &signal_id, &detail, TRUE)) { + PyObject *repr = PyObject_Repr((PyObject*)self); + const char* ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)self)), - signal); + (ptr ? ptr : ""), signal); + Py_CLEAR(repr); return NULL; } g_signal_stop_emission(self->obj, signal_id, detail); @@ -1938,8 +1950,11 @@ pygobject_disconnect_by_func(PyGObject *self, PyObject *args) closure = gclosure_from_pyfunc(self, pyfunc); if (!closure) { + PyObject *repr = PyObject_Repr(pyfunc); + const char* ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "nothing connected to %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)pyfunc))); + (ptr ? ptr : "")); + Py_CLEAR(repr); return NULL; } @@ -1970,8 +1985,11 @@ pygobject_handler_block_by_func(PyGObject *self, PyObject *args) closure = gclosure_from_pyfunc(self, pyfunc); if (!closure) { + PyObject *repr = PyObject_Repr(pyfunc); + const char* ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "nothing connected to %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)pyfunc))); + (ptr ? ptr : "")); + Py_CLEAR(repr); return NULL; } @@ -2002,8 +2020,11 @@ pygobject_handler_unblock_by_func(PyGObject *self, PyObject *args) closure = gclosure_from_pyfunc(self, pyfunc); if (!closure) { + PyObject *repr = PyObject_Repr(pyfunc); + const char* ptr = (repr ? PyString_AsString(repr) : ""); PyErr_Format(PyExc_TypeError, "nothing connected to %s", - _PyUnicode_AsString(PyObject_Repr((PyObject*)pyfunc))); + (ptr ? ptr : "")); + Py_CLEAR(repr); return NULL; } -- cgit