Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(169530)

Delta Between Two Patch Sets: Modules/socketmodule.c

Issue 23001: Accept mutable bytes-like objects
Left Patch Set: Created 5 years ago
Right Patch Set: Created 4 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* Socket module */ 1 /* Socket module */
2 2
3 /* 3 /*
4 4
5 This module provides an interface to Berkeley socket IPC. 5 This module provides an interface to Berkeley socket IPC.
6 6
7 Limitations: 7 Limitations:
8 8
9 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a 9 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
10 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported 10 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 277
278 # include <fcntl.h> 278 # include <fcntl.h>
279 279
280 #else 280 #else
281 281
282 /* MS_WINDOWS includes */ 282 /* MS_WINDOWS includes */
283 # ifdef HAVE_FCNTL_H 283 # ifdef HAVE_FCNTL_H
284 # include <fcntl.h> 284 # include <fcntl.h>
285 # endif 285 # endif
286 286
287 #if defined(_MSC_VER) && _MSC_VER >= 1800
288 /* Provides the IsWindows7SP1OrGreater() function */
289 #include <VersionHelpers.h>
290 #endif
291
287 #endif 292 #endif
288 293
289 #include <stddef.h> 294 #include <stddef.h>
290 295
291 #ifndef O_NONBLOCK 296 #ifndef O_NONBLOCK
292 # define O_NONBLOCK O_NDELAY 297 # define O_NONBLOCK O_NDELAY
293 #endif 298 #endif
294 299
295 /* include Python's addrinfo.h unless it causes trouble */ 300 /* include Python's addrinfo.h unless it causes trouble */
296 #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) 301 #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
(...skipping 1664 matching lines...) Expand 10 before | Expand all | Expand 10 after
1961 return 0; 1966 return 0;
1962 /* Note that POSIX allows msg_controllen to be of a signed type. This is 1967 /* Note that POSIX allows msg_controllen to be of a signed type. This is
1963 annoying under OS X as it's unsigned there and so it triggers a 1968 annoying under OS X as it's unsigned there and so it triggers a
1964 tautological comparison warning under Clang when compared against 0. 1969 tautological comparison warning under Clang when compared against 0.
1965 Since the check is valid on other platforms, silence the warning under 1970 Since the check is valid on other platforms, silence the warning under
1966 Clang. */ 1971 Clang. */
1967 #ifdef __clang__ 1972 #ifdef __clang__
1968 #pragma clang diagnostic push 1973 #pragma clang diagnostic push
1969 #pragma clang diagnostic ignored "-Wtautological-compare" 1974 #pragma clang diagnostic ignored "-Wtautological-compare"
1970 #endif 1975 #endif
1976 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINO R__ > 5)))
1977 #pragma GCC diagnostic push
1978 #pragma GCC diagnostic ignored "-Wtype-limits"
1979 #endif
1971 if (msg->msg_controllen < 0) 1980 if (msg->msg_controllen < 0)
1972 return 0; 1981 return 0;
1982 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINO R__ > 5)))
1983 #pragma GCC diagnostic pop
1984 #endif
1973 #ifdef __clang__ 1985 #ifdef __clang__
1974 #pragma clang diagnostic pop 1986 #pragma clang diagnostic pop
1975 #endif 1987 #endif
1976 if (space < cmsg_len_end) 1988 if (space < cmsg_len_end)
1977 space = cmsg_len_end; 1989 space = cmsg_len_end;
1978 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; 1990 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
1979 return (cmsg_offset <= (size_t)-1 - space && 1991 return (cmsg_offset <= (size_t)-1 - space &&
1980 cmsg_offset + space <= msg->msg_controllen); 1992 cmsg_offset + space <= msg->msg_controllen);
1981 } 1993 }
1982 1994
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2032 static PyObject * 2044 static PyObject *
2033 sock_accept(PySocketSockObject *s) 2045 sock_accept(PySocketSockObject *s)
2034 { 2046 {
2035 sock_addr_t addrbuf; 2047 sock_addr_t addrbuf;
2036 SOCKET_T newfd = INVALID_SOCKET; 2048 SOCKET_T newfd = INVALID_SOCKET;
2037 socklen_t addrlen; 2049 socklen_t addrlen;
2038 PyObject *sock = NULL; 2050 PyObject *sock = NULL;
2039 PyObject *addr = NULL; 2051 PyObject *addr = NULL;
2040 PyObject *res = NULL; 2052 PyObject *res = NULL;
2041 int timeout; 2053 int timeout;
2054 int async_err = 0;
2042 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) 2055 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2043 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ 2056 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
2044 static int accept4_works = -1; 2057 static int accept4_works = -1;
2045 #endif 2058 #endif
2046 2059
2047 if (!getsockaddrlen(s, &addrlen)) 2060 if (!getsockaddrlen(s, &addrlen))
2048 return NULL; 2061 return NULL;
2049 memset(&addrbuf, 0, addrlen); 2062 memset(&addrbuf, 0, addrlen);
2050 2063
2051 if (!IS_SELECTABLE(s)) 2064 if (!IS_SELECTABLE(s))
2052 return select_error(); 2065 return select_error();
2053 2066
2054 BEGIN_SELECT_LOOP(s) 2067 BEGIN_SELECT_LOOP(s)
2055 2068 do {
2056 Py_BEGIN_ALLOW_THREADS 2069 Py_BEGIN_ALLOW_THREADS
2057 timeout = internal_select_ex(s, 0, interval); 2070 timeout = internal_select_ex(s, 0, interval);
2058 if (!timeout) { 2071 if (!timeout) {
2059 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) 2072 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2060 if (accept4_works != 0) { 2073 if (accept4_works != 0) {
2061 newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, 2074 newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen,
2062 SOCK_CLOEXEC); 2075 SOCK_CLOEXEC);
2063 if (newfd == INVALID_SOCKET && accept4_works == -1) { 2076 if (newfd == INVALID_SOCKET && accept4_works == -1) {
2064 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ 2077 /* On Linux older than 2.6.28, accept4() fails with ENOSYS * /
2065 accept4_works = (errno != ENOSYS); 2078 accept4_works = (errno != ENOSYS);
2079 }
2066 } 2080 }
2067 } 2081 if (accept4_works == 0)
2068 if (accept4_works == 0) 2082 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2083 #else
2069 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); 2084 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2070 #else 2085 #endif
2071 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); 2086 }
2072 #endif 2087 Py_END_ALLOW_THREADS
2073 } 2088 } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())) ;
2074 Py_END_ALLOW_THREADS
2075
2076 if (timeout == 1) { 2089 if (timeout == 1) {
2077 PyErr_SetString(socket_timeout, "timed out"); 2090 PyErr_SetString(socket_timeout, "timed out");
2078 return NULL; 2091 return NULL;
2079 } 2092 }
2080 END_SELECT_LOOP(s) 2093 END_SELECT_LOOP(s)
2081 2094
2082 if (newfd == INVALID_SOCKET) 2095 if (newfd == INVALID_SOCKET)
2083 return s->errorhandler(); 2096 return (!async_err) ? s->errorhandler() : NULL;
2084 2097
2085 #ifdef MS_WINDOWS 2098 #ifdef MS_WINDOWS
2086 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { 2099 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
2087 PyErr_SetFromWindowsErr(0); 2100 PyErr_SetFromWindowsErr(0);
2088 SOCKETCLOSE(newfd); 2101 SOCKETCLOSE(newfd);
2089 goto finally; 2102 goto finally;
2090 } 2103 }
2091 #else 2104 #else
2092 2105
2093 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) 2106 #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
2335 2348
2336 /* s.close() method. 2349 /* s.close() method.
2337 Set the file descriptor to -1 so operations tried subsequently 2350 Set the file descriptor to -1 so operations tried subsequently
2338 will surely fail. */ 2351 will surely fail. */
2339 2352
2340 static PyObject * 2353 static PyObject *
2341 sock_close(PySocketSockObject *s) 2354 sock_close(PySocketSockObject *s)
2342 { 2355 {
2343 SOCKET_T fd; 2356 SOCKET_T fd;
2344 2357
2358 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
2359 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
2360 * for more details.
2361 */
2345 if ((fd = s->sock_fd) != -1) { 2362 if ((fd = s->sock_fd) != -1) {
2346 s->sock_fd = -1; 2363 s->sock_fd = -1;
2347 Py_BEGIN_ALLOW_THREADS 2364 Py_BEGIN_ALLOW_THREADS
2348 (void) SOCKETCLOSE(fd); 2365 (void) SOCKETCLOSE(fd);
2349 Py_END_ALLOW_THREADS 2366 Py_END_ALLOW_THREADS
2350 } 2367 }
2351 Py_INCREF(Py_None); 2368 Py_INCREF(Py_None);
2352 return Py_None; 2369 return Py_None;
2353 } 2370 }
2354 2371
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 2524
2508 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) 2525 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2509 return NULL; 2526 return NULL;
2510 2527
2511 Py_BEGIN_ALLOW_THREADS 2528 Py_BEGIN_ALLOW_THREADS
2512 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); 2529 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2513 Py_END_ALLOW_THREADS 2530 Py_END_ALLOW_THREADS
2514 2531
2515 /* Signals are not errors (though they may raise exceptions). Adapted 2532 /* Signals are not errors (though they may raise exceptions). Adapted
2516 from PyErr_SetFromErrnoWithFilenameObject(). */ 2533 from PyErr_SetFromErrnoWithFilenameObject(). */
2517 #ifdef EINTR
2518 if (res == EINTR && PyErr_CheckSignals()) 2534 if (res == EINTR && PyErr_CheckSignals())
2519 return NULL; 2535 return NULL;
2520 #endif
2521 2536
2522 return PyLong_FromLong((long) res); 2537 return PyLong_FromLong((long) res);
2523 } 2538 }
2524 2539
2525 PyDoc_STRVAR(connect_ex_doc, 2540 PyDoc_STRVAR(connect_ex_doc,
2526 "connect_ex(address) -> errno\n\ 2541 "connect_ex(address) -> errno\n\
2527 \n\ 2542 \n\
2528 This is like connect(address), but returns an error code (the errno value)\n\ 2543 This is like connect(address), but returns an error code (the errno value)\n\
2529 instead of raising an exception when an error occurs."); 2544 instead of raising an exception when an error occurs.");
2530 2545
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
2644 * successfully read. If there was an error, it returns -1. Note that it is 2659 * successfully read. If there was an error, it returns -1. Note that it is
2645 * also possible that we return a number of bytes smaller than the request 2660 * also possible that we return a number of bytes smaller than the request
2646 * bytes. 2661 * bytes.
2647 */ 2662 */
2648 2663
2649 static Py_ssize_t 2664 static Py_ssize_t
2650 sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags) 2665 sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
2651 { 2666 {
2652 Py_ssize_t outlen = -1; 2667 Py_ssize_t outlen = -1;
2653 int timeout; 2668 int timeout;
2669 int async_err = 0;
2654 2670
2655 if (!IS_SELECTABLE(s)) { 2671 if (!IS_SELECTABLE(s)) {
2656 select_error(); 2672 select_error();
2657 return -1; 2673 return -1;
2658 } 2674 }
2659 if (len == 0) { 2675 if (len == 0) {
2660 /* If 0 bytes were requested, do nothing. */ 2676 /* If 0 bytes were requested, do nothing. */
2661 return 0; 2677 return 0;
2662 } 2678 }
2663 2679
2664 BEGIN_SELECT_LOOP(s) 2680 BEGIN_SELECT_LOOP(s)
2665 Py_BEGIN_ALLOW_THREADS 2681 do {
2666 timeout = internal_select_ex(s, 0, interval); 2682 Py_BEGIN_ALLOW_THREADS
2667 if (!timeout) { 2683 timeout = internal_select_ex(s, 0, interval);
2684 if (!timeout) {
2668 #ifdef MS_WINDOWS 2685 #ifdef MS_WINDOWS
2669 if (len > INT_MAX) 2686 if (len > INT_MAX)
2670 len = INT_MAX; 2687 len = INT_MAX;
2671 outlen = recv(s->sock_fd, cbuf, (int)len, flags); 2688 outlen = recv(s->sock_fd, cbuf, (int)len, flags);
2672 #else 2689 #else
2673 outlen = recv(s->sock_fd, cbuf, len, flags); 2690 outlen = recv(s->sock_fd, cbuf, len, flags);
2674 #endif 2691 #endif
2675 } 2692 }
2676 Py_END_ALLOW_THREADS 2693 Py_END_ALLOW_THREADS
2694 } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()) );
2677 2695
2678 if (timeout == 1) { 2696 if (timeout == 1) {
2679 PyErr_SetString(socket_timeout, "timed out"); 2697 PyErr_SetString(socket_timeout, "timed out");
2680 return -1; 2698 return -1;
2681 } 2699 }
2682 END_SELECT_LOOP(s) 2700 END_SELECT_LOOP(s)
2683 if (outlen < 0) { 2701 if (outlen < 0) {
2684 /* Note: the call to errorhandler() ALWAYS indirectly returned 2702 /* Note: the call to errorhandler() ALWAYS indirectly returned
2685 NULL, so ignore its return value */ 2703 NULL, so ignore its return value */
2686 s->errorhandler(); 2704 if (!async_err)
2705 s->errorhandler();
2687 return -1; 2706 return -1;
2688 } 2707 }
2689 return outlen; 2708 return outlen;
2690 } 2709 }
2691 2710
2692 2711
2693 /* s.recv(nbytes [,flags]) method */ 2712 /* s.recv(nbytes [,flags]) method */
2694 2713
2695 static PyObject * 2714 static PyObject *
2696 sock_recv(PySocketSockObject *s, PyObject *args) 2715 sock_recv(PySocketSockObject *s, PyObject *args)
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2813 * it yourself. 2832 * it yourself.
2814 */ 2833 */
2815 static Py_ssize_t 2834 static Py_ssize_t
2816 sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags, 2835 sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
2817 PyObject** addr) 2836 PyObject** addr)
2818 { 2837 {
2819 sock_addr_t addrbuf; 2838 sock_addr_t addrbuf;
2820 int timeout; 2839 int timeout;
2821 Py_ssize_t n = -1; 2840 Py_ssize_t n = -1;
2822 socklen_t addrlen; 2841 socklen_t addrlen;
2842 int async_err = 0;
2823 2843
2824 *addr = NULL; 2844 *addr = NULL;
2825 2845
2826 if (!getsockaddrlen(s, &addrlen)) 2846 if (!getsockaddrlen(s, &addrlen))
2827 return -1; 2847 return -1;
2828 2848
2829 if (!IS_SELECTABLE(s)) { 2849 if (!IS_SELECTABLE(s)) {
2830 select_error(); 2850 select_error();
2831 return -1; 2851 return -1;
2832 } 2852 }
2833 2853
2834 BEGIN_SELECT_LOOP(s) 2854 BEGIN_SELECT_LOOP(s)
2835 Py_BEGIN_ALLOW_THREADS 2855 do {
2836 memset(&addrbuf, 0, addrlen); 2856 Py_BEGIN_ALLOW_THREADS
2837 timeout = internal_select_ex(s, 0, interval); 2857 memset(&addrbuf, 0, addrlen);
2838 if (!timeout) { 2858 timeout = internal_select_ex(s, 0, interval);
2859 if (!timeout) {
2839 #ifdef MS_WINDOWS 2860 #ifdef MS_WINDOWS
2840 if (len > INT_MAX) 2861 if (len > INT_MAX)
2841 len = INT_MAX; 2862 len = INT_MAX;
2842 n = recvfrom(s->sock_fd, cbuf, (int)len, flags, 2863 n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
2843 (void *) &addrbuf, &addrlen); 2864 (void *) &addrbuf, &addrlen);
2844 #else 2865 #else
2845 n = recvfrom(s->sock_fd, cbuf, len, flags, 2866 n = recvfrom(s->sock_fd, cbuf, len, flags,
2846 SAS2SA(&addrbuf), &addrlen); 2867 SAS2SA(&addrbuf), &addrlen);
2847 #endif 2868 #endif
2848 } 2869 }
2849 Py_END_ALLOW_THREADS 2870 Py_END_ALLOW_THREADS
2871 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2850 2872
2851 if (timeout == 1) { 2873 if (timeout == 1) {
2852 PyErr_SetString(socket_timeout, "timed out"); 2874 PyErr_SetString(socket_timeout, "timed out");
2853 return -1; 2875 return -1;
2854 } 2876 }
2855 END_SELECT_LOOP(s) 2877 END_SELECT_LOOP(s)
2856 if (n < 0) { 2878 if (n < 0) {
2857 s->errorhandler(); 2879 if (!async_err)
2880 s->errorhandler();
2858 return -1; 2881 return -1;
2859 } 2882 }
2860 2883
2861 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), 2884 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2862 addrlen, s->sock_proto))) 2885 addrlen, s->sock_proto)))
2863 return -1; 2886 return -1;
2864 2887
2865 return n; 2888 return n;
2866 } 2889 }
2867 2890
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
2987 * error). On error, closes any file descriptors received via 3010 * error). On error, closes any file descriptors received via
2988 * SCM_RIGHTS. 3011 * SCM_RIGHTS.
2989 */ 3012 */
2990 static PyObject * 3013 static PyObject *
2991 sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, 3014 sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
2992 int flags, Py_ssize_t controllen, 3015 int flags, Py_ssize_t controllen,
2993 PyObject *(*makeval)(ssize_t, void *), void *makeval_data) 3016 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
2994 { 3017 {
2995 ssize_t bytes_received = -1; 3018 ssize_t bytes_received = -1;
2996 int timeout; 3019 int timeout;
3020 int async_err = 0;
2997 sock_addr_t addrbuf; 3021 sock_addr_t addrbuf;
2998 socklen_t addrbuflen; 3022 socklen_t addrbuflen;
2999 struct msghdr msg = {0}; 3023 struct msghdr msg = {0};
3000 PyObject *cmsg_list = NULL, *retval = NULL; 3024 PyObject *cmsg_list = NULL, *retval = NULL;
3001 void *controlbuf = NULL; 3025 void *controlbuf = NULL;
3002 struct cmsghdr *cmsgh; 3026 struct cmsghdr *cmsgh;
3003 size_t cmsgdatalen = 0; 3027 size_t cmsgdatalen = 0;
3004 int cmsg_status; 3028 int cmsg_status;
3005 3029
3006 /* XXX: POSIX says that msg_name and msg_namelen "shall be 3030 /* XXX: POSIX says that msg_name and msg_namelen "shall be
(...skipping 15 matching lines...) Expand all
3022 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) 3046 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
3023 return PyErr_NoMemory(); 3047 return PyErr_NoMemory();
3024 3048
3025 /* Make the system call. */ 3049 /* Make the system call. */
3026 if (!IS_SELECTABLE(s)) { 3050 if (!IS_SELECTABLE(s)) {
3027 select_error(); 3051 select_error();
3028 goto finally; 3052 goto finally;
3029 } 3053 }
3030 3054
3031 BEGIN_SELECT_LOOP(s) 3055 BEGIN_SELECT_LOOP(s)
3032 Py_BEGIN_ALLOW_THREADS; 3056 do {
3033 msg.msg_name = SAS2SA(&addrbuf); 3057 Py_BEGIN_ALLOW_THREADS;
3034 msg.msg_namelen = addrbuflen; 3058 msg.msg_name = SAS2SA(&addrbuf);
3035 msg.msg_iov = iov; 3059 msg.msg_namelen = addrbuflen;
3036 msg.msg_iovlen = iovlen; 3060 msg.msg_iov = iov;
3037 msg.msg_control = controlbuf; 3061 msg.msg_iovlen = iovlen;
3038 msg.msg_controllen = controllen; 3062 msg.msg_control = controlbuf;
3039 timeout = internal_select_ex(s, 0, interval); 3063 msg.msg_controllen = controllen;
3040 if (!timeout) 3064 timeout = internal_select_ex(s, 0, interval);
3041 bytes_received = recvmsg(s->sock_fd, &msg, flags); 3065 if (!timeout)
3042 Py_END_ALLOW_THREADS; 3066 bytes_received = recvmsg(s->sock_fd, &msg, flags);
3043 if (timeout == 1) { 3067 Py_END_ALLOW_THREADS;
3044 PyErr_SetString(socket_timeout, "timed out"); 3068 if (timeout == 1) {
3045 goto finally; 3069 PyErr_SetString(socket_timeout, "timed out");
3046 } 3070 goto finally;
3071 }
3072 } while (bytes_received < 0 && errno == EINTR &&
3073 !(async_err = PyErr_CheckSignals()));
3047 END_SELECT_LOOP(s) 3074 END_SELECT_LOOP(s)
3048 3075
3049 if (bytes_received < 0) { 3076 if (bytes_received < 0) {
3050 s->errorhandler(); 3077 if (!async_err)
3078 s->errorhandler();
3051 goto finally; 3079 goto finally;
3052 } 3080 }
3053 3081
3054 /* Make list of (level, type, data) tuples from control messages. */ 3082 /* Make list of (level, type, data) tuples from control messages. */
3055 if ((cmsg_list = PyList_New(0)) == NULL) 3083 if ((cmsg_list = PyList_New(0)) == NULL)
3056 goto err_closefds; 3084 goto err_closefds;
3057 /* Check for empty ancillary data as old CMSG_FIRSTHDR() 3085 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
3058 implementations didn't do so. */ 3086 implementations didn't do so. */
3059 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); 3087 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3060 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { 3088 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
3299 #endif /* CMSG_LEN */ 3327 #endif /* CMSG_LEN */
3300 3328
3301 3329
3302 /* s.send(data [,flags]) method */ 3330 /* s.send(data [,flags]) method */
3303 3331
3304 static PyObject * 3332 static PyObject *
3305 sock_send(PySocketSockObject *s, PyObject *args) 3333 sock_send(PySocketSockObject *s, PyObject *args)
3306 { 3334 {
3307 char *buf; 3335 char *buf;
3308 Py_ssize_t len, n = -1; 3336 Py_ssize_t len, n = -1;
3337 int async_err = 0;
3309 int flags = 0, timeout; 3338 int flags = 0, timeout;
3310 Py_buffer pbuf; 3339 Py_buffer pbuf;
3311 3340
3312 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) 3341 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
3313 return NULL; 3342 return NULL;
3314 3343
3315 if (!IS_SELECTABLE(s)) { 3344 if (!IS_SELECTABLE(s)) {
3316 PyBuffer_Release(&pbuf); 3345 PyBuffer_Release(&pbuf);
3317 return select_error(); 3346 return select_error();
3318 } 3347 }
3319 buf = pbuf.buf; 3348 buf = pbuf.buf;
3320 len = pbuf.len; 3349 len = pbuf.len;
3321 3350
3322 BEGIN_SELECT_LOOP(s) 3351 BEGIN_SELECT_LOOP(s)
3323 Py_BEGIN_ALLOW_THREADS 3352 do {
3324 timeout = internal_select_ex(s, 1, interval); 3353 Py_BEGIN_ALLOW_THREADS
3325 if (!timeout) { 3354 timeout = internal_select_ex(s, 1, interval);
3355 if (!timeout) {
3326 #ifdef MS_WINDOWS 3356 #ifdef MS_WINDOWS
3327 if (len > INT_MAX) 3357 if (len > INT_MAX)
3328 len = INT_MAX; 3358 len = INT_MAX;
3329 n = send(s->sock_fd, buf, (int)len, flags); 3359 n = send(s->sock_fd, buf, (int)len, flags);
3330 #else 3360 #else
3331 n = send(s->sock_fd, buf, len, flags); 3361 n = send(s->sock_fd, buf, len, flags);
3332 #endif 3362 #endif
3333 } 3363 }
3334 Py_END_ALLOW_THREADS 3364 Py_END_ALLOW_THREADS
3365 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3335 if (timeout == 1) { 3366 if (timeout == 1) {
3336 PyBuffer_Release(&pbuf); 3367 PyBuffer_Release(&pbuf);
3337 PyErr_SetString(socket_timeout, "timed out"); 3368 PyErr_SetString(socket_timeout, "timed out");
3338 return NULL; 3369 return NULL;
3339 } 3370 }
3340 END_SELECT_LOOP(s) 3371 END_SELECT_LOOP(s)
3341 3372
3342 PyBuffer_Release(&pbuf); 3373 PyBuffer_Release(&pbuf);
3343 if (n < 0) 3374 if (n < 0)
3344 return s->errorhandler(); 3375 return (!async_err) ? s->errorhandler() : NULL;
3345 return PyLong_FromSsize_t(n); 3376 return PyLong_FromSsize_t(n);
3346 } 3377 }
3347 3378
3348 PyDoc_STRVAR(send_doc, 3379 PyDoc_STRVAR(send_doc,
3349 "send(data[, flags]) -> count\n\ 3380 "send(data[, flags]) -> count\n\
3350 \n\ 3381 \n\
3351 Send a data string to the socket. For the optional flags\n\ 3382 Send a data string to the socket. For the optional flags\n\
3352 argument, see the Unix manual. Return the number of bytes\n\ 3383 argument, see the Unix manual. Return the number of bytes\n\
3353 sent; this may be less than len(data) if the network is busy."); 3384 sent; this may be less than len(data) if the network is busy.");
3354 3385
3355 3386
3356 /* s.sendall(data [,flags]) method */ 3387 /* s.sendall(data [,flags]) method */
3357 3388
3358 static PyObject * 3389 static PyObject *
3359 sock_sendall(PySocketSockObject *s, PyObject *args) 3390 sock_sendall(PySocketSockObject *s, PyObject *args)
3360 { 3391 {
3361 char *buf; 3392 char *buf;
3362 Py_ssize_t len, n = -1; 3393 Py_ssize_t len, n = -1;
3363 int flags = 0, timeout, saved_errno; 3394 int async_err = 0;
3395 int flags = 0, timeout;
3364 Py_buffer pbuf; 3396 Py_buffer pbuf;
3365 3397
3366 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) 3398 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3367 return NULL; 3399 return NULL;
3368 buf = pbuf.buf; 3400 buf = pbuf.buf;
3369 len = pbuf.len; 3401 len = pbuf.len;
3370 3402
3371 if (!IS_SELECTABLE(s)) { 3403 if (!IS_SELECTABLE(s)) {
3372 PyBuffer_Release(&pbuf); 3404 PyBuffer_Release(&pbuf);
3373 return select_error(); 3405 return select_error();
(...skipping 11 matching lines...) Expand all
3385 #else 3417 #else
3386 n = send(s->sock_fd, buf, len, flags); 3418 n = send(s->sock_fd, buf, len, flags);
3387 #endif 3419 #endif
3388 } 3420 }
3389 Py_END_ALLOW_THREADS 3421 Py_END_ALLOW_THREADS
3390 if (timeout == 1) { 3422 if (timeout == 1) {
3391 PyBuffer_Release(&pbuf); 3423 PyBuffer_Release(&pbuf);
3392 PyErr_SetString(socket_timeout, "timed out"); 3424 PyErr_SetString(socket_timeout, "timed out");
3393 return NULL; 3425 return NULL;
3394 } 3426 }
3395 /* PyErr_CheckSignals() might change errno */ 3427 if (n >= 0) {
3396 saved_errno = errno; 3428 buf += n;
3397 /* We must run our signal handlers before looping again. 3429 len -= n;
3398 send() can return a successful partial write when it is 3430 }
3399 interrupted, so we can't restrict ourselves to EINTR. */ 3431 } while (len > 0 && (n >= 0 || errno == EINTR) &&
3400 if (PyErr_CheckSignals()) { 3432 !(async_err = PyErr_CheckSignals()));
3401 PyBuffer_Release(&pbuf);
3402 return NULL;
3403 }
3404 if (n < 0) {
3405 /* If interrupted, try again */
3406 if (saved_errno == EINTR)
3407 continue;
3408 else
3409 break;
3410 }
3411 buf += n;
3412 len -= n;
3413 } while (len > 0);
3414 PyBuffer_Release(&pbuf); 3433 PyBuffer_Release(&pbuf);
3415 3434
3416 if (n < 0) 3435 if (n < 0 || async_err)
3417 return s->errorhandler(); 3436 return (!async_err) ? s->errorhandler() : NULL;
3418 3437
3419 Py_INCREF(Py_None); 3438 Py_INCREF(Py_None);
3420 return Py_None; 3439 return Py_None;
3421 } 3440 }
3422 3441
3423 PyDoc_STRVAR(sendall_doc, 3442 PyDoc_STRVAR(sendall_doc,
3424 "sendall(data[, flags])\n\ 3443 "sendall(data[, flags])\n\
3425 \n\ 3444 \n\
3426 Send a data string to the socket. For the optional flags\n\ 3445 Send a data string to the socket. For the optional flags\n\
3427 argument, see the Unix manual. This calls send() repeatedly\n\ 3446 argument, see the Unix manual. This calls send() repeatedly\n\
3428 until all data is sent. If an error occurs, it's impossible\n\ 3447 until all data is sent. If an error occurs, it's impossible\n\
3429 to tell how much data has been sent."); 3448 to tell how much data has been sent.");
3430 3449
3431 3450
3432 /* s.sendto(data, [flags,] sockaddr) method */ 3451 /* s.sendto(data, [flags,] sockaddr) method */
3433 3452
3434 static PyObject * 3453 static PyObject *
3435 sock_sendto(PySocketSockObject *s, PyObject *args) 3454 sock_sendto(PySocketSockObject *s, PyObject *args)
3436 { 3455 {
3437 Py_buffer pbuf; 3456 Py_buffer pbuf;
3438 PyObject *addro; 3457 PyObject *addro;
3439 char *buf; 3458 char *buf;
3440 Py_ssize_t len, arglen; 3459 Py_ssize_t len, arglen;
3441 sock_addr_t addrbuf; 3460 sock_addr_t addrbuf;
3442 int addrlen, n = -1, flags, timeout; 3461 int addrlen, n = -1, flags, timeout;
3462 int async_err = 0;
3443 3463
3444 flags = 0; 3464 flags = 0;
3445 arglen = PyTuple_Size(args); 3465 arglen = PyTuple_Size(args);
3446 switch (arglen) { 3466 switch (arglen) {
3447 case 2: 3467 case 2:
3448 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); 3468 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3449 break; 3469 break;
3450 case 3: 3470 case 3:
3451 PyArg_ParseTuple(args, "y*iO:sendto", 3471 PyArg_ParseTuple(args, "y*iO:sendto",
3452 &pbuf, &flags, &addro); 3472 &pbuf, &flags, &addro);
(...skipping 14 matching lines...) Expand all
3467 PyBuffer_Release(&pbuf); 3487 PyBuffer_Release(&pbuf);
3468 return select_error(); 3488 return select_error();
3469 } 3489 }
3470 3490
3471 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { 3491 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3472 PyBuffer_Release(&pbuf); 3492 PyBuffer_Release(&pbuf);
3473 return NULL; 3493 return NULL;
3474 } 3494 }
3475 3495
3476 BEGIN_SELECT_LOOP(s) 3496 BEGIN_SELECT_LOOP(s)
3477 Py_BEGIN_ALLOW_THREADS 3497 do {
3478 timeout = internal_select_ex(s, 1, interval); 3498 Py_BEGIN_ALLOW_THREADS
3479 if (!timeout) { 3499 timeout = internal_select_ex(s, 1, interval);
3500 if (!timeout) {
3480 #ifdef MS_WINDOWS 3501 #ifdef MS_WINDOWS
3481 if (len > INT_MAX) 3502 if (len > INT_MAX)
3482 len = INT_MAX; 3503 len = INT_MAX;
3483 n = sendto(s->sock_fd, buf, (int)len, flags, 3504 n = sendto(s->sock_fd, buf, (int)len, flags,
3484 SAS2SA(&addrbuf), addrlen); 3505 SAS2SA(&addrbuf), addrlen);
3485 #else 3506 #else
3486 n = sendto(s->sock_fd, buf, len, flags, 3507 n = sendto(s->sock_fd, buf, len, flags,
3487 SAS2SA(&addrbuf), addrlen); 3508 SAS2SA(&addrbuf), addrlen);
3488 #endif 3509 #endif
3489 } 3510 }
3490 Py_END_ALLOW_THREADS 3511 Py_END_ALLOW_THREADS
3512 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3491 3513
3492 if (timeout == 1) { 3514 if (timeout == 1) {
3493 PyBuffer_Release(&pbuf); 3515 PyBuffer_Release(&pbuf);
3494 PyErr_SetString(socket_timeout, "timed out"); 3516 PyErr_SetString(socket_timeout, "timed out");
3495 return NULL; 3517 return NULL;
3496 } 3518 }
3497 END_SELECT_LOOP(s) 3519 END_SELECT_LOOP(s)
3498 PyBuffer_Release(&pbuf); 3520 PyBuffer_Release(&pbuf);
3499 if (n < 0) 3521 if (n < 0)
3500 return s->errorhandler(); 3522 return (!async_err) ? s->errorhandler() : NULL;
3501 return PyLong_FromSsize_t(n); 3523 return PyLong_FromSsize_t(n);
3502 } 3524 }
3503 3525
3504 PyDoc_STRVAR(sendto_doc, 3526 PyDoc_STRVAR(sendto_doc,
3505 "sendto(data[, flags], address) -> count\n\ 3527 "sendto(data[, flags], address) -> count\n\
3506 \n\ 3528 \n\
3507 Like send(data, flags) but allows specifying the destination address.\n\ 3529 Like send(data, flags) but allows specifying the destination address.\n\
3508 For IP sockets, the address is a pair (hostaddr, port)."); 3530 For IP sockets, the address is a pair (hostaddr, port).");
3509 3531
3510 3532
(...skipping 11 matching lines...) Expand all
3522 sock_addr_t addrbuf; 3544 sock_addr_t addrbuf;
3523 struct msghdr msg = {0}; 3545 struct msghdr msg = {0};
3524 struct cmsginfo { 3546 struct cmsginfo {
3525 int level; 3547 int level;
3526 int type; 3548 int type;
3527 Py_buffer data; 3549 Py_buffer data;
3528 } *cmsgs = NULL; 3550 } *cmsgs = NULL;
3529 void *controlbuf = NULL; 3551 void *controlbuf = NULL;
3530 size_t controllen, controllen_last; 3552 size_t controllen, controllen_last;
3531 ssize_t bytes_sent = -1; 3553 ssize_t bytes_sent = -1;
3554 int async_err = 0;
3532 int addrlen, timeout, flags = 0; 3555 int addrlen, timeout, flags = 0;
3533 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, 3556 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL,
3534 *cmsg_fast = NULL, *retval = NULL; 3557 *cmsg_fast = NULL, *retval = NULL;
3535 3558
3536 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", 3559 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
3537 &data_arg, &cmsg_arg, &flags, &addr_arg)) 3560 &data_arg, &cmsg_arg, &flags, &addr_arg))
3538 return NULL; 3561 return NULL;
3539 3562
3540 /* Parse destination address. */ 3563 /* Parse destination address. */
3541 if (addr_arg != NULL && addr_arg != Py_None) { 3564 if (addr_arg != NULL && addr_arg != Py_None) {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
3679 } 3702 }
3680 } 3703 }
3681 3704
3682 /* Make the system call. */ 3705 /* Make the system call. */
3683 if (!IS_SELECTABLE(s)) { 3706 if (!IS_SELECTABLE(s)) {
3684 select_error(); 3707 select_error();
3685 goto finally; 3708 goto finally;
3686 } 3709 }
3687 3710
3688 BEGIN_SELECT_LOOP(s) 3711 BEGIN_SELECT_LOOP(s)
3689 Py_BEGIN_ALLOW_THREADS; 3712 do {
3690 timeout = internal_select_ex(s, 1, interval); 3713 Py_BEGIN_ALLOW_THREADS;
3691 if (!timeout) 3714 timeout = internal_select_ex(s, 1, interval);
3692 bytes_sent = sendmsg(s->sock_fd, &msg, flags); 3715 if (!timeout)
3693 Py_END_ALLOW_THREADS; 3716 bytes_sent = sendmsg(s->sock_fd, &msg, flags);
3694 if (timeout == 1) { 3717 Py_END_ALLOW_THREADS;
3695 PyErr_SetString(socket_timeout, "timed out"); 3718 if (timeout == 1) {
3696 goto finally; 3719 PyErr_SetString(socket_timeout, "timed out");
3697 } 3720 goto finally;
3721 }
3722 } while (bytes_sent < 0 && errno == EINTR &&
3723 !(async_err = PyErr_CheckSignals()));
3698 END_SELECT_LOOP(s) 3724 END_SELECT_LOOP(s)
3699 3725
3700 if (bytes_sent < 0) { 3726 if (bytes_sent < 0) {
3701 s->errorhandler(); 3727 if (!async_err)
3728 s->errorhandler();
3702 goto finally; 3729 goto finally;
3703 } 3730 }
3704 retval = PyLong_FromSsize_t(bytes_sent); 3731 retval = PyLong_FromSsize_t(bytes_sent);
3705 3732
3706 finally: 3733 finally:
3707 PyMem_Free(controlbuf); 3734 PyMem_Free(controlbuf);
3708 for (i = 0; i < ncmsgbufs; i++) 3735 for (i = 0; i < ncmsgbufs; i++)
3709 PyBuffer_Release(&cmsgs[i].data); 3736 PyBuffer_Release(&cmsgs[i].data);
3710 PyMem_Free(cmsgs); 3737 PyMem_Free(cmsgs);
3711 Py_XDECREF(cmsg_fast); 3738 Py_XDECREF(cmsg_fast);
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
4192 return PyUnicode_FromWideChar(buf, size); 4219 return PyUnicode_FromWideChar(buf, size);
4193 4220
4194 if (GetLastError() != ERROR_MORE_DATA) 4221 if (GetLastError() != ERROR_MORE_DATA)
4195 return PyErr_SetFromWindowsErr(0); 4222 return PyErr_SetFromWindowsErr(0);
4196 4223
4197 if (size == 0) 4224 if (size == 0)
4198 return PyUnicode_New(0, 0); 4225 return PyUnicode_New(0, 0);
4199 4226
4200 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer 4227 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4201 names */ 4228 names */
4202 name = PyMem_Malloc(size * sizeof(wchar_t)); 4229 name = PyMem_New(wchar_t, size);
4203 if (!name) 4230 if (!name) {
4204 return NULL; 4231 PyErr_NoMemory();
4232 return NULL;
4233 }
4205 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, 4234 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4206 name, 4235 name,
4207 &size)) 4236 &size))
4208 { 4237 {
4209 PyMem_Free(name); 4238 PyMem_Free(name);
4210 return PyErr_SetFromWindowsErr(0); 4239 return PyErr_SetFromWindowsErr(0);
4211 } 4240 }
4212 4241
4213 result = PyUnicode_FromWideChar(name, size); 4242 result = PyUnicode_FromWideChar(name, size);
4214 PyMem_Free(name); 4243 PyMem_Free(name);
(...skipping 1636 matching lines...) Expand 10 before | Expand all | Expand 10 after
5851 PyMODINIT_FUNC 5880 PyMODINIT_FUNC
5852 PyInit__socket(void) 5881 PyInit__socket(void)
5853 { 5882 {
5854 PyObject *m, *has_ipv6; 5883 PyObject *m, *has_ipv6;
5855 5884
5856 if (!os_init()) 5885 if (!os_init())
5857 return NULL; 5886 return NULL;
5858 5887
5859 #ifdef MS_WINDOWS 5888 #ifdef MS_WINDOWS
5860 if (support_wsa_no_inherit == -1) { 5889 if (support_wsa_no_inherit == -1) {
5890 #if defined(_MSC_VER) && _MSC_VER >= 1800
5891 support_wsa_no_inherit = IsWindows7SP1OrGreater();
5892 #else
5861 DWORD version = GetVersion(); 5893 DWORD version = GetVersion();
5862 DWORD major = (DWORD)LOBYTE(LOWORD(version)); 5894 DWORD major = (DWORD)LOBYTE(LOWORD(version));
5863 DWORD minor = (DWORD)HIBYTE(LOWORD(version)); 5895 DWORD minor = (DWORD)HIBYTE(LOWORD(version));
5864 /* need Windows 7 SP1, 2008 R2 SP1 or later */ 5896 /* need Windows 7 SP1, 2008 R2 SP1 or later */
5865 support_wsa_no_inherit = (major >= 6 && minor >= 1); 5897 support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1);
5898 #endif
5866 } 5899 }
5867 #endif 5900 #endif
5868 5901
5869 Py_TYPE(&sock_type) = &PyType_Type; 5902 Py_TYPE(&sock_type) = &PyType_Type;
5870 m = PyModule_Create(&socketmodule); 5903 m = PyModule_Create(&socketmodule);
5871 if (m == NULL) 5904 if (m == NULL)
5872 return NULL; 5905 return NULL;
5873 5906
5874 Py_INCREF(PyExc_OSError); 5907 Py_INCREF(PyExc_OSError);
5875 PySocketModuleAPI.error = PyExc_OSError; 5908 PySocketModuleAPI.error = PyExc_OSError;
(...skipping 1144 matching lines...) Expand 10 before | Expand all | Expand 10 after
7020 return NULL; 7053 return NULL;
7021 memcpy(&packed_addr, src, sizeof(packed_addr)); 7054 memcpy(&packed_addr, src, sizeof(packed_addr));
7022 return strncpy(dst, inet_ntoa(packed_addr), size); 7055 return strncpy(dst, inet_ntoa(packed_addr), size);
7023 } 7056 }
7024 /* Should set errno to EAFNOSUPPORT */ 7057 /* Should set errno to EAFNOSUPPORT */
7025 return NULL; 7058 return NULL;
7026 } 7059 }
7027 7060
7028 #endif 7061 #endif
7029 #endif 7062 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+