Index: trunk/Modules/socketmodule.c =================================================================== --- trunk/Modules/socketmodule.c (revision 47012) +++ trunk/Modules/socketmodule.c (working copy) @@ -672,7 +672,7 @@ The argument writing indicates the direction. This does not raise an exception; we'll let our caller do that after they've reacquired the interpreter lock. - Returns 1 on timeout, 0 otherwise. */ + Returns 1 on timeout, -1 on error, 0 otherwise. */ static int internal_select(PySocketSockObject *s, int writing) { @@ -699,6 +699,9 @@ n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); else n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); + + if (n < 0) + return -1; if (n == 0) return 1; return 0; @@ -1498,7 +1501,7 @@ &addrlen); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return NULL; } @@ -1867,9 +1870,15 @@ if (s->sock_timeout > 0.0) { if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { timeout = internal_select(s, 1); - res = connect(s->sock_fd, addr, addrlen); - if (res < 0 && errno == EISCONN) - res = 0; + if (timeout == 0) { + res = connect(s->sock_fd, addr, addrlen); + if (res < 0 && errno == EISCONN) + res = 0; + } + else if (timeout == -1) + res = errno; /* had error */ + else + res = EWOULDBLOCK; /* timed out */ } } @@ -1899,7 +1908,7 @@ res = internal_connect(s, addr, addrlen, &timeout); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return NULL; } @@ -1933,6 +1942,13 @@ res = internal_connect(s, addr, addrlen, &timeout); Py_END_ALLOW_THREADS + /* Signals are not errors (though they may raise exceptions). Adapted + from PyErr_SetFromErrnoWithFilenameObject(). */ +#ifdef EINTR + if (res == EINTR && PyErr_CheckSignals()) + return NULL; +#endif + return PyInt_FromLong((long) res); } @@ -2153,10 +2169,10 @@ static ssize_t sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) { - ssize_t outlen = 0; + ssize_t outlen = -1; int timeout; #ifdef __VMS - int remaining, nread; + int remaining; char *read_buf; #endif @@ -2172,7 +2188,7 @@ outlen = recv(s->sock_fd, cbuf, len, flags); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return -1; } @@ -2187,6 +2203,7 @@ remaining = len; while (remaining != 0) { unsigned int segment; + int nread = -1; segment = remaining /SEGMENT_SIZE; if (segment != 0) { @@ -2202,7 +2219,7 @@ nread = recv(s->sock_fd, read_buf, segment, flags); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return -1; } @@ -2350,7 +2367,7 @@ { sock_addr_t addrbuf; int timeout; - ssize_t n = 0; + ssize_t n = -1; socklen_t addrlen; *addr = NULL; @@ -2382,7 +2399,7 @@ } Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return -1; } @@ -2497,7 +2514,7 @@ sock_send(PySocketSockObject *s, PyObject *args) { char *buf; - int len, n = 0, flags = 0, timeout; + int len, n = -1, flags = 0, timeout; #ifdef __VMS int send_length; #endif @@ -2515,7 +2532,7 @@ n = send(s->sock_fd, buf, len, flags); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return NULL; } @@ -2537,10 +2554,11 @@ } Py_BEGIN_ALLOW_THREADS timeout = internal_select(s, 1); + n = -1; if (!timeout) n = send(s->sock_fd, buf, segment, flags); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return NULL; } @@ -2568,7 +2586,7 @@ sock_sendall(PySocketSockObject *s, PyObject *args) { char *buf; - int len, n = 0, flags = 0, timeout; + int len, n = -1, flags = 0, timeout; if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) return NULL; @@ -2579,6 +2597,7 @@ Py_BEGIN_ALLOW_THREADS do { timeout = internal_select(s, 1); + n = -1; if (timeout) break; n = send(s->sock_fd, buf, len, flags); @@ -2589,7 +2608,7 @@ } while (len > 0); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return NULL; } @@ -2617,7 +2636,7 @@ PyObject *addro; char *buf; struct sockaddr *addr; - int addrlen, len, n = 0, flags, timeout; + int addrlen, len, n = -1, flags, timeout; flags = 0; if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { @@ -2639,7 +2658,7 @@ n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); Py_END_ALLOW_THREADS - if (timeout) { + if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); return NULL; }