From f2ed889027f23ecf184a6ef52b7a7cc4921c3000 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 29 Jul 2014 23:08:00 +0200 Subject: [PATCH 1/2] asyncio: Use the new os.set_blocking() function of Python 3.5 if available --- Lib/asyncio/unix_events.py | 12 ++++++++---- Lib/test/test_asyncio/test_unix_events.py | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 5020cc5db58..8d3e25eb0d1 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -259,10 +259,14 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): return server -def _set_nonblocking(fd): - flags = fcntl.fcntl(fd, fcntl.F_GETFL) - flags = flags | os.O_NONBLOCK - fcntl.fcntl(fd, fcntl.F_SETFL, flags) +if hasattr(os, 'set_blocking'): + def _set_nonblocking(fd): + os.set_blocking(fd, False) +else: + def _set_nonblocking(fd): + flags = fcntl.fcntl(fd, fcntl.F_GETFL) + flags = flags | os.O_NONBLOCK + fcntl.fcntl(fd, fcntl.F_SETFL, flags) class _UnixReadPipeTransport(transports.ReadTransport): diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 099d4d51af4..e397598222d 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -306,9 +306,9 @@ class UnixReadPipeTransportTests(test_utils.TestCase): self.pipe = mock.Mock(spec_set=io.RawIOBase) self.pipe.fileno.return_value = 5 - fcntl_patcher = mock.patch('fcntl.fcntl') - fcntl_patcher.start() - self.addCleanup(fcntl_patcher.stop) + blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking') + blocking_patcher.start() + self.addCleanup(blocking_patcher.stop) fstat_patcher = mock.patch('os.fstat') m_fstat = fstat_patcher.start() @@ -469,9 +469,9 @@ class UnixWritePipeTransportTests(test_utils.TestCase): self.pipe = mock.Mock(spec_set=io.RawIOBase) self.pipe.fileno.return_value = 5 - fcntl_patcher = mock.patch('fcntl.fcntl') - fcntl_patcher.start() - self.addCleanup(fcntl_patcher.stop) + blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking') + blocking_patcher.start() + self.addCleanup(blocking_patcher.stop) fstat_patcher = mock.patch('os.fstat') m_fstat = fstat_patcher.start() From 9c9f1f10d391ff3458a80fc3d0110870d50012e2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 29 Jul 2014 23:08:17 +0200 Subject: [PATCH 2/2] Close #22063: socket operations (socket,recv, sock_sendall, sock_connect, sock_accept) now raise an exception in debug mode if sockets are in blocking mode. --- Lib/asyncio/proactor_events.py | 8 ++++++++ Lib/asyncio/selector_events.py | 8 ++++++++ Lib/test/test_asyncio/test_events.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py index ab566b32757..751155bfed3 100644 --- a/Lib/asyncio/proactor_events.py +++ b/Lib/asyncio/proactor_events.py @@ -385,12 +385,18 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): self._selector = None def sock_recv(self, sock, n): + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") return self._proactor.recv(sock, n) def sock_sendall(self, sock, data): + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") return self._proactor.send(sock, data) def sock_connect(self, sock, address): + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") try: base_events._check_resolved_address(sock, address) except ValueError as err: @@ -401,6 +407,8 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): return self._proactor.connect(sock, address) def sock_accept(self, sock): + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") return self._proactor.accept(sock) def _socketpair(self): diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index eca48b8e7c2..6b7bdf01c13 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -256,6 +256,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): This method is a coroutine. """ + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") fut = futures.Future(loop=self) self._sock_recv(fut, False, sock, n) return fut @@ -292,6 +294,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): This method is a coroutine. """ + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") fut = futures.Future(loop=self) if data: self._sock_sendall(fut, False, sock, data) @@ -333,6 +337,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): This method is a coroutine. """ + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") fut = futures.Future(loop=self) try: base_events._check_resolved_address(sock, address) @@ -374,6 +380,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): This method is a coroutine. """ + if self.get_debug() and sock.gettimeout() != 0: + raise ValueError("the socket must be non-blocking") fut = futures.Future(loop=self) self._sock_accept(fut, False, sock) return fut diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index b0657495ad4..0cff00ae675 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -383,6 +383,24 @@ class EventLoopTestsMixin: self.assertEqual(read, data) def _basetest_sock_client_ops(self, httpd, sock): + # in debug mode, socket operations must fail + # if the socket is not in blocking mode + self.loop.set_debug(True) + sock.setblocking(True) + with self.assertRaises(ValueError): + self.loop.run_until_complete( + self.loop.sock_connect(sock, httpd.address)) + with self.assertRaises(ValueError): + self.loop.run_until_complete( + self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n')) + with self.assertRaises(ValueError): + self.loop.run_until_complete( + self.loop.sock_recv(sock, 1024)) + with self.assertRaises(ValueError): + self.loop.run_until_complete( + self.loop.sock_accept(sock)) + + # test in non-blocking mode sock.setblocking(False) self.loop.run_until_complete( self.loop.sock_connect(sock, httpd.address))