diff -r -c python.timeout/python/dist/src/Doc/lib/libsocket.tex python.orig/python/dist/src/Doc/lib/libsocket.tex *** python.timeout/python/dist/src/Doc/lib/libsocket.tex Sun May 12 07:57:20 2002 --- python.orig/python/dist/src/Doc/lib/libsocket.tex Sat Dec 22 14:07:58 2001 *************** *** 514,532 **** block until they can proceed. \end{methoddesc} - \begin{methoddesc}[socket]{settimeout}{value} - Set a timeout on blocking socket operations. Socket operations will - raise an \exception{error} exception if the timeout period \var{value} - has elapsed before the operation has completed. Setting a timeout of - \var{None} disables timeouts on socket operations. - \end{methoddesc} - - \begin{methoddesc}[socket]{gettimeout}{} - Returns the timeout in seconds associated with socket operations. - A timeout of None indicates that timeouts on socket operations are - disabled. - \end{methoddesc} - \begin{methoddesc}[socket]{setsockopt}{level, optname, value} Set the value of the given socket option (see the \UNIX{} manual page \manpage{setsockopt}{2}). The needed symbolic constants are defined in --- 514,519 ---- diff -r -c python.timeout/python/dist/src/Lib/socket.py python.orig/python/dist/src/Lib/socket.py *** python.timeout/python/dist/src/Lib/socket.py Sun May 12 07:56:23 2002 --- python.orig/python/dist/src/Lib/socket.py Sat Feb 16 23:25:24 2002 *************** *** 134,141 **** _socketmethods = ( 'bind', 'connect', 'connect_ex', 'fileno', 'listen', 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', ! 'recv', 'recvfrom', 'send', 'sendall', 'sendto', 'setblocking', ! 'settimeout', 'gettimeout', 'shutdown') class _socketobject: --- 134,140 ---- _socketmethods = ( 'bind', 'connect', 'connect_ex', 'fileno', 'listen', 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', ! 'recv', 'recvfrom', 'send', 'sendall', 'sendto', 'setblocking', 'shutdown') class _socketobject: *************** *** 169,261 **** class _fileobject: - """Implements a file object on top of a regular socket object.""" ! def __init__ (self, sock, mode='rb', bufsize=8192): self._sock = sock self._mode = mode ! if bufsize <= 0: bufsize = 512 ! self._rbufsize = bufsize self._wbufsize = bufsize ! self._rbuf = [ ] ! self._wbuf = "" ! def close (self): try: if self._sock: self.flush() finally: ! self._sock = None ! def __del__ (self): self.close() ! def flush (self): if self._wbuf: ! self._sock.sendall (self._wbuf) self._wbuf = "" ! def fileno (self): ! return self._sock.fileno () ! def write (self, data): self._wbuf = self._wbuf + data ! if len (self._wbuf) >= self._wbufsize: ! self.flush() ! def writelines (self, list): filter(self._sock.sendall, list) self.flush() ! def __get_rbuf_len (self): ! buf_len = 0 ! for i in [ len (x) for x in self._rbuf ]: ! buf_len += i ! return buf_len ! ! def read (self, size=-1): ! buf_len = self.__get_rbuf_len () ! while size < 0 or buf_len < size: ! recv_size = max (self._rbufsize, size - buf_len) ! data = self._sock.recv (recv_size) ! if not data: ! break ! buf_len += len (data) ! self._rbuf.append (data) ! data = ''.join (self._rbuf) ! # Clear the rbuf at the end so we're not affected by ! # an exception during a recv ! self._rbuf = [ ] ! if buf_len > size: ! self._rbuf.append (data[size:]) ! data = data[:size] ! return data ! ! def readline (self, size=-1): ! index = -1 ! buf_len = self.__get_rbuf_len () ! if len (self._rbuf): ! index = min ([ x.find ('\n') for x in self._rbuf ]) ! while index < 0 and (size < 0 or buf_len < size): ! recv_size = max (self._rbufsize, size - buf_len) ! data = self._sock.recv (recv_size) ! if not data: ! break ! buf_len += len (data) ! self._rbuf.append (data) ! index = data.find ('\n') ! data = ''.join (self._rbuf) ! self._rbuf = [ ] ! index = data.find ('\n') ! if index >= 0: ! index += 1 ! elif buf_len > size: ! index = size ! else: ! index = buf_len ! self._rbuf.append (data[index:]) ! data = data[:index] return data def readlines(self, sizehint = 0): --- 168,258 ---- class _fileobject: ! def __init__(self, sock, mode, bufsize): self._sock = sock self._mode = mode ! if bufsize < 0: bufsize = 512 ! self._rbufsize = max(1, bufsize) self._wbufsize = bufsize ! self._wbuf = self._rbuf = "" ! def close(self): try: if self._sock: self.flush() finally: ! self._sock = 0 ! def __del__(self): self.close() ! def flush(self): if self._wbuf: ! self._sock.sendall(self._wbuf) self._wbuf = "" ! def fileno(self): ! return self._sock.fileno() ! def write(self, data): self._wbuf = self._wbuf + data ! if self._wbufsize == 1: ! if '\n' in data: ! self.flush() ! else: ! if len(self._wbuf) >= self._wbufsize: ! self.flush() ! def writelines(self, list): filter(self._sock.sendall, list) self.flush() ! def read(self, n=-1): ! if n >= 0: ! k = len(self._rbuf) ! if n <= k: ! data = self._rbuf[:n] ! self._rbuf = self._rbuf[n:] ! return data ! n = n - k ! L = [self._rbuf] ! self._rbuf = "" ! while n > 0: ! new = self._sock.recv(max(n, self._rbufsize)) ! if not new: break ! k = len(new) ! if k > n: ! L.append(new[:n]) ! self._rbuf = new[n:] ! break ! L.append(new) ! n = n - k ! return "".join(L) ! k = max(512, self._rbufsize) ! L = [self._rbuf] ! self._rbuf = "" ! while 1: ! new = self._sock.recv(k) ! if not new: break ! L.append(new) ! k = min(k*2, 1024**2) ! return "".join(L) ! ! def readline(self, limit=-1): ! data = "" ! i = self._rbuf.find('\n') ! while i < 0 and not (0 < limit <= len(self._rbuf)): ! new = self._sock.recv(self._rbufsize) ! if not new: break ! i = new.find('\n') ! if i >= 0: i = i + len(self._rbuf) ! self._rbuf = self._rbuf + new ! if i < 0: i = len(self._rbuf) ! else: i = i+1 ! if 0 <= limit < len(self._rbuf): i = limit ! data, self._rbuf = self._rbuf[:i], self._rbuf[i:] return data def readlines(self, sizehint = 0): Only in python.timeout/python/dist/src/Lib/test: test_timeout.py diff -r -c python.timeout/python/dist/src/Modules/socketmodule.c python.orig/python/dist/src/Modules/socketmodule.c *** python.timeout/python/dist/src/Modules/socketmodule.c Sun May 12 07:56:35 2002 --- python.orig/python/dist/src/Modules/socketmodule.c Sat Apr 27 14:44:31 2002 *************** *** 66,73 **** - s.sendall(string [,flags]) # tries to send everything in a loop - s.sendto(string, [flags,] sockaddr) --> nbytes - s.setblocking(0 | 1) --> None - - s.settimeout (None | time) -> None - - s.gettimeout () -> None or floating secs - s.setsockopt(level, optname, value) --> None - s.shutdown(how) --> None - repr(s) --> "" --- 66,71 ---- *************** *** 132,138 **** /* Generic includes */ #include #include - #include /* Generic socket object definitions and includes */ #define PySocket_BUILDING_SOCKET --- 130,135 ---- *************** *** 235,244 **** /* XXX There's a problem here: *static* functions are not supposed to have a Py prefix (or use CapitalizedWords). Later... */ - /* Global variable holding the reference to the select module for use - * in implementing socket timeouts. */ - static PyObject *PySelectModule; - /* Global variable holding the exception type for errors detected by this module (but not argument type or memory errors, etc.). */ --- 232,237 ---- *************** *** 428,546 **** return NULL; } - /* For timeout errors */ - static PyObject * - PyTimeout_Err (void) - { - PyObject *v; - - #ifdef MS_WINDOWS - v = Py_BuildValue ("(is)", WSAETIMEDOUT, "Socket operation timed out"); - #else - v = Py_BuildValue ("(is)", ETIMEDOUT, "Socket operation timed out"); - #endif - - if (v != NULL) { - PyErr_SetObject (PySocket_Error, v); - Py_DECREF (v); - } - - return NULL; - } - - /* Function to perfrom the setting of socket blocking mode - * internally. block = (1 | 0). - */ - static int - internal_setblocking(PySocketSockObject *s, int block) - { - #ifndef RISCOS - #ifndef MS_WINDOWS - int delay_flag; - #endif - #endif - - Py_BEGIN_ALLOW_THREADS - #ifdef __BEOS__ - block = !block; - setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK, - (void *)(&block), sizeof( int ) ); - #else - #ifndef RISCOS - #ifndef MS_WINDOWS - #if defined(PYOS_OS2) && !defined(PYCC_GCC) - block = !block; - ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); - #else /* !PYOS_OS2 */ - delay_flag = fcntl (s->sock_fd, F_GETFL, 0); - if (block) - delay_flag &= (~O_NDELAY); - else - delay_flag |= O_NDELAY; - fcntl (s->sock_fd, F_SETFL, delay_flag); - #endif /* !PYOS_OS2 */ - #else /* MS_WINDOWS */ - block = !block; - ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); - #endif /* MS_WINDOWS */ - #endif /* __BEOS__ */ - #endif /* RISCOS */ - Py_END_ALLOW_THREADS - - /* Since these don't return anything */ - return 1; - } - - /* For access to the select module to poll the socket for timeout - * functionality. If direction is: 1 poll as read, 0, poll as - * write - */ - static int - internal_select(PySocketSockObject *s, int direction) - { - PyObject *empty, *sock_list, *sel_res; - int count = 0; - - /* Construct the arguments to select */ - empty = PyList_New (0); - - sock_list = PyList_New (0); - if (PyList_Append (sock_list, (PyObject *)s) < 0) - return -1; - - Py_INCREF (s->sock_timeout); - - /* Now poll to see if the socket is ready */ - sel_res = PyObject_CallMethod (PySelectModule, "select", "OOOO", - direction ? empty : sock_list, - direction ? sock_list : empty, - empty, - s->sock_timeout); - - Py_DECREF (empty); - Py_DECREF (sock_list); - Py_DECREF (s->sock_timeout); - - if (sel_res == NULL) - return -1; - - /* Determine if we were polled */ - sock_list = PySequence_GetItem (sel_res, direction ? 1 : 0); - count = PySequence_Count (sock_list, (PyObject *)s); - - Py_DECREF (sock_list); - Py_DECREF (sel_res); - - return count; - } - /* Initialize a new socket object. */ static void init_sockobject(PySocketSockObject *s, SOCKET_T fd, int family, int type, int proto) { - #ifdef RISCOS int block = 1; #endif --- 421,432 ---- *************** *** 548,570 **** s->sock_family = family; s->sock_type = type; s->sock_proto = proto; - s->sock_blocking = 1; /* Start in blocking mode */ - - /* Assign a timeout of none */ - s->sock_timeout = Py_None; - Py_INCREF (s->sock_timeout); - s->errorhandler = &PySocket_Err; #ifdef RISCOS if(taskwindow) { socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); } #endif - - /* Start in non-blocking mode since select will take - * care of all our blocking for us - */ - internal_setblocking (s, 0); } --- 434,445 ---- *************** *** 982,1027 **** PyObject *sock = NULL; PyObject *addr = NULL; PyObject *res = NULL; - int count = 0; if (!getsockaddrlen(s, &addrlen)) return NULL; memset(addrbuf, 0, addrlen); - - errno = 0; /* Reset indicator */ - - Py_BEGIN_ALLOW_THREADS - newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); - Py_END_ALLOW_THREADS - - #ifdef MS_WINDOWS - if (newfd == INVALID_SOCKET) - if (!s->sock_blocking || errno != WSAEWOULDBLOCK) - return s->errorhandler (); - #else - if (newfd < 0) - if (!s->sock_blocking || (errno != EAGAIN && errno != EWOULDBLOCK)) - return s->errorhandler (); - #endif - - /* try waiting the timeout period */ - count = internal_select (s, 0); - if (count < 0) - return NULL; - else if (count == 0) - return PyTimeout_Err (); - Py_BEGIN_ALLOW_THREADS newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); Py_END_ALLOW_THREADS - - /* At this point, we really have an error */ #ifdef MS_WINDOWS if (newfd == INVALID_SOCKET) #else if (newfd < 0) #endif ! return s->errorhandler (); /* Create the new object with unspecified family, to avoid calls to bind() etc. on it. */ --- 857,875 ---- PyObject *sock = NULL; PyObject *addr = NULL; PyObject *res = NULL; if (!getsockaddrlen(s, &addrlen)) return NULL; memset(addrbuf, 0, addrlen); Py_BEGIN_ALLOW_THREADS newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); Py_END_ALLOW_THREADS #ifdef MS_WINDOWS if (newfd == INVALID_SOCKET) #else if (newfd < 0) #endif ! return s->errorhandler(); /* Create the new object with unspecified family, to avoid calls to bind() etc. on it. */ *************** *** 1029,1047 **** s->sock_family, s->sock_type, s->sock_proto); - if (sock == NULL) { SOCKETCLOSE(newfd); goto finally; } addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, ! addrlen); if (addr == NULL) goto finally; res = Py_BuildValue("OO", sock, addr); ! finally: Py_XDECREF(sock); Py_XDECREF(addr); return res; --- 877,894 ---- s->sock_family, s->sock_type, s->sock_proto); if (sock == NULL) { SOCKETCLOSE(newfd); goto finally; } addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, ! addrlen); if (addr == NULL) goto finally; res = Py_BuildValue("OO", sock, addr); ! finally: Py_XDECREF(sock); Py_XDECREF(addr); return res; *************** *** 1054,1071 **** connection, and the address of the client. For IP sockets, the address\n\ info is a pair (hostaddr, port)."; /* s.setblocking(1 | 0) method */ static PyObject * PySocketSock_setblocking(PySocketSockObject *s, PyObject *arg) { int block; ! block = PyInt_AsLong(arg); if (block == -1 && PyErr_Occurred()) return NULL; ! ! s->sock_blocking = block; Py_INCREF(Py_None); return Py_None; --- 901,947 ---- connection, and the address of the client. For IP sockets, the address\n\ info is a pair (hostaddr, port)."; + /* s.setblocking(1 | 0) method */ static PyObject * PySocketSock_setblocking(PySocketSockObject *s, PyObject *arg) { int block; ! #ifndef RISCOS ! #ifndef MS_WINDOWS ! int delay_flag; ! #endif ! #endif block = PyInt_AsLong(arg); if (block == -1 && PyErr_Occurred()) return NULL; ! Py_BEGIN_ALLOW_THREADS ! #ifdef __BEOS__ ! block = !block; ! setsockopt( s->sock_fd, SOL_SOCKET, SO_NONBLOCK, ! (void *)(&block), sizeof( int ) ); ! #else ! #ifndef RISCOS ! #ifndef MS_WINDOWS ! #if defined(PYOS_OS2) && !defined(PYCC_GCC) ! block = !block; ! ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); ! #else /* !PYOS_OS2 */ ! delay_flag = fcntl (s->sock_fd, F_GETFL, 0); ! if (block) ! delay_flag &= (~O_NDELAY); ! else ! delay_flag |= O_NDELAY; ! fcntl (s->sock_fd, F_SETFL, delay_flag); ! #endif /* !PYOS_OS2 */ ! #else /* MS_WINDOWS */ ! block = !block; ! ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); ! #endif /* MS_WINDOWS */ ! #endif /* __BEOS__ */ ! #endif /* RISCOS */ ! Py_END_ALLOW_THREADS Py_INCREF(Py_None); return Py_None; *************** *** 1077,1130 **** Set the socket to blocking (flag is true) or non-blocking (false).\n\ This uses the FIONBIO ioctl with the O_NDELAY flag."; - /* s.settimeout (integer number) method. - * Causes an exception to be raised when the integer number of seconds - * has elapsed when performing a blocking socket operation. - */ - static PyObject * - PySocketSock_settimeout(PySocketSockObject *s, PyObject *arg) - { - double value = 0.0; - - if (! (arg == Py_None || - ((PyFloat_Check (arg) || PyInt_Check (arg)) && - (value = PyFloat_AsDouble (arg)) >= 0.0))) { - PyErr_SetString(PySocket_Error, "Invalid timeout value"); - return NULL; - } - - Py_DECREF (s->sock_timeout); - s->sock_timeout = (arg == Py_None) ? Py_None : PyFloat_FromDouble (value); - Py_INCREF (s->sock_timeout); - - Py_INCREF(Py_None); - return Py_None; - } - - static char settimeout_doc[] = - "settimeout(seconds)\n\ - \n\ - Set a timeout on blocking socket operations. Socket operations will\n\ - raise an exception if the timeout period has elapsed before the\n\ - operation has completed. Setting a timeout of None disables timeouts\n\ - on socket operations."; - - /* s.gettimeout () method. - * Returns the timeout associated with a socket. - */ - static PyObject * - PySocketSock_gettimeout(PySocketSockObject *s) - { - Py_INCREF (s->sock_timeout); - return s->sock_timeout; - } - - static char gettimeout_doc[] = - "gettimeout()\n\ - \n\ - Returns the timeout in seconds associated with socket operations.\n\ - A timeout of None indicates that timeouts on socket operations are\n\ - disabled."; #ifdef RISCOS /* s.sleeptaskw(1 | 0) method */ --- 953,958 ---- *************** *** 1132,1147 **** static PyObject * PySocketSock_sleeptaskw(PySocketSockObject *s,PyObject *args) { ! int block; ! int delay_flag; ! if (!PyArg_Parse(args, "i", &block)) ! return NULL; ! Py_BEGIN_ALLOW_THREADS ! socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); ! Py_END_ALLOW_THREADS ! Py_INCREF(Py_None); ! return Py_None; } static char sleeptaskw_doc[] = "sleeptaskw(flag)\n\ --- 960,975 ---- static PyObject * PySocketSock_sleeptaskw(PySocketSockObject *s,PyObject *args) { ! int block; ! int delay_flag; ! if (!PyArg_Parse(args, "i", &block)) ! return NULL; ! Py_BEGIN_ALLOW_THREADS ! socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); ! Py_END_ALLOW_THREADS ! Py_INCREF(Py_None); ! return Py_None; } static char sleeptaskw_doc[] = "sleeptaskw(flag)\n\ *************** *** 1311,1361 **** struct sockaddr *addr; int addrlen; int res; - int count = 0; if (!getsockaddrarg(s, addro, &addr, &addrlen)) return NULL; - - errno = 0; /* Reset the err indicator */ - - Py_BEGIN_ALLOW_THREADS - res = connect(s->sock_fd, addr, addrlen); - Py_END_ALLOW_THREADS - - if (res < 0) { - /* Return if we're already connected */ - #ifdef MS_WINDOWS - if (errno == WSAEINVAL || errno == WSAEISCONN) - #else - if (errno == EISCONN) - #endif - goto connected; - - /* Check if we have an error */ - #ifdef MS_WINDOWS - if (!s->sock_blocking || errno != WSAEWOULDBLOCK) - #else - if (!s->sock_blocking || (errno != EINPROGRESS && errno != EALREADY && errno != EWOULDBLOCK)) - #endif - return s->errorhandler (); - } - - /* Check if we're ready for the connect via select */ - count = internal_select (s, 1); - if (count < 0) - return NULL; - else if (count == 0) /* Timeout elapsed */ - return PyTimeout_Err (); - - /* Complete the connection now */ Py_BEGIN_ALLOW_THREADS res = connect(s->sock_fd, addr, addrlen); Py_END_ALLOW_THREADS - if (res < 0) ! return s->errorhandler (); ! ! connected: Py_INCREF(Py_None); return Py_None; } --- 1139,1152 ---- struct sockaddr *addr; int addrlen; int res; if (!getsockaddrarg(s, addro, &addr, &addrlen)) return NULL; Py_BEGIN_ALLOW_THREADS res = connect(s->sock_fd, addr, addrlen); Py_END_ALLOW_THREADS if (res < 0) ! return s->errorhandler(); Py_INCREF(Py_None); return Py_None; } *************** *** 1594,1626 **** { int len, n, flags = 0; PyObject *buf; - int count = 0; - if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags)) return NULL; ! ! if (len < 0) { PyErr_SetString(PyExc_ValueError, "negative buffersize in connect"); return NULL; } - buf = PyString_FromStringAndSize((char *) 0, len); if (buf == NULL) return NULL; - - if (s->sock_blocking) { - count = internal_select (s, 0); - if (count < 0) - return NULL; - else if (count == 0) /* Timeout elapsed */ - return PyTimeout_Err (); - } - Py_BEGIN_ALLOW_THREADS ! n = recv (s->sock_fd, PyString_AS_STRING(buf), len, flags); Py_END_ALLOW_THREADS - if (n < 0) { Py_DECREF(buf); return s->errorhandler(); --- 1385,1403 ---- { int len, n, flags = 0; PyObject *buf; if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags)) return NULL; ! if (len < 0) { PyErr_SetString(PyExc_ValueError, "negative buffersize in connect"); return NULL; } buf = PyString_FromStringAndSize((char *) 0, len); if (buf == NULL) return NULL; Py_BEGIN_ALLOW_THREADS ! n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags); Py_END_ALLOW_THREADS if (n < 0) { Py_DECREF(buf); return s->errorhandler(); *************** *** 1648,1674 **** PyObject *buf = NULL; PyObject *addr = NULL; PyObject *ret = NULL; int len, n, flags = 0; socklen_t addrlen; - int count = 0; - if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags)) return NULL; - if (!getsockaddrlen(s, &addrlen)) return NULL; buf = PyString_FromStringAndSize((char *) 0, len); if (buf == NULL) return NULL; - - if (s->sock_blocking) { - count = internal_select (s, 0); - if (count < 0) - return NULL; - else if (count == 0) /* Timeout elapsed */ - return PyTimeout_Err (); - } - Py_BEGIN_ALLOW_THREADS memset(addrbuf, 0, addrlen); n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags, --- 1425,1440 ---- PyObject *buf = NULL; PyObject *addr = NULL; PyObject *ret = NULL; + int len, n, flags = 0; socklen_t addrlen; if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags)) return NULL; if (!getsockaddrlen(s, &addrlen)) return NULL; buf = PyString_FromStringAndSize((char *) 0, len); if (buf == NULL) return NULL; Py_BEGIN_ALLOW_THREADS memset(addrbuf, 0, addrlen); n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags, *************** *** 1683,1694 **** #endif ); Py_END_ALLOW_THREADS - if (n < 0) { Py_DECREF(buf); return s->errorhandler(); } - if (n != len && _PyString_Resize(&buf, n) < 0) return NULL; --- 1449,1458 ---- *************** *** 1696,1703 **** goto finally; ret = Py_BuildValue("OO", buf, addr); ! ! finally: Py_XDECREF(addr); Py_XDECREF(buf); return ret; --- 1460,1466 ---- goto finally; ret = Py_BuildValue("OO", buf, addr); ! finally: Py_XDECREF(addr); Py_XDECREF(buf); return ret; *************** *** 1708,1713 **** --- 1471,1477 ---- \n\ Like recv(buffersize, flags) but also return the sender's address info."; + /* s.send(data [,flags]) method */ static PyObject * *************** *** 1715,1737 **** { char *buf; int len, n, flags = 0; - int count = 0; - if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) return NULL; - - if (s->sock_timeout) { - count = internal_select (s, 1); - if (count < 0) - return NULL; - else if (count == 0) /* Timeout elapsed */ - return PyTimeout_Err (); - } - Py_BEGIN_ALLOW_THREADS n = send(s->sock_fd, buf, len, flags); Py_END_ALLOW_THREADS - if (n < 0) return s->errorhandler(); return PyInt_FromLong((long)n); --- 1479,1489 ---- *************** *** 1752,1770 **** { char *buf; int len, n, flags = 0; - int count = 0; - if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) return NULL; - - if (s->sock_timeout) { - count = internal_select (s, 1); - if (count < 0) - return NULL; - else if (count == 0) /* Timeout elapsed */ - return PyTimeout_Err (); - } - Py_BEGIN_ALLOW_THREADS do { n = send(s->sock_fd, buf, len, flags); --- 1504,1511 ---- *************** *** 1774,1783 **** len -= n; } while (len > 0); Py_END_ALLOW_THREADS - if (n < 0) return s->errorhandler(); - Py_INCREF(Py_None); return Py_None; } --- 1515,1522 ---- *************** *** 1800,1807 **** char *buf; struct sockaddr *addr; int addrlen, len, n, flags; - int count = 0; - flags = 0; if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { PyErr_Clear(); --- 1539,1544 ---- *************** *** 1809,1830 **** &buf, &len, &flags, &addro)) return NULL; } - if (!getsockaddrarg(s, addro, &addr, &addrlen)) return NULL; - - if (s->sock_timeout) { - count = internal_select (s, 1); - if (count < 0) - return NULL; - else if (count == 0) /* Timeout elapsed */ - return PyTimeout_Err (); - } - Py_BEGIN_ALLOW_THREADS n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); Py_END_ALLOW_THREADS - if (n < 0) return s->errorhandler(); return PyInt_FromLong((long)n); --- 1546,1556 ---- *************** *** 1909,1918 **** sendto_doc}, {"setblocking", (PyCFunction)PySocketSock_setblocking, METH_O, setblocking_doc}, - {"settimeout", (PyCFunction)PySocketSock_settimeout, METH_O, - settimeout_doc}, - {"gettimeout", (PyCFunction)PySocketSock_gettimeout, METH_NOARGS, - gettimeout_doc}, {"setsockopt", (PyCFunction)PySocketSock_setsockopt, METH_VARARGS, setsockopt_doc}, {"shutdown", (PyCFunction)PySocketSock_shutdown, METH_O, --- 1635,1640 ---- *************** *** 1989,1999 **** "|iii:socket", keywords, &family, &type, &proto)) return -1; - Py_BEGIN_ALLOW_THREADS fd = socket(family, type, proto); Py_END_ALLOW_THREADS - #ifdef MS_WINDOWS if (fd == INVALID_SOCKET) #else --- 1711,1719 ---- *************** *** 2009,2017 **** #ifdef SIGPIPE (void) signal(SIGPIPE, SIG_IGN); #endif - return 0; - } --- 1729,1735 ---- *************** *** 2165,2171 **** #endif return NULL; } - if (h->h_addrtype != af) { #ifdef HAVE_STRERROR /* Let's get real error message to return */ --- 1883,1888 ---- *************** *** 2176,2222 **** #endif return NULL; } - switch (af) { - case AF_INET: if (alen < sizeof(struct sockaddr_in)) return NULL; break; - #ifdef ENABLE_IPV6 case AF_INET6: if (alen < sizeof(struct sockaddr_in6)) return NULL; break; #endif - } - if ((name_list = PyList_New(0)) == NULL) goto err; - if ((addr_list = PyList_New(0)) == NULL) goto err; - for (pch = h->h_aliases; *pch != NULL; pch++) { int status; tmp = PyString_FromString(*pch); if (tmp == NULL) goto err; - status = PyList_Append(name_list, tmp); Py_DECREF(tmp); - if (status) goto err; } - for (pch = h->h_addr_list; *pch != NULL; pch++) { int status; - switch (af) { - case AF_INET: { struct sockaddr_in sin; --- 1893,1927 ---- *************** *** 2227,2238 **** #endif memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); - if (pch == h->h_addr_list && alen >= sizeof(sin)) memcpy((char *) addr, &sin, sizeof(sin)); break; } - #ifdef ENABLE_IPV6 case AF_INET6: { --- 1932,1941 ---- *************** *** 2245,2275 **** memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); tmp = makeipaddr((struct sockaddr *)&sin6, sizeof(sin6)); - if (pch == h->h_addr_list && alen >= sizeof(sin6)) memcpy((char *) addr, &sin6, sizeof(sin6)); break; } #endif - default: /* can't happen */ PyErr_SetString(PySocket_Error, "unsupported address family"); return NULL; } - if (tmp == NULL) goto err; - status = PyList_Append(addr_list, tmp); Py_DECREF(tmp); - if (status) goto err; } - rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); - err: Py_XDECREF(name_list); Py_XDECREF(addr_list); --- 1948,1971 ---- *************** *** 3041,3050 **** if (PyModule_AddObject(m, PySocket_CAPI_NAME, PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL) ) != 0) - return; - - /* Import in the select module for implementing timeout functionality */ - if ((PySelectModule = PyImport_ImportModule ("select")) == NULL) return; /* Address families (we only support AF_INET and AF_UNIX) */ --- 2737,2742 ---- diff -r -c python.timeout/python/dist/src/Modules/socketmodule.h python.orig/python/dist/src/Modules/socketmodule.h *** python.timeout/python/dist/src/Modules/socketmodule.h Sun May 12 07:56:35 2002 --- python.orig/python/dist/src/Modules/socketmodule.h Fri Mar 1 03:31:07 2002 *************** *** 83,91 **** PyObject *(*errorhandler)(void); /* Error handler; checks errno, returns NULL and sets a Python exception */ - int sock_blocking; /* Flag indicated whether the socket is - * in blocking mode */ - PyObject *sock_timeout; /* Operation timeout value */ } PySocketSockObject; /* --- C API ----------------------------------------------------*/ --- 83,88 ----