diff -r de01f7c37b53 Lib/tempfile.py --- a/Lib/tempfile.py Sat May 17 20:02:28 2014 -0700 +++ b/Lib/tempfile.py Sun May 18 11:52:43 2014 +0200 @@ -33,6 +33,7 @@ import io as _io import os as _os import shutil as _shutil import errno as _errno +import sys as _sys from random import Random as _Random import weakref as _weakref @@ -55,6 +56,14 @@ if hasattr(_os, 'TMP_MAX'): else: TMP_MAX = 10000 +if hasattr(_os, 'O_TMPFILE'): + _O_TMPFILE = _os.O_TMPFILE +elif _sys.platform == 'linux': + __O_TMPFILE = 0o20000000 + _O_TMPFILE = (__O_TMPFILE | _os.O_DIRECTORY) +else: + _O_TMPFILE = None + # Although it does not have an underscore for historical reasons, this # variable is an internal implementation detail (see issue 10354). template = "tmp" @@ -488,11 +497,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 if dir is None: dir = gettempdir() flags = _bin_openflags + if _O_TMPFILE is not None: + try: + flags2 = (flags | _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 = None + 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: