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

Guido van Rossum guido@python.org
Thu, 20 Dec 2001 08:39:04 -0500


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

Modified Files:
	spreadmodule.c 
Log Message:
While msg_type is an int16 (== short) according to the Spread headers,
there's no reason to declare all function arguments, local variables
and Python object members containing a msg_type value as a short.  I
changed all function prototypes and the RegularMsg object to declare
it as an int.  I found several bugs: the RegularMsg_memberlist
referenced it using T_INT instead of T_SHORT, and the
PyArg_ParseTuple() call in mailbox_multicast() didn't have an '&' in
front of it.  Since sp_func.h declares prototypes for all Spread
functions, an int passed where a function requires an int16 is
automatically shrunk.  (Except that the C std requires that all
arguments have at least the size of an int, so an int16 argument is
widened by the compiler to an int anyway, I believe.)

Also fixed indentation glitches caused by Py_{BEGIN,END}_ALLOW_THREADS
-- CC-mode doesn't indent the code inside those or after the
Py_END... right.


=== Packages/Spread/spreadmodule.c 1.10 => 1.11 ===
 static PyObject *SpreadError;
 static PyObject *spread_error(int);
-static void spread_too_short_error(int, int, short, int);
+static void spread_too_short_error(int, int, int, int);
 
 #define MAX_GROUPS 10
 #define MSG_SIZE 8 * 1024
@@ -35,7 +35,7 @@
 	PyObject_HEAD
 	PyObject *sender;
 	PyObject *groups;
-	short msg_type; /* sp.h: #define int16 short*/
+	int msg_type;
 	int endian;
 	PyObject *message;
 	int truncated;
@@ -259,7 +259,7 @@
 
 static PyObject *
 new_regular_msg(PyObject *sender, int num_groups,
-		char (*groups)[MAX_GROUP_NAME], short msg_type,
+		char (*groups)[MAX_GROUP_NAME], int msg_type,
 		int endian, PyObject *message, int size)
 {
 	RegularMsg *self;
@@ -403,10 +403,10 @@
 	if (!PyArg_ParseTuple(args, "s:join", &group))
 		return NULL;
 	Py_BEGIN_ALLOW_THREADS
-		err = SP_join(self->mbox, group);
+	err = SP_join(self->mbox, group);
 	Py_END_ALLOW_THREADS
-		if (err < 0)
-			return spread_error(err);
+	if (err < 0)
+		return spread_error(err);
 	Py_INCREF(Py_None);
 	return Py_None;
 }
@@ -420,10 +420,10 @@
 	if (!PyArg_ParseTuple(args, "s:leave", &group))
 		return NULL;
 	Py_BEGIN_ALLOW_THREADS
-		err = SP_leave(self->mbox, group);
+	err = SP_leave(self->mbox, group);
 	Py_END_ALLOW_THREADS
-		if (err < 0)
-			return spread_error(err);
+	if (err < 0)
+		return spread_error(err);
 	Py_INCREF(Py_None);
 	return Py_None;
 }
@@ -539,11 +539,11 @@
 mailbox_multicast(MailboxObject *self, PyObject *args)
 {
 	int svc_type, bytes, msg_len;
-	short msg_type = 0;
+	int msg_type = 0;
 	char *group, *msg;
 
-	if (!PyArg_ParseTuple(args, "iss#|h:multicast",
-			      &svc_type, &group, &msg, &msg_len, msg_type))
+	if (!PyArg_ParseTuple(args, "iss#|i:multicast",
+			      &svc_type, &group, &msg, &msg_len, &msg_type))
 		return NULL;
 
 	/* XXX This doesn't check that svc_type is set to exactly on of
@@ -554,11 +554,11 @@
 	}
 
 	Py_BEGIN_ALLOW_THREADS
-		bytes = SP_multicast(self->mbox, svc_type, group, msg_type,
-				     msg_len, msg);
+	bytes = SP_multicast(self->mbox, svc_type, group, msg_type,
+			     msg_len, msg);
 	Py_END_ALLOW_THREADS
-		if (bytes < 0)
-			return spread_error(bytes);
+	if (bytes < 0)
+		return spread_error(bytes);
 	return PyInt_FromLong(bytes);
 }
 
@@ -661,30 +661,30 @@
 	memset(PyString_AS_STRING(group_name), '\0', MAX_GROUP_NAME);
 
 	Py_BEGIN_ALLOW_THREADS
-		ret = SP_connect(spread_name, private_name, priority,
-				 membership, &_mbox,
-				 PyString_AS_STRING(group_name));
+	ret = SP_connect(spread_name, private_name, priority,
+			 membership, &_mbox,
+			 PyString_AS_STRING(group_name));
 	Py_END_ALLOW_THREADS
 
-		if (ret == ACCEPT_SESSION) {
-			mbox = new_mailbox(_mbox);
-			if (mbox == NULL) {
-				SP_disconnect(_mbox);
-				return NULL;
-			}
-			if (_PyString_Resize(
-				&group_name,
-				strlen(PyString_AS_STRING(group_name))) < 0)
-			{
-				SP_disconnect(_mbox);
-				Py_DECREF(mbox);
-				return NULL;
-			}
-			mbox->private_group = group_name;
-			return (PyObject*)mbox;
+	if (ret == ACCEPT_SESSION) {
+		mbox = new_mailbox(_mbox);
+		if (mbox == NULL) {
+			SP_disconnect(_mbox);
+			return NULL;
 		}
-		else
-			return spread_error(ret);
+		if (_PyString_Resize(
+			&group_name,
+			strlen(PyString_AS_STRING(group_name))) < 0)
+		{
+			SP_disconnect(_mbox);
+			Py_DECREF(mbox);
+			return NULL;
+		}
+		mbox->private_group = group_name;
+		return (PyObject*)mbox;
+	}
+	else
+		return spread_error(ret);
 }
 
 /* List of functions defined in the module */
@@ -703,7 +703,7 @@
 
 /* Three error handling routines below */
 
-static void spread_too_short_error(int err, int svc_type, short msg_type,
+static void spread_too_short_error(int err, int svc_type, int msg_type,
 				   int needed)
 {
 	PyObject *val;
@@ -722,7 +722,7 @@
 	if (!reason)
 		return;
 
-	val = Py_BuildValue("iNihi", err, reason, svc_type, msg_type,
+	val = Py_BuildValue("iNiii", err, reason, svc_type, msg_type,
 			    needed < 0 ? -needed : 0);
 	if (val)
 		PyErr_SetObject(SpreadError, val);