diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-14 15:05:38 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-14 15:05:38 (GMT) |
commit | b1c54967381062bccef7db574b8e84f48a0eca76 (patch) | |
tree | 61af78425836dcb0891283994152df9c0e1b6556 /Modules | |
parent | f4061dac607d9bc2b3af0b72da5e8917428d869e (diff) | |
download | cpython-b1c54967381062bccef7db574b8e84f48a0eca76.zip cpython-b1c54967381062bccef7db574b8e84f48a0eca76.tar.gz cpython-b1c54967381062bccef7db574b8e84f48a0eca76.tar.bz2 |
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
where supported by the system. Patch by Nikita Vetoshkin.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/socketmodule.c | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 94d5f7c..50ca911 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -615,6 +615,12 @@ internal_setblocking(PySocketSockObject *s, int block) #ifndef MS_WINDOWS int 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 @@ -764,12 +770,18 @@ init_sockobject(PySocketSockObject *s, s->sock_family = family; s->sock_type = type; s->sock_proto = proto; - s->sock_timeout = defaulttimeout; s->errorhandler = &set_error; - - if (defaulttimeout >= 0.0) - internal_setblocking(s, 0); +#ifdef SOCK_NONBLOCK + if (type & SOCK_NONBLOCK) + s->sock_timeout = 0.0; + else +#endif + { + s->sock_timeout = defaulttimeout; + if (defaulttimeout >= 0.0) + internal_setblocking(s, 0); + } } @@ -1645,7 +1657,9 @@ sock_accept(PySocketSockObject *s) PyObject *addr = NULL; PyObject *res = NULL; int timeout; - +#ifdef HAVE_ACCEPT4 + int flags = 0; +#endif if (!getsockaddrlen(s, &addrlen)) return NULL; memset(&addrbuf, 0, addrlen); @@ -1656,8 +1670,15 @@ sock_accept(PySocketSockObject *s) BEGIN_SELECT_LOOP(s) Py_BEGIN_ALLOW_THREADS timeout = internal_select_ex(s, 0, interval); - if (!timeout) + if (!timeout) { +#ifdef HAVE_ACCEPT4 + /* inherit socket flags and use accept4 call */ + flags = s->sock_type & (SOCK_CLOEXEC | SOCK_NONBLOCK); + newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, flags); +#else newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); +#endif /* HAVE_ACCEPT4 */ + } Py_END_ALLOW_THREADS if (timeout == 1) { @@ -4599,6 +4620,12 @@ PyInit__socket(void) #if defined(SOCK_RDM) PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); #endif +#ifdef SOCK_CLOEXEC + PyModule_AddIntConstant(m, "SOCK_CLOEXEC", SOCK_CLOEXEC); +#endif +#ifdef SOCK_NONBLOCK + PyModule_AddIntConstant(m, "SOCK_NONBLOCK", SOCK_NONBLOCK); +#endif #ifdef SO_DEBUG PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |