[Python-checkins] CVS: python/dist/src/Modules socketmodule.c,1.187,1.188

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 25 Oct 2001 20:25:03 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv30548

Modified Files:
	socketmodule.c 
Log Message:
Add sendall() method, which loops until all data is written or an
error occurs, and doesn't return a count.  (This is my second patch
from SF patch #474307, with small change to the docstring for send().)

2.1.2 "bugfix" candidate.


Index: socketmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v
retrieving revision 1.187
retrieving revision 1.188
diff -C2 -d -r1.187 -r1.188
*** socketmodule.c	2001/10/25 09:04:03	1.187
--- socketmodule.c	2001/10/26 03:25:00	1.188
***************
*** 67,70 ****
--- 67,71 ----
  - s.recvfrom(buflen [,flags]) --> string, sockaddr
  - s.send(string [,flags]) --> nbytes
+ - s.sendall(string [,flags]) # tries to send everything in a loop
  - s.sendto(string, [flags,] sockaddr) --> nbytes
  - s.setblocking(0 | 1) --> None
***************
*** 1554,1562 ****
  
  static char send_doc[] =
! "send(data[, flags])\n\
  \n\
  Send a data string to the socket.  For the optional flags\n\
! argument, see the Unix manual.";
  
  
  /* s.sendto(data, [flags,] sockaddr) method */
--- 1555,1598 ----
  
  static char send_doc[] =
! "send(data[, flags]) -> count\n\
  \n\
  Send a data string to the socket.  For the optional flags\n\
! argument, see the Unix manual.  Return the number of bytes\n\
! sent; this may be less than len(data) if the network is busy.";
! 
! 
! /* s.sendall(data [,flags]) method */
! 
! static PyObject *
! PySocketSock_sendall(PySocketSockObject *s, PyObject *args)
! {
! 	char *buf;
! 	int len, n, flags = 0, total = 0;
! 	if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
! 		return NULL;
! 	Py_BEGIN_ALLOW_THREADS
! 	do {
! 		n = send(s->sock_fd, buf, len, flags);
! 		if (n < 0)
! 			break;
! 		total += n;
! 		buf += n;
! 		len -= n;
! 	} while (len > 0);
! 	Py_END_ALLOW_THREADS
! 	if (n < 0)
! 		return PySocket_Err();
! 	Py_INCREF(Py_None);
! 	return Py_None;
! }
  
+ static char sendall_doc[] =
+ "sendall(data[, flags])\n\
+ \n\
+ Send a data string to the socket.  For the optional flags\n\
+ argument, see the Unix manual.  This calls send() repeatedly\n\
+ until all data is sent.  If an error occurs, it's impossible\n\
+ to tell how much data has been sent.";
+ 
  
  /* s.sendto(data, [flags,] sockaddr) method */
***************
*** 1659,1662 ****
--- 1695,1700 ----
  	{"send",	(PyCFunction)PySocketSock_send, METH_VARARGS,
  			send_doc},
+ 	{"sendall",	(PyCFunction)PySocketSock_sendall, METH_VARARGS,
+ 			sendall_doc},
  	{"sendto",	(PyCFunction)PySocketSock_sendto, METH_VARARGS,
  			sendto_doc},
***************
*** 2693,2697 ****
--- 2731,2737 ----
  		return NULL;
  
+ 	Py_BEGIN_ALLOW_THREADS
  	len = SSL_write(self->ssl, data, len);
+ 	Py_END_ALLOW_THREADS
  	if (len > 0)
  		return PyInt_FromLong(len);
***************
*** 2718,2722 ****
--- 2758,2764 ----
  		return NULL;
  
+ 	Py_BEGIN_ALLOW_THREADS
  	count = SSL_read(self->ssl, PyString_AsString(buf), len);
+ 	Py_END_ALLOW_THREADS
   	if (count <= 0) {
  		Py_DECREF(buf);