diff Python/exceptions.c @ 17268:d467d184b429 2.0

- exceptions.c - make_class() Added a "goto finally" so that if populate_methods() fails, the return status will be -1 (failure) instead of 0 (success). fini_exceptions(): When decref'ing the static pointers to the exception classes, clear out their dictionaries too. This breaks a cycle from class->dict->method->class and allows the classes with unbound methods to be reclaimed. This plugs a large memory leak in a common Py_Initialize()/dosomething/Py_Finalize() loop.
author Moshe Zadka <moshez@math.huji.ac.il>
date Fri, 30 Mar 2001 21:01:09 +0000
parents 03401af36b5c
children
line wrap: on
line diff
--- a/Python/exceptions.c	Fri Mar 30 20:44:51 2001 +0000
+++ b/Python/exceptions.c	Fri Mar 30 21:01:09 2001 +0000
@@ -168,6 +168,7 @@
     if (populate_methods(*klass, dict, methods)) {
 	Py_DECREF(*klass);
 	*klass = NULL;
+	goto finally;
     }
 
     status = 0;
@@ -1096,6 +1097,14 @@
     PyExc_MemoryErrorInst = NULL;
 
     for (i=0; exctable[i].name; i++) {
+	/* clear the class's dictionary, freeing up circular references
+	 * between the class and its methods.
+	 */
+	PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
+	PyDict_Clear(cdict);
+	Py_DECREF(cdict);
+
+	/* Now decref the exception class */
 	Py_XDECREF(*exctable[i].exc);
 	*exctable[i].exc = NULL;
     }