mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
If possible, set FD_CLOEXEC flag on file descriptors opened using
TemporaryFile. This flag causes the fd to be closed on exec().
This commit is contained in:
parent
1c90d7ab3c
commit
153cc0fddc
1 changed files with 12 additions and 0 deletions
|
@ -180,6 +180,17 @@ class TemporaryFileWrapper:
|
||||||
setattr(self, name, a)
|
setattr(self, name, a)
|
||||||
return 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=""):
|
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
|
||||||
"""Create and return a temporary file (opened read-write by default)."""
|
"""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':
|
if os.name == 'posix':
|
||||||
# Unix -- be very careful
|
# Unix -- be very careful
|
||||||
fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
|
fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
|
||||||
|
_set_cloexec(fd)
|
||||||
try:
|
try:
|
||||||
os.unlink(name)
|
os.unlink(name)
|
||||||
return os.fdopen(fd, mode, bufsize)
|
return os.fdopen(fd, mode, bufsize)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue