summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-06-05 12:27:45 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-06-05 12:27:45 (GMT)
commitd967fc9da2c5c094865514bd213dd702698e4dad (patch)
tree4cb062e9dc26d0e0f10e89618a9881d7c7a0b306
parent7088b99eb8adfe16b5602bb7447d760915036a26 (diff)
downloadcpython-d967fc9da2c5c094865514bd213dd702698e4dad.zip
cpython-d967fc9da2c5c094865514bd213dd702698e4dad.tar.gz
cpython-d967fc9da2c5c094865514bd213dd702698e4dad.tar.bz2
Issue #21515: tempfile.TemporaryFile now uses os.O_TMPFILE flag is available
-rw-r--r--Doc/library/tempfile.rst7
-rw-r--r--Lib/tempfile.py26
-rw-r--r--Misc/NEWS2
3 files changed, 35 insertions, 0 deletions
diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst
index d78159d..4ad7d9c 100644
--- a/Doc/library/tempfile.rst
+++ b/Doc/library/tempfile.rst
@@ -54,6 +54,13 @@ The module defines the following user-callable items:
underlying true file object. This file-like object can be used in a
:keyword:`with` statement, just like a normal file.
+ The :py:data:`os.O_TMPFILE` flag is used if it is available and works
+ (Linux-specific, require Linux kernel 3.11 or later).
+
+ .. versionchanged:: 3.5
+
+ The :py:data:`os.O_TMPFILE` flag is now used if available.
+
.. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True)
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index f0e25fc..b26e50e 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -473,6 +473,11 @@ if _os.name != 'posix' or _os.sys.platform == 'cygwin':
TemporaryFile = NamedTemporaryFile
else:
+ # Is the O_TMPFILE flag available and does it work?
+ # The flag is set to False if os.open(dir, os.O_TMPFILE) raises an
+ # IsADirectoryError exception
+ _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE')
+
def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
newline=None, suffix="", prefix=template,
dir=None):
@@ -488,11 +493,32 @@ else:
Returns an object with a file-like interface. The file has no
name, and will cease to exist when it is closed.
"""
+ global _O_TMPFILE_WORKS
if dir is None:
dir = gettempdir()
flags = _bin_openflags
+ if _O_TMPFILE_WORKS:
+ try:
+ flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT
+ fd = _os.open(dir, flags2, 0o600)
+ except IsADirectoryError:
+ # Linux kernel older than 3.11 ignores O_TMPFILE flag.
+ # Set flag to None to not try again.
+ _O_TMPFILE_WORKS = False
+ except OSError:
+ # The filesystem of the directory does not support O_TMPFILE.
+ # For example, OSError(95, 'Operation not supported').
+ pass
+ else:
+ try:
+ return _io.open(fd, mode, buffering=buffering,
+ newline=newline, encoding=encoding)
+ except:
+ _os.close(fd)
+ raise
+ # Fallback to _mkstemp_inner().
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
try:
diff --git a/Misc/NEWS b/Misc/NEWS
index ffc1727..8e5fb3a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -88,6 +88,8 @@ Core and Builtins
Library
-------
+- Issue #21515: tempfile.TemporaryFile now uses os.O_TMPFILE flag is available.
+
- Issue #21618: The subprocess module could fail to close open fds that were
inherited by the calling process and already higher than POSIX resource
limits would otherwise allow. On systems with a functioning /proc/self/fd