Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a95483

Browse files
committed
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.
1 parent 99c2ab4 commit 9a95483

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #19827: On UNIX, setblocking() and settimeout() methods of
22+
socket.socket can now avoid a second syscall if the ioctl() function can be
23+
used, or if the non-blocking flag of the socket is unchanged.
24+
2125
- Issue #19785: smtplib now supports SSLContext.check_hostname and server name
2226
indication for TLS/SSL connections.
2327

Modules/socketmodule.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,9 @@ sendsegmented(int sock_fd, char *buf, int len, int flags)
585585
static int
586586
internal_setblocking(PySocketSockObject *s, int block)
587587
{
588-
#ifndef MS_WINDOWS
589-
int delay_flag;
588+
#if !defined(MS_WINDOWS) \
589+
&& !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) || defined(__VMS))
590+
int delay_flag, new_delay_flag;
590591
#endif
591592
#ifdef SOCK_NONBLOCK
592593
if (block)
@@ -597,17 +598,18 @@ internal_setblocking(PySocketSockObject *s, int block)
597598

598599
Py_BEGIN_ALLOW_THREADS
599600
#ifndef MS_WINDOWS
600-
#if defined(__VMS)
601+
#if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) || defined(__VMS)
601602
block = !block;
602603
ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
603-
#else /* !__VMS */
604+
#else
604605
delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
605606
if (block)
606-
delay_flag &= (~O_NONBLOCK);
607+
new_delay_flag = delay_flag & (~O_NONBLOCK);
607608
else
608-
delay_flag |= O_NONBLOCK;
609-
fcntl(s->sock_fd, F_SETFL, delay_flag);
610-
#endif /* !__VMS */
609+
new_delay_flag = delay_flag | O_NONBLOCK;
610+
if (new_delay_flag != delay_flag)
611+
fcntl(s->sock_fd, F_SETFL, new_delay_flag);
612+
#endif
611613
#else /* MS_WINDOWS */
612614
block = !block;
613615
ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);

0 commit comments

Comments
 (0)