]> granicus.if.org Git - python/commitdiff
asyncio: Use the new os.set_blocking() function of Python 3.5 if available
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 29 Jul 2014 21:08:00 +0000 (23:08 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 29 Jul 2014 21:08:00 +0000 (23:08 +0200)
Lib/asyncio/unix_events.py
Lib/test/test_asyncio/test_unix_events.py

index 5020cc5db58e2441c369dbeba8418dfa506aee45..8d3e25eb0d1f75f2ab47b58c621ee2d890ed3840 100644 (file)
@@ -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):
index 099d4d51af4af461bd7b2af93647a3ae85791a90..e397598222d8867af8d79011206a9af529605075 100644 (file)
@@ -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()