[issue12196] add pipe2() to the os module

Charles-François Natali report at bugs.python.org
Tue May 31 19:28:13 CEST 2011


Charles-François Natali <neologix at free.fr> added the comment:

> I like your patch, it removes many duplicate code :-)
>
> Some comments:

Patch attached.

> Doc/whatsnew/3.3.rst

Patch attached.

----------
Added file: http://bugs.python.org/file22209/pipe2_whatsnew.diff
Added file: http://bugs.python.org/file22210/support_linux_version.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12196>
_______________________________________
-------------- next part --------------
diff -r 6fb2e5fb9082 Doc/whatsnew/3.3.rst
--- a/Doc/whatsnew/3.3.rst	Tue May 31 12:15:42 2011 +0200
+++ b/Doc/whatsnew/3.3.rst	Tue May 31 19:27:08 2011 +0200
@@ -106,6 +106,11 @@
 os
 --
 
+* The :mod:`os` module has a new :func:`~os.pipe2` function that makes it
+  possible to create a pipe with :data:`~os.O_CLOEXEC` or :data:`os.O_NONBLOCK`
+  flags set atomically. This is especially useful to avoid race conditions in
+  multi-threaded programs.
+
 * The :mod:`os` module has a new :func:`~os.sendfile` function which provides
   an efficent "zero-copy" way for copying data from one file (or socket)
   descriptor to another. The phrase "zero-copy" refers to the fact that all of
-------------- next part --------------
diff -r 6fb2e5fb9082 Lib/test/support.py
--- a/Lib/test/support.py	Tue May 31 12:15:42 2011 +0200
+++ b/Lib/test/support.py	Tue May 31 19:20:57 2011 +0200
@@ -291,13 +291,24 @@
             msg = "Use of the `%s' resource not enabled" % resource
         raise ResourceDenied(msg)
 
-def linux_version():
+def requires_linux_version(*min_version):
+    """Raise SkipTest if the OS is Linux and the kernel version if less than
+    min_version.
+
+    For example, support.requires_linux_version(2, 6, 28) raises SkipTest if the
+    version is less than 2.6.28.
+    """
+    if not sys.platform.startswith('linux'):
+        return
     try:
         # platform.release() is something like '2.6.33.7-desktop-2mnb'
-        version_string = platform.release().split('-')[0]
-        return tuple(map(int, version_string.split('.')))
+        version_string = platform.release().split('-', 1)[0]
+        version = tuple(map(int, version_string.split('.')))
     except ValueError:
-        return 0, 0, 0
+        return
+    if version < min_version:
+        raise unittest.SkipTest("Linux kernel %s or higher required" %
+                                ".".join(map(str, min_version)))
 
 HOST = 'localhost'
 
diff -r 6fb2e5fb9082 Lib/test/test_posix.py
--- a/Lib/test/test_posix.py	Tue May 31 12:15:42 2011 +0200
+++ b/Lib/test/test_posix.py	Tue May 31 19:20:57 2011 +0200
@@ -310,10 +310,7 @@
 
     @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
     def test_oscloexec(self):
-        version = support.linux_version()
-        if sys.platform == 'linux2' and version < (2, 6, 23):
-            self.skipTest("Linux kernel 2.6.23 or higher required, "
-                          "not %s.%s.%s" % version)
+        support.requires_linux_version(2, 6, 23)
         fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
         self.addCleanup(os.close, fd)
         self.assertTrue(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
@@ -480,10 +477,7 @@
 
     @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
     def test_pipe2(self):
-        version = support.linux_version()
-        if sys.platform == 'linux2' and version < (2, 6, 27):
-            self.skipTest("Linux kernel 2.6.27 or higher required, "
-                          "not %s.%s.%s" % version)
+        support.requires_linux_version(2, 6, 27)
         self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
         self.assertRaises(TypeError, os.pipe2, 0, 0)
 
diff -r 6fb2e5fb9082 Lib/test/test_socket.py
--- a/Lib/test/test_socket.py	Tue May 31 12:15:42 2011 +0200
+++ b/Lib/test/test_socket.py	Tue May 31 19:20:57 2011 +0200
@@ -1024,10 +1024,7 @@
 
     if hasattr(socket, "SOCK_NONBLOCK"):
         def testInitNonBlocking(self):
-            v = support.linux_version()
-            if v < (2, 6, 28):
-                self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
-                              % ".".join(map(str, v)))
+            support.requires_linux_version(2, 6, 28)
             # reinit server socket
             self.serv.close()
             self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM |
@@ -2002,10 +1999,7 @@
 @unittest.skipUnless(fcntl, "module fcntl not available")
 class CloexecConstantTest(unittest.TestCase):
     def test_SOCK_CLOEXEC(self):
-        v = support.linux_version()
-        if v < (2, 6, 28):
-            self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
-                          % ".".join(map(str, v)))
+        support.requires_linux_version(2, 6, 28)
         with socket.socket(socket.AF_INET,
                            socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
             self.assertTrue(s.type & socket.SOCK_CLOEXEC)
@@ -2024,10 +2018,7 @@
             self.assertEqual(s.gettimeout(), None)
 
     def test_SOCK_NONBLOCK(self):
-        v = support.linux_version()
-        if v < (2, 6, 28):
-            self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
-                          % ".".join(map(str, v)))
+        support.requires_linux_version(2, 6, 28)
         # a lot of it seems silly and redundant, but I wanted to test that
         # changing back and forth worked ok
         with socket.socket(socket.AF_INET,


More information about the Python-bugs-list mailing list