[Zope3-checkins] CVS: ZODB4/Persistence - cPersistence.c:1.24 cPersistence.h:1.8

Jeremy Hylton jeremy@zope.com
Thu, 17 Oct 2002 15:53:13 -0400


Update of /cvs-repository/ZODB4/Persistence
In directory cvs.zope.org:/tmp/cvs-serv18211/Persistence

Modified Files:
	cPersistence.c cPersistence.h 
Log Message:
Repair memory leaks in cPersistence C API (collector #83).

The _PyPersist_Load() and _PyPersist_RegistrDataManager() functions
were leaking their return values.  Fixed by changing them to return
int instead of PyObject *, as suggested by Phillip J. Eby.  This may
require apps written against the C API to change.

Some of the macros defined in cPersistenceAPI.h now return non-zero
ints on success, zero on failure.


=== ZODB4/Persistence/cPersistence.c 1.23 => 1.24 ===
--- ZODB4/Persistence/cPersistence.c:1.23	Thu Oct 10 17:54:42 2002
+++ ZODB4/Persistence/cPersistence.c	Thu Oct 17 15:53:12 2002
@@ -36,24 +36,22 @@
 
 static PyObject *s_register = NULL;
 
-PyObject *
+int
 _PyPersist_RegisterDataManager(PyPersistObject *self) 
 {
     PyObject *meth, *arg, *result;
 
-    if (self->po_dm == NULL) {
-	Py_INCREF(Py_None);
-	return Py_None;
-    }
+    if (self->po_dm == NULL)
+	return 0;
     if (s_register == NULL) 
 	s_register = PyString_InternFromString("register");
     meth = PyObject_GetAttr((PyObject *)self->po_dm, s_register);
     if (meth == NULL)
-	return NULL;
+	return 0;
     arg = PyTuple_New(1);
     if (arg == NULL) {
 	Py_DECREF(meth);
-	return NULL;
+	return 0;
     }
     Py_INCREF(self);
     PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
@@ -62,38 +60,46 @@
     Py_DECREF(meth);
     if (self->po_state == UPTODATE || self->po_state == STICKY)
 	self->po_state = CHANGED;
-    return result;
+    if (result) {
+	Py_DECREF(result);
+	return 1;
+    }
+    else
+	return 0;
 }
 
 /* A helper function that loads an object's state from its data manager.
 */
 
-PyObject *
+int
 _PyPersist_Load(PyPersistObject *self) 
 {
     static PyObject *s_setstate = NULL;
     PyObject *meth, *arg, *result;
 
-    if (self->po_dm == NULL) {
-	Py_INCREF(Py_None);
-	return Py_None;
-    }
+    if (self->po_dm == NULL)
+	return 0;
     if (s_setstate == NULL) 
 	s_setstate = PyString_InternFromString("setstate");
     meth = PyObject_GetAttr((PyObject *)self->po_dm, s_setstate);
     if (meth == NULL)
-	return NULL;
+	return 0;
     arg = PyTuple_New(1);
     if (arg == NULL) {
 	Py_DECREF(meth);
-	return NULL;
+	return 0;
     }
     Py_INCREF(self);
     PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
     result = PyObject_Call(meth, arg, NULL);
     Py_DECREF(arg);
     Py_DECREF(meth);
-    return result;
+    if (result) {
+	Py_DECREF(result);
+	return 1;
+    }
+    else 
+	return 0;
 }
 
 /* A helper function to set the atime from the current time.  The
@@ -196,8 +202,9 @@
 static PyObject *
 persist_activate(PyPersistObject *self)
 {
-    if (self->po_state == GHOST && self->po_dm)
-	return _PyPersist_Load((PyPersistObject *)self);
+    if (self->po_state == GHOST && self->po_dm
+	&& !_PyPersist_Load((PyPersistObject *)self))
+	return NULL;
     Py_INCREF(Py_None);
     return Py_None;
 }
@@ -287,7 +294,7 @@
 	if (newstate == CHANGED_TRUE || newstate == CHANGED_FALSE) {
 	    /* Turn a ghost into a real object. */
 	    self->po_state = CHANGED;
-	    if (_PyPersist_Load((PyPersistObject *)self) == NULL)
+	    if (!_PyPersist_Load((PyPersistObject *)self))
 		return -1;
 	    if (newstate == CHANGED_TRUE)
 		self->po_state = CHANGED;
@@ -395,7 +402,7 @@
 	       already registered. 
 	    */
 	    self->po_state = CHANGED;
-	    if (_PyPersist_Load((PyPersistObject *)self) == NULL) {
+	    if (!_PyPersist_Load((PyPersistObject *)self)) {
 		call_p_deactivate(self);
 		self->po_state = GHOST;
 		return NULL;
@@ -452,7 +459,7 @@
 				"attempt to modify unrevivable ghost");
 		return -1;
 	    }
-	    if (_PyPersist_Load((PyPersistObject *)self) == NULL)
+	    if (!_PyPersist_Load((PyPersistObject *)self))
 		return -1;
 	}
 	/* If the object is marked as UPTODATE then it must be


=== ZODB4/Persistence/cPersistence.h 1.7 => 1.8 ===
--- ZODB4/Persistence/cPersistence.h:1.7	Thu Oct 10 17:36:18 2002
+++ ZODB4/Persistence/cPersistence.h	Thu Oct 17 15:53:12 2002
@@ -44,8 +44,8 @@
     PyPersist_HEAD
 } PyPersistObject;
 
-extern PyObject *_PyPersist_Load(PyPersistObject *);
-extern PyObject *_PyPersist_RegisterDataManager(PyPersistObject *);
+extern int _PyPersist_Load(PyPersistObject *);
+extern int _PyPersist_RegisterDataManager(PyPersistObject *);
 extern void _PyPersist_SetATime(PyPersistObject *);
 
 /* A struct to encapsulation the PyPersist C API for use by other
@@ -54,8 +54,8 @@
 
 typedef struct {
     PyTypeObject *base_type;
-    PyObject *(*load)(PyPersistObject *);
-    PyObject *(*reg_mgr)(PyPersistObject *);
+    int (*load)(PyPersistObject *);
+    int (*reg_mgr)(PyPersistObject *);
     void (*set_atime)(PyPersistObject *);
 } PyPersist_C_API_struct;