[Python-checkins] cpython (3.4): asyncio: Use the new os.set_blocking() function of Python 3.5 if available

victor.stinner python-checkins at python.org
Tue Jul 29 23:11:24 CEST 2014


http://hg.python.org/cpython/rev/e5cf8ea793c7
changeset:   91928:e5cf8ea793c7
branch:      3.4
parent:      91926:d4cf32230ed7
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Jul 29 23:08:00 2014 +0200
summary:
  asyncio: Use the new os.set_blocking() function of Python 3.5 if available

files:
  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
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -259,10 +259,14 @@
         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
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -306,9 +306,9 @@
         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 @@
         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()

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list