diff Modules/socketmodule.c @ 38580:a114003aa3d3 legacy-trunk

Fixes in struct and socket from merge reviews. - Following Guido's comments, renamed * pack_to -> pack_into * recv_buf -> recv_into * recvfrom_buf -> recvfrom_into - Made fixes to _struct.c according to Neal Norwitz comments on the checkins list. - Converted some ints into the appropriate -- I hope -- ssize_t and size_t.
author Martin Blais <blais@furius.ca>
date Sun, 04 Jun 2006 13:49:49 +0000
parents 8dfba087dc07
children 1e38ff81c8a9
line wrap: on
line diff
--- a/Modules/socketmodule.c	Sun Jun 04 12:59:59 2006 +0000
+++ b/Modules/socketmodule.c	Sun Jun 04 13:49:49 2006 +0000
@@ -104,9 +104,9 @@
 listen(n) -- start listening for incoming connections\n\
 makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
 recv(buflen[, flags]) -- receive data\n\
-recv_buf(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
+recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
 recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
-recvfrom_buf(buffer[, nbytes, [, flags])\n\
+recvfrom_into(buffer[, nbytes, [, flags])\n\
   -- receive data and sender\'s address (into a buffer)\n\
 sendall(data[, flags]) -- send all data\n\
 send(data[, flags]) -- send data, may not send all of it\n\
@@ -2139,17 +2139,18 @@
 #endif /* NO_DUP */
 
 /*
- * This is the guts of the recv() and recv_buf() methods, which reads into a
+ * This is the guts of the recv() and recv_into() methods, which reads into a
  * char buffer.  If you have any inc/def ref to do to the objects that contain
  * the buffer, do it in the caller.  This function returns the number of bytes
  * succesfully read.  If there was an error, it returns -1.  Note that it is
  * also possible that we return a number of bytes smaller than the request
  * bytes.
  */
-static int
+static ssize_t
 sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
 {
-        int timeout, outlen = 0;
+        ssize_t outlen = 0;
+        int timeout;
 #ifdef __VMS
 	int remaining, nread;
 	char *read_buf;
@@ -2225,7 +2226,8 @@
 static PyObject *
 sock_recv(PySocketSockObject *s, PyObject *args)
 {
-	int recvlen, flags = 0, outlen;
+	int recvlen, flags = 0;
+        ssize_t outlen;
 	PyObject *buf;
 
 	if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
@@ -2243,7 +2245,7 @@
 		return NULL;
 
 	/* Call the guts */
-	outlen = sock_recv_guts(s, PyString_AsString(buf), recvlen, flags);
+	outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);
 	if (outlen < 0) {
 		/* An error occured, release the string and return an
 		   error. */
@@ -2270,19 +2272,20 @@
 the remote end is closed and all data is read, return the empty string.");
 
 
-/* s.recv_buf(buffer, [nbytes [,flags]]) method */
+/* s.recv_into(buffer, [nbytes [,flags]]) method */
 
 static PyObject*
-sock_recv_buf(PySocketSockObject *s, PyObject *args, PyObject *kwds)
+sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
 {
 	static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
 
-	int recvlen = 0, flags = 0, readlen;
+	int recvlen = 0, flags = 0;
+        ssize_t readlen;
 	char *buf;
 	int buflen;
 
 	/* Get the buffer's memory */
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#|ii:recv", kwlist,
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv", kwlist,
 					 &buf, &buflen, &recvlen, &flags))
 		return NULL;
 	assert(buf != 0 && buflen > 0);
@@ -2313,11 +2316,11 @@
 
 	/* Return the number of bytes read.  Note that we do not do anything
 	   special here in the case that readlen < recvlen. */
-	return PyInt_FromLong(readlen);
+	return PyInt_FromSsize_t(readlen);
 }
 
-PyDoc_STRVAR(recv_buf_doc,
-"recv_buf(buffer, [nbytes[, flags]]) -> nbytes_read\n\
+PyDoc_STRVAR(recv_into_doc,
+"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
 \n\
 A version of recv() that stores its data into a buffer rather than creating \n\
 a new string.  Receive up to buffersize bytes from the socket.  If buffersize \n\
@@ -2327,7 +2330,7 @@
 
 
 /*
- * This is the guts of the recv() and recv_buf() methods, which reads into a
+ * This is the guts of the recv() and recv_into() methods, which reads into a
  * char buffer.  If you have any inc/def ref to do to the objects that contain
  * the buffer, do it in the caller.  This function returns the number of bytes
  * succesfully read.  If there was an error, it returns -1.  Note that it is
@@ -2337,12 +2340,13 @@
  * 'addr' is a return value for the address object.  Note that you must decref
  * it yourself.
  */
-static int
+static ssize_t
 sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
 		   PyObject** addr)
 {
 	sock_addr_t addrbuf;
-	int n = 0, timeout;
+	int timeout;
+	ssize_t n = 0;
 	socklen_t addrlen;
 
 	*addr = NULL;
@@ -2398,7 +2402,8 @@
 	PyObject *buf = NULL;
 	PyObject *addr = NULL;
 	PyObject *ret = NULL;
-	int recvlen, outlen, flags = 0;
+	int recvlen, flags = 0;
+        ssize_t outlen;
 
 	if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
 		return NULL;
@@ -2435,21 +2440,22 @@
 Like recv(buffersize, flags) but also return the sender's address info.");
 
 
-/* s.recvfrom_buf(buffer[, nbytes [,flags]]) method */
+/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
 
 static PyObject *
-sock_recvfrom_buf(PySocketSockObject *s, PyObject *args, PyObject* kwds)
+sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
 {
 	static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
 
-	int recvlen = 0, flags = 0, readlen;
+	int recvlen = 0, flags = 0;
+        ssize_t readlen;
 	char *buf;
 	int buflen;
 
 	PyObject *addr = NULL;
 	PyObject *ret = NULL;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#|ii:recvfrom", kwlist,
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom", kwlist,
 					 &buf, &buflen, &recvlen, &flags))
 		return NULL;
 	assert(buf != 0 && buflen > 0);
@@ -2467,22 +2473,19 @@
 	readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
 	if (readlen < 0) {
 		/* Return an error */
-		goto finally;
+		Py_XDECREF(addr);
+		return NULL;
 	}
 
 	/* Return the number of bytes read and the address.  Note that we do
 	   not do anything special here in the case that readlen < recvlen. */
-	ret = Py_BuildValue("lO", readlen, addr);
-	
-finally:
-	Py_XDECREF(addr);
-	return ret;
+ 	return Py_BuildValue("lO", readlen, addr);
 }
 
-PyDoc_STRVAR(recvfrom_buf_doc,
-"recvfrom_buf(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
+PyDoc_STRVAR(recvfrom_into_doc,
+"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
 \n\
-Like recv_buf(buffer[, nbytes[, flags]]) but also return the sender's address info.");
+Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
 
 
 /* s.send(data [,flags]) method */
@@ -2711,12 +2714,12 @@
 #endif
 	{"recv",	  (PyCFunction)sock_recv, METH_VARARGS,
 			  recv_doc},
-	{"recv_buf",	  (PyCFunction)sock_recv_buf, METH_VARARGS | METH_KEYWORDS,
-			  recv_buf_doc},
+	{"recv_into",	  (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
+			  recv_into_doc},
 	{"recvfrom",	  (PyCFunction)sock_recvfrom, METH_VARARGS,
 			  recvfrom_doc},
-	{"recvfrom_buf",  (PyCFunction)sock_recvfrom_buf, METH_VARARGS | METH_KEYWORDS,
-			  recvfrom_buf_doc},
+	{"recvfrom_into",  (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
+			  recvfrom_into_doc},
 	{"send",	  (PyCFunction)sock_send, METH_VARARGS,
 			  send_doc},
 	{"sendall",	  (PyCFunction)sock_sendall, METH_VARARGS,