Commit 6ac0a9c6 authored by Victor Stinner's avatar Victor Stinner

Close #19827: On UNIX, setblocking() and settimeout() methods of socket.socket

can now avoid a second syscall if the ioctl() function can be used, or if the
non-blocking flag of the socket is unchanged.
parent f0981052
...@@ -18,6 +18,10 @@ Core and Builtins ...@@ -18,6 +18,10 @@ Core and Builtins
Library Library
------- -------
- Issue #19827: On UNIX, setblocking() and settimeout() methods of
socket.socket can now avoid a second syscall if the ioctl() function can be
used, or if the non-blocking flag of the socket is unchanged.
- Issue #19785: smtplib now supports SSLContext.check_hostname and server name - Issue #19785: smtplib now supports SSLContext.check_hostname and server name
indication for TLS/SSL connections. indication for TLS/SSL connections.
......
...@@ -585,8 +585,9 @@ sendsegmented(int sock_fd, char *buf, int len, int flags) ...@@ -585,8 +585,9 @@ sendsegmented(int sock_fd, char *buf, int len, int flags)
static int static int
internal_setblocking(PySocketSockObject *s, int block) internal_setblocking(PySocketSockObject *s, int block)
{ {
#ifndef MS_WINDOWS #if !defined(MS_WINDOWS) \
int delay_flag; && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) || defined(__VMS))
int delay_flag, new_delay_flag;
#endif #endif
#ifdef SOCK_NONBLOCK #ifdef SOCK_NONBLOCK
if (block) if (block)
...@@ -597,17 +598,18 @@ internal_setblocking(PySocketSockObject *s, int block) ...@@ -597,17 +598,18 @@ internal_setblocking(PySocketSockObject *s, int block)
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
#if defined(__VMS) #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) || defined(__VMS)
block = !block; block = !block;
ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
#else /* !__VMS */ #else
delay_flag = fcntl(s->sock_fd, F_GETFL, 0); delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
if (block) if (block)
delay_flag &= (~O_NONBLOCK); new_delay_flag = delay_flag & (~O_NONBLOCK);
else else
delay_flag |= O_NONBLOCK; new_delay_flag = delay_flag | O_NONBLOCK;
fcntl(s->sock_fd, F_SETFL, delay_flag); if (new_delay_flag != delay_flag)
#endif /* !__VMS */ fcntl(s->sock_fd, F_SETFL, new_delay_flag);
#endif
#else /* MS_WINDOWS */ #else /* MS_WINDOWS */
block = !block; block = !block;
ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment