diff options
author | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2002-03-24 22:21:48 (GMT) |
---|---|---|
committer | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2002-03-24 22:21:48 (GMT) |
commit | 153cc0fddc33415c35a071d351442e5cb889b5d7 (patch) | |
tree | f4ec4730e098d9fd16d4f6f7fca5090dec0fd63d | |
parent | 1c90d7ab3c9a13e8578d6148cd33b5ace23f3aec (diff) | |
download | cpython-153cc0fddc33415c35a071d351442e5cb889b5d7.zip cpython-153cc0fddc33415c35a071d351442e5cb889b5d7.tar.gz cpython-153cc0fddc33415c35a071d351442e5cb889b5d7.tar.bz2 |
If possible, set FD_CLOEXEC flag on file descriptors opened using
TemporaryFile. This flag causes the fd to be closed on exec().
-rw-r--r-- | Lib/tempfile.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index b981084..7492240 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -180,6 +180,17 @@ class TemporaryFileWrapper: setattr(self, name, a) return a +try: + import fcntl as _fcntl + def _set_cloexec(fd, flag=_fcntl.FD_CLOEXEC): + flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0) + if flags >= 0: + # flags read successfully, modify + flags |= flag + _fcntl.fcntl(fd, _fcntl.F_SETFD, flags) +except (ImportError, AttributeError): + def _set_cloexec(fd): + pass def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): """Create and return a temporary file (opened read-write by default).""" @@ -187,6 +198,7 @@ def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): if os.name == 'posix': # Unix -- be very careful fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700) + _set_cloexec(fd) try: os.unlink(name) return os.fdopen(fd, mode, bufsize) |