[Python-checkins] peps: PEP 466: add test_cloexec.py

victor.stinner python-checkins at python.org
Wed Jul 17 13:15:25 CEST 2013


http://hg.python.org/peps/rev/464590583890
changeset:   5003:464590583890
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Jul 17 13:10:26 2013 +0200
summary:
  PEP 466: add test_cloexec.py

files:
  pep-0466/test_cloexec.py |  50 ++++++++++++++++++++++++++++
  1 files changed, 50 insertions(+), 0 deletions(-)


diff --git a/pep-0466/test_cloexec.py b/pep-0466/test_cloexec.py
new file mode 100644
--- /dev/null
+++ b/pep-0466/test_cloexec.py
@@ -0,0 +1,50 @@
+import os, fcntl, sys, errno
+
+def get_cloexec(fd):
+    try:
+        flags = fcntl.fcntl(fd, fcntl.F_GETFD)
+        return bool(flags & fcntl.FD_CLOEXEC)
+    except IOError as err:
+        if err.errno == errno.EBADF:
+            return '<invalid file descriptor>'
+        else:
+            return str(err)
+
+def set_cloexec(fd):
+    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
+    flags |= fcntl.FD_CLOEXEC
+    fcntl.fcntl(fd, fcntl.F_SETFD, flags)
+
+def main():
+    f = open(__file__, "rb")
+    fd = f.fileno()
+    print("initial state: fd=%s, cloexec=%s" % (fd, get_cloexec(fd)))
+
+
+    pid = os.fork()
+    if not pid:
+        set_cloexec(fd)
+        print("child process after fork, set cloexec: cloexec=%s" % get_cloexec(fd))
+        child_argv = [sys.executable, __file__, str(fd),
+                      'child process after exec']
+        os.execv(child_argv[0], child_argv)
+
+    os.waitpid(pid, 0)
+    print("parent process after fork: cloexec=%s" % get_cloexec(fd))
+    child_argv = [sys.executable, __file__, str(fd),
+                  'parent process after exec']
+    os.execv(child_argv[0], child_argv)
+
+def after_exec():
+    fd = int(sys.argv[1])
+    name = sys.argv[2]
+    print("%s: fd=%s, cloexec=%s"
+          % (name, fd, get_cloexec(fd)))
+    sys.exit()
+
+if __name__ == "__main__":
+    if len(sys.argv) == 1:
+        main()
+    else:
+        after_exec()
+

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


More information about the Python-checkins mailing list