[pygobject/py3k: 2/20] Fix reference leak when using repr in exception strings



commit d8109d9de81a776794965d4cc1093ca03bebd6af
Author: John Ehresman <jpe wingware com>
Date:   Sun Apr 11 22:14:39 2010 -0400

    Fix reference leak when using repr in exception strings

 glib/glibmodule.c       |   10 +++++++-
 gobject/gobjectmodule.c |   20 ++++++++++++-----
 gobject/pygobject.c     |   51 +++++++++++++++++++++++++++++++++-------------
 3 files changed, 58 insertions(+), 23 deletions(-)
---
diff --git a/glib/glibmodule.c b/glib/glibmodule.c
index f794d5d..c529f87 100644
--- a/glib/glibmodule.c
+++ b/glib/glibmodule.c
@@ -558,9 +558,12 @@ static PyObject*
 pyglib_set_application_name(PyObject *self, PyObject *arg)
 {
     if (!PyString_Check(arg)) {
+	PyObject *repr = PyObject_Repr(arg);
+	const char *repr_ptr = (repr ? PyString_AsString(repr) : "<?>");
 	PyErr_Format(PyExc_TypeError,
 		     "first argument must be a string, not '%s'",
-		     PyString_AS_STRING(PyObject_Repr(arg)));
+		     (repr_ptr ? repr_ptr : "<?>"));
+	Py_CLEAR(repr);
 	return NULL;
     }
     g_set_application_name(PyString_AS_STRING(arg));
@@ -585,9 +588,12 @@ static PyObject*
 pyglib_set_prgname(PyObject *self, PyObject *arg)
 {
     if (!PyString_Check(arg)) {
+	PyObject *repr = PyObject_Repr(arg);
+	const char *repr_ptr = (repr ? PyString_AsString(repr) : "<?>");
 	PyErr_Format(PyExc_TypeError,
 		     "first argument must be a string, not '%s'",
-		     PyString_AS_STRING(PyObject_Repr(arg)));
+		     (repr_ptr ? repr_ptr : "<?>"));
+	Py_CLEAR(repr);
 	return NULL;
     }
     g_set_prgname(PyString_AS_STRING(arg));
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;
     }
     



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