[Zope-CVS] CVS: Packages/Spread - spreadmodule.c:1.5

Fred Drake Jr fdrake@acm.org
Wed, 19 Dec 2001 17:56:27 -0500


Update of /cvs-repository/Packages/Spread
In directory cvs.zope.org:/tmp/cvs-serv31658

Modified Files:
	spreadmodule.c 
Log Message:
Clean up the memory management in the constructors for RegularMsg and
MembershipMsg.  Simplify a couple of getattr handlers that use empty method
lists.


=== Packages/Spread/spreadmodule.c 1.4 => 1.5 ===
     int i;
 
-    /* XXX Need to re-work the error returns */
-    
     self = PyObject_New(MembershipMsg, &MembershipMsg_Type);
-    if (!self)
+    if (self == NULL)
 	return NULL;
     self->reason = type & CAUSED_BY_MASK; /* from sp.h defines */
+    assert(group != NULL);
     Py_INCREF(group);
     self->group = group;
+    self->members = NULL;
+    self->extra = NULL;
     self->group_id = (PyObject *)new_group_id(extra->gid);
     if (self->group_id == NULL) {
-	/* XXX */
+	Py_DECREF((PyObject *) self);
 	return NULL;
     }
     self->members = PyTuple_New(num_members);
-    if (self->members == NULL)
-	return NULL; /* XXX dealloc object */
-
+    if (self->members == NULL) {
+	Py_DECREF((PyObject *) self);
+	return NULL;
+    }
     for (i = 0; i < num_members; ++i) {
 	PyObject *s = PyString_FromString(members[i]);
 	if (!s) {
-	    Py_DECREF(self->members);
+	    Py_DECREF((PyObject *) self);
 	    return NULL;
 	}
 	PyTuple_SET_ITEM(self->members, i, s);
@@ -187,7 +189,7 @@
 	PyObject *s = PyString_FromStringAndSize(extra->members[i],
 						 MAX_GROUP_NAME);
 	if (!s) {
-	    Py_DECREF(self->extra);
+	    Py_DECREF((PyObject *) self);
 	    return NULL;
 	}
 	_PyString_Resize(&s, strlen(PyString_AS_STRING(s)));
@@ -199,14 +201,12 @@
 static void
 membership_msg_dealloc(MembershipMsg *self)
 {
-    Py_DECREF(self->group);
-    Py_DECREF(self->members);
+    Py_XDECREF(self->group);
+    Py_XDECREF(self->members);
+    Py_XDECREF(self->extra);
+    Py_XDECREF(self->group_id);
 }
 
-static PyMethodDef MembershipMsg_methods[] = {
-	{NULL,		NULL}		/* sentinel */
-};
-
 #define OFF(x) offsetof(MembershipMsg, x)
 
 static struct memberlist MembershipMsg_memberlist[] = {
@@ -223,12 +223,6 @@
 static PyObject *
 membership_msg_getattr(MembershipMsg *self, char *name)
 {
-    PyObject *meth;
-    
-    meth = Py_FindMethod(MembershipMsg_methods, (PyObject *)self, name);
-    if (meth)
-	return meth;
-    PyErr_Clear();
     return PyMember_Get((char *)self, MembershipMsg_memberlist, name);
 }
 
@@ -264,15 +258,18 @@
     if (self == NULL)
 	return NULL;
 
+    self->message = NULL;
+    self->sender = NULL;
     self->groups = PyTuple_New(num_groups);
     if (self->groups == NULL) {
-	return NULL; /* XXX dealloc object */
+        Py_DECREF((PyObject *) self);
+	return NULL;
     }
     for (i = 0; i < num_groups; ++i) {
 	PyObject *s = PyString_FromString(groups[i]);
 	if (!s) {
-	    Py_DECREF(self->groups);
-	    return NULL; /* XXX dealloc object */
+	    Py_DECREF(self);
+	    return NULL;
 	}
 	PyTuple_SET_ITEM(self->groups, i, s);
     }
@@ -293,24 +290,20 @@
 static void
 regular_msg_dealloc(RegularMsg *self)
 {
-    Py_DECREF(self->sender);
-    Py_DECREF(self->groups);
-    Py_DECREF(self->message);
+    Py_XDECREF(self->sender);
+    Py_XDECREF(self->groups);
+    Py_XDECREF(self->message);
 }
 
-static PyMethodDef RegularMsg_methods[] = {
-	{NULL,		NULL}		/* sentinel */
-};
-
 #define OFF(x) offsetof(RegularMsg, x)
 
 static struct memberlist RegularMsg_memberlist[] = {
-    {"msg_type",T_INT,		OFF(msg_type)},
-    {"endian",	T_INT,		OFF(endian)},
+    {"msg_type", T_INT,		OFF(msg_type)},
+    {"endian",   T_INT,		OFF(endian)},
     {"truncated",T_INT,		OFF(truncated)},
-    {"sender",	T_OBJECT,	OFF(sender)},
-    {"groups",	T_OBJECT,	OFF(groups)},
-    {"message",	T_OBJECT,	OFF(message)},
+    {"sender",   T_OBJECT,	OFF(sender)},
+    {"groups",   T_OBJECT,	OFF(groups)},
+    {"message",	 T_OBJECT,	OFF(message)},
     {NULL}
 };
 
@@ -319,12 +312,6 @@
 static PyObject *
 regular_msg_getattr(RegularMsg *self, char *name)
 {
-    PyObject *meth;
-    
-    meth = Py_FindMethod(RegularMsg_methods, (PyObject *)self, name);
-    if (meth)
-	return meth;
-    PyErr_Clear();
     return PyMember_Get((char *)self, RegularMsg_memberlist, name);
 }
 
@@ -710,7 +697,7 @@
     if (!reason)
 	return;
 
-    val = Py_BuildValue("iOihi", err, reason, svc_type, msg_type, 
+    val = Py_BuildValue("iNihi", err, reason, svc_type, msg_type, 
 			needed < 0 ? -needed : 0);
     if (val)
 	PyErr_SetObject(SpreadError, val);
@@ -727,7 +714,7 @@
     if (code == NULL)
 	return NULL;
 
-    /* It would be better is spread provided an API function to map
+    /* It would be better if spread provided an API function to map
        these to error strings.  SP_error() merely prints a string,
        which is useful in only limited circumstances. */
     switch (err) {
@@ -805,7 +792,7 @@
 DL_EXPORT(void)
 initspread(void)
 {
-    PyObject *m, *d;
+    PyObject *m;
 
     /* Create the module and add the functions */
     m = Py_InitModule("spread", spread_methods);
@@ -826,9 +813,13 @@
 		       (PyObject *)&MembershipMsg_Type);
 
     /* Add some symbolic constants to the module */
-    d = PyModule_GetDict(m);
-    SpreadError = PyErr_NewException("spread.error", NULL, NULL);
-    PyDict_SetItemString(d, "error", SpreadError);
+    if (SpreadError == NULL) {
+        SpreadError = PyErr_NewException("spread.error", NULL, NULL);
+        if (SpreadError == NULL)
+            return;
+    }
+    Py_INCREF(SpreadError);
+    PyModule_AddObject(m, "error", SpreadError);
 
     /* programmatically generated from sp.h */
     PyModule_AddIntConstant(m, "LOW_PRIORITY", LOW_PRIORITY);
@@ -877,4 +868,3 @@
     PyModule_AddIntConstant(m, "GROUPS_TOO_SHORT", GROUPS_TOO_SHORT);
     PyModule_AddIntConstant(m, "MESSAGE_TOO_LONG", MESSAGE_TOO_LONG);
 }
-