summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2017-12-18 20:02:54 -0500
committerGitHub <noreply@github.com>2017-12-18 20:02:54 -0500
commit9818142b1bd20243733a953fb8aa2c7be314c47c (patch)
tree625350fae6c199ae5442118eaf36db480fe00046 /Modules/socketmodule.c
parentbpo-32030: Fix compilation on FreeBSD, #include <fenv.h> (#4919) (diff)
downloadcpython-9818142b1bd20243733a953fb8aa2c7be314c47c.tar.gz
cpython-9818142b1bd20243733a953fb8aa2c7be314c47c.tar.bz2
cpython-9818142b1bd20243733a953fb8aa2c7be314c47c.zip
bpo-32331: Fix socket.type when SOCK_NONBLOCK is available (#4877)
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r--Modules/socketmodule.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 91c879f9d63..d52d9db743a 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -582,12 +582,6 @@ internal_setblocking(PySocketSockObject *s, int block)
&& !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)))
int delay_flag, new_delay_flag;
#endif
-#ifdef SOCK_NONBLOCK
- if (block)
- s->sock_type &= (~SOCK_NONBLOCK);
- else
- s->sock_type |= SOCK_NONBLOCK;
-#endif
Py_BEGIN_ALLOW_THREADS
#ifndef MS_WINDOWS
@@ -876,7 +870,22 @@ init_sockobject(PySocketSockObject *s,
{
s->sock_fd = fd;
s->sock_family = family;
+
s->sock_type = type;
+
+ /* It's possible to pass SOCK_NONBLOCK and SOCK_CLOEXEC bit flags
+ on some OSes as part of socket.type. We want to reset them here,
+ to make socket.type be set to the same value on all platforms.
+ Otherwise, simple code like 'if sock.type == SOCK_STREAM' is
+ not portable.
+ */
+#ifdef SOCK_NONBLOCK
+ s->sock_type = s->sock_type & ~SOCK_NONBLOCK;
+#endif
+#ifdef SOCK_CLOEXEC
+ s->sock_type = s->sock_type & ~SOCK_CLOEXEC;
+#endif
+
s->sock_proto = proto;
s->errorhandler = &set_error;