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 20:03:11 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 20:03:11 2011 +0200 @@ -14,6 +14,7 @@ import stat import unittest import warnings +from test.script_helper import spawn_python class PosixTester(unittest.TestCase): @@ -307,6 +308,24 @@ fp1.close() fp2.close() + @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC") + def test_oscloexec(self): + # open a file with O_CLOEXEC, and check that it's closed in a spawned + # Python interpreter + fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC) + self.addCleanup(os.close, fd) + p = spawn_python("-c", """if 1: + import os, sys, errno + try: + os.close(%d) + except OSError as e: + if e.errno == errno.EBADF: + sys.exit(0) + sys.exit(1)""" % fd, + close_fds=False) + p.communicate() + self.assertEqual(p.returncode, 0) + 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 20:03:11 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;