[issue12105] open() does not able to set flags, such as O_CLOEXEC

Charles-François Natali report at bugs.python.org
Thu May 19 22:34:20 CEST 2011


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

> Using spawn_python() to check that os.O_CLOEXEC flag is correctly set seems
> overkill. Why not just testing fcntl.fcntl(f.fileno(), fcntl.F_GETFL) &
> FD_CLOEXEC)?

Because I couldn't find a place where the CLOEXEC flag was fully
tested (I mean, checking in the child process that the FD was
correctly closed), so I took the opportunity to test it thoroughly
here.
But you're right it's maybe a little bit overkill, so here's a patch
using just F_GETFL. Pick up whichever you like.

>> Note that I'm not sure that adding this flag to built-in open()
>> is necessarily a good idea
>
> I agree.
>

OK.

> open() documentation may explain the os.fdopen(os.open()) "trick" to use
> low-level options like O_SYNC or O_CLOEXEC.
>

Why not, but I leave it to someone more comfortable with documentation
than me :-)

----------
Added file: http://bugs.python.org/file22032/os_cloexec_1.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12105>
_______________________________________
-------------- next part --------------
diff -r 9d1fb6a9104b Doc/library/os.rst
--- a/Doc/library/os.rst	Thu May 19 19:56:12 2011 +0200
+++ b/Doc/library/os.rst	Thu May 19 22:21:09 2011 +0200
@@ -1298,6 +1298,7 @@
           O_NOCTTY
           O_SHLOCK
           O_EXLOCK
+          O_CLOEXEC
 
    These constants are only available on Unix.
 
diff -r 9d1fb6a9104b Lib/test/test_posix.py
--- a/Lib/test/test_posix.py	Thu May 19 19:56:12 2011 +0200
+++ b/Lib/test/test_posix.py	Thu May 19 22:21:09 2011 +0200
@@ -9,6 +9,7 @@
 import sys
 import time
 import os
+import fcntl
 import pwd
 import shutil
 import stat
@@ -307,6 +308,12 @@
                 fp1.close()
                 fp2.close()
 
+    @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
+    def test_oscloexec(self):
+        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)
+
     def test_osexlock(self):
         if hasattr(posix, "O_EXLOCK"):
             fd = os.open(support.TESTFN,
diff -r 9d1fb6a9104b Modules/posixmodule.c
--- a/Modules/posixmodule.c	Thu May 19 19:56:12 2011 +0200
+++ b/Modules/posixmodule.c	Thu May 19 22:21:09 2011 +0200
@@ -9783,6 +9783,9 @@
 #ifdef PRIO_USER
     if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
 #endif
+#ifdef O_CLOEXEC
+    if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
+#endif
 /* posix - constants for *at functions */
 #ifdef AT_SYMLINK_NOFOLLOW
         if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;


More information about the Python-bugs-list mailing list