cpython: 4b51a992cb70 (original) (raw)
Mercurial > cpython
changeset 91033:4b51a992cb70
Issue #21515: tempfile.TemporaryFile now uses os.O_TMPFILE flag is available [#21515]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Thu, 05 Jun 2014 14:27:45 +0200 |
parents | 782c3b4cbc88 |
children | b3063de0dbd9 |
files | Doc/library/tempfile.rst Lib/tempfile.py Misc/NEWS |
diffstat | 3 files changed, 35 insertions(+), 0 deletions(-)[+] [-] Doc/library/tempfile.rst 7 Lib/tempfile.py 26 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Doc/library/tempfile.rst
+++ b/Doc/library/tempfile.rst
@@ -54,6 +54,13 @@ The module defines the following user-ca
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.[](#l1.12)
+ .. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True)
--- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -473,6 +473,11 @@ if _os.name != 'posix' or _os.sys.platfo 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[](#l2.19)
if dir is None: dir = gettempdir() flags = _bin_openflags
if _O_TMPFILE_WORKS:[](#l2.25)
try:[](#l2.26)
flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT[](#l2.27)
fd = _os.open(dir, flags2, 0o600)[](#l2.28)
except IsADirectoryError:[](#l2.29)
# Linux kernel older than 3.11 ignores O_TMPFILE flag.[](#l2.30)
# Set flag to None to not try again.[](#l2.31)
_O_TMPFILE_WORKS = False[](#l2.32)
except OSError:[](#l2.33)
# The filesystem of the directory does not support O_TMPFILE.[](#l2.34)
# For example, OSError(95, 'Operation not supported').[](#l2.35)
pass[](#l2.36)
else:[](#l2.37)
try:[](#l2.38)
return _io.open(fd, mode, buffering=buffering,[](#l2.39)
newline=newline, encoding=encoding)[](#l2.40)
except:[](#l2.41)
_os.close(fd)[](#l2.42)
raise[](#l2.43)
# Fallback to _mkstemp_inner().[](#l2.44)
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) try:
--- 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. +