tempfile.py 18.2 KB
Newer Older
1
"""Temporary files.
2

3 4 5 6 7
This module provides generic, low- and high-level interfaces for
creating temporary files and directories.  The interfaces listed
as "safe" just below can be used without fear of race conditions.
Those listed as "unsafe" cannot, and are provided for backward
compatibility only.
Guido van Rossum's avatar
Guido van Rossum committed
8

9
This module also provides some data items to the user:
Guido van Rossum's avatar
Guido van Rossum committed
10

11 12 13 14 15 16 17 18 19 20 21
  TMP_MAX  - maximum number of names that will be tried before
             giving up.
  template - the default prefix for all temporary names.
             You may change this to control the default prefix.
  tempdir  - If this is set to a string before the first use of
             any routine from this module, it will be considered as
             another candidate location to store temporary files.
"""

__all__ = [
    "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
22
    "SpooledTemporaryFile",
23 24 25 26 27 28 29 30 31
    "mkstemp", "mkdtemp",                  # low level safe interfaces
    "mktemp",                              # deprecated unsafe interface
    "TMP_MAX", "gettempprefix",            # constants
    "tempdir", "gettempdir"
   ]


# Imports.

32
import io as _io
33 34 35 36 37 38
import os as _os
import errno as _errno
from random import Random as _Random

try:
    import fcntl as _fcntl
39
except ImportError:
40 41 42
    def _set_cloexec(fd):
        pass
else:
43
    def _set_cloexec(fd):
44 45 46 47
        try:
            flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
        except IOError:
            pass
48
        else:
49 50 51
            # flags read successfully, modify
            flags |= _fcntl.FD_CLOEXEC
            _fcntl.fcntl(fd, _fcntl.F_SETFD, flags)
52

53 54

try:
55
    import _thread
56
except ImportError:
57
    import _dummy_thread as _thread
58
_allocate_lock = _thread.allocate_lock
59 60

_text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL
Tim Peters's avatar
Tim Peters committed
61 62 63 64
if hasattr(_os, 'O_NOINHERIT'):
    _text_openflags |= _os.O_NOINHERIT
if hasattr(_os, 'O_NOFOLLOW'):
    _text_openflags |= _os.O_NOFOLLOW
65 66

_bin_openflags = _text_openflags
Tim Peters's avatar
Tim Peters committed
67 68
if hasattr(_os, 'O_BINARY'):
    _bin_openflags |= _os.O_BINARY
69 70 71 72 73 74

if hasattr(_os, 'TMP_MAX'):
    TMP_MAX = _os.TMP_MAX
else:
    TMP_MAX = 10000

75
template = "tmp"
76

77 78 79 80
# Internal routines.

_once_lock = _allocate_lock()

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
if hasattr(_os, "lstat"):
    _stat = _os.lstat
elif hasattr(_os, "stat"):
    _stat = _os.stat
else:
    # Fallback.  All we need is something that raises os.error if the
    # file doesn't exist.
    def _stat(fn):
        try:
            f = open(fn)
        except IOError:
            raise _os.error
        f.close()

def _exists(fn):
    try:
        _stat(fn)
    except _os.error:
        return False
    else:
        return True

103 104 105 106 107 108 109 110
class _RandomNameSequence:
    """An instance of _RandomNameSequence generates an endless
    sequence of unpredictable strings which can safely be incorporated
    into file names.  Each string is six characters long.  Multiple
    threads can safely use the same instance at the same time.

    _RandomNameSequence is an iterator."""

111 112
    characters = ("abcdefghijklmnopqrstuvwxyz" +
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
113
                  "0123456789_")
114 115 116 117 118

    def __init__(self):
        self.mutex = _allocate_lock()
        self.rng = _Random()
        self.normcase = _os.path.normcase
119

120 121 122
    def __iter__(self):
        return self

123
    def __next__(self):
124 125
        m = self.mutex
        c = self.characters
126
        choose = self.rng.choice
127

128
        m.acquire()
129
        try:
130
            letters = [choose(c) for dummy in "123456"]
131 132 133
        finally:
            m.release()

134
        return self.normcase(''.join(letters))
135 136 137 138 139 140 141 142

def _candidate_tempdir_list():
    """Generate a list of candidate temporary directories which
    _get_default_tempdir will try."""

    dirlist = []

    # First, try the environment.
143
    for envname in 'TMPDIR', 'TEMP', 'TMP':
144 145 146 147
        dirname = _os.getenv(envname)
        if dirname: dirlist.append(dirname)

    # Failing that, try OS-specific locations.
148
    if _os.name == 'nt':
149 150 151 152 153 154 155 156 157 158 159
        dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
    else:
        dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])

    # As a last resort, the current directory.
    try:
        dirlist.append(_os.getcwd())
    except (AttributeError, _os.error):
        dirlist.append(_os.curdir)

    return dirlist
Tim Peters's avatar
Tim Peters committed
160

161 162
def _get_default_tempdir():
    """Calculate the default directory to use for temporary files.
163
    This routine should be called exactly once.
164 165 166 167 168 169 170 171 172 173 174 175 176

    We determine whether or not a candidate temp dir is usable by
    trying to create and write to a file in that directory.  If this
    is successful, the test file is deleted.  To prevent denial of
    service, the name of the test file must be randomized."""

    namer = _RandomNameSequence()
    dirlist = _candidate_tempdir_list()

    for dir in dirlist:
        if dir != _os.curdir:
            dir = _os.path.normcase(_os.path.abspath(dir))
        # Try only a few names per directory.
177
        for seq in range(100):
178
            name = next(namer)
179 180
            filename = _os.path.join(dir, name)
            try:
181
                fd = _os.open(filename, _bin_openflags, 0o600)
182 183
                fp = _io.open(fd, 'wb')
                fp.write(b'blat')
Tim Peters's avatar
Tim Peters committed
184
                fp.close()
185 186 187
                _os.unlink(filename)
                del fp, fd
                return dir
188
            except (OSError, IOError) as e:
Georg Brandl's avatar
Georg Brandl committed
189
                if e.args[0] != _errno.EEXIST:
190 191
                    break # no point trying more names in this directory
                pass
192 193
    raise IOError(_errno.ENOENT,
                  "No usable temporary directory found in %s" % dirlist)
194

195 196
_name_sequence = None

197 198
def _get_candidate_names():
    """Common setup sequence for all user-callable interfaces."""
199

200 201 202 203 204 205 206 207
    global _name_sequence
    if _name_sequence is None:
        _once_lock.acquire()
        try:
            if _name_sequence is None:
                _name_sequence = _RandomNameSequence()
        finally:
            _once_lock.release()
208 209 210 211 212 213 214 215
    return _name_sequence


def _mkstemp_inner(dir, pre, suf, flags):
    """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""

    names = _get_candidate_names()

216
    for seq in range(TMP_MAX):
217
        name = next(names)
218 219
        file = _os.path.join(dir, pre + name + suf)
        try:
220
            fd = _os.open(file, flags, 0o600)
221
            _set_cloexec(fd)
222
            return (fd, _os.path.abspath(file))
223
        except OSError as e:
224 225 226 227
            if e.errno == _errno.EEXIST:
                continue # try again
            raise

228
    raise IOError(_errno.EEXIST, "No usable temporary file name found")
Tim Peters's avatar
Tim Peters committed
229

230 231

# User visible interfaces.
232

233
def gettempprefix():
234 235 236
    """Accessor for tempdir.template."""
    return template

237 238
tempdir = None

239
def gettempdir():
Christian Heimes's avatar
Christian Heimes committed
240
    """Accessor for tempfile.tempdir."""
241 242 243 244 245 246 247 248
    global tempdir
    if tempdir is None:
        _once_lock.acquire()
        try:
            if tempdir is None:
                tempdir = _get_default_tempdir()
        finally:
            _once_lock.release()
249 250
    return tempdir

251
def mkstemp(suffix="", prefix=template, dir=None, text=False):
Christian Heimes's avatar
Christian Heimes committed
252
    """User-callable function to create and return a unique temporary
253 254 255 256 257 258 259 260 261 262 263 264
    file.  The return value is a pair (fd, name) where fd is the
    file descriptor returned by os.open, and name is the filename.

    If 'suffix' is specified, the file name will end with that suffix,
    otherwise there will be no suffix.

    If 'prefix' is specified, the file name will begin with that prefix,
    otherwise a default prefix is used.

    If 'dir' is specified, the file will be created in that directory,
    otherwise a default directory is used.

265 266 267
    If 'text' is specified and true, the file is opened in text
    mode.  Else (the default) the file is opened in binary mode.  On
    some operating systems, this makes no difference.
268 269 270 271 272

    The file is readable and writable only by the creating user ID.
    If the operating system uses permission bits to indicate whether a
    file is executable, the file is executable by no one. The file
    descriptor is not inherited by children of this process.
273

274
    Caller is responsible for deleting the file when done with it.
275 276
    """

277 278 279
    if dir is None:
        dir = gettempdir()

280
    if text:
281
        flags = _text_openflags
282 283
    else:
        flags = _bin_openflags
284

285
    return _mkstemp_inner(dir, prefix, suffix, flags)
Guido van Rossum's avatar
Guido van Rossum committed
286

287

288
def mkdtemp(suffix="", prefix=template, dir=None):
Christian Heimes's avatar
Christian Heimes committed
289
    """User-callable function to create and return a unique temporary
290 291
    directory.  The return value is the pathname of the directory.

292
    Arguments are as for mkstemp, except that the 'text' argument is
293 294 295 296 297 298 299 300
    not accepted.

    The directory is readable, writable, and searchable only by the
    creating user.

    Caller is responsible for deleting the directory when done with it.
    """

301 302 303
    if dir is None:
        dir = gettempdir()

304
    names = _get_candidate_names()
Tim Peters's avatar
Tim Peters committed
305

306
    for seq in range(TMP_MAX):
307
        name = next(names)
308 309
        file = _os.path.join(dir, prefix + name + suffix)
        try:
310
            _os.mkdir(file, 0o700)
311
            return file
312
        except OSError as e:
313 314 315
            if e.errno == _errno.EEXIST:
                continue # try again
            raise
316

317
    raise IOError(_errno.EEXIST, "No usable temporary directory name found")
318

319
def mktemp(suffix="", prefix=template, dir=None):
Christian Heimes's avatar
Christian Heimes committed
320
    """User-callable function to return a unique temporary file name.  The
321
    file is not created.
322

323
    Arguments are as for mkstemp, except that the 'text' argument is
324 325 326 327 328 329
    not accepted.

    This function is unsafe and should not be used.  The file name
    refers to a file that did not exist at some point, but by the time
    you get around to creating it, someone else may have beaten you to
    the punch.
330
    """
331

332 333 334
##    from warnings import warn as _warn
##    _warn("mktemp is a potential security risk to your program",
##          RuntimeWarning, stacklevel=2)
335

336 337 338
    if dir is None:
        dir = gettempdir()

339
    names = _get_candidate_names()
340
    for seq in range(TMP_MAX):
341
        name = next(names)
342
        file = _os.path.join(dir, prefix + name + suffix)
343
        if not _exists(file):
344 345
            return file

346
    raise IOError(_errno.EEXIST, "No usable temporary filename found")
347

Christian Heimes's avatar
Christian Heimes committed
348

349 350 351 352 353 354 355
class _TemporaryFileWrapper:
    """Temporary file wrapper

    This class provides a wrapper around files opened for
    temporary use.  In particular, it seeks to automatically
    remove the file when it is no longer needed.
    """
356

357
    def __init__(self, file, name, delete=True):
358 359
        self.file = file
        self.name = name
360
        self.close_called = False
361
        self.delete = delete
362 363

    def __getattr__(self, name):
Christian Heimes's avatar
Christian Heimes committed
364 365 366
        # Attribute lookups are delegated to the underlying file
        # and cached for non-numeric results
        # (i.e. methods are cached, closed and friends are not)
367 368
        file = self.__dict__['file']
        a = getattr(file, name)
Christian Heimes's avatar
Christian Heimes committed
369
        if not isinstance(a, int):
370
            setattr(self, name, a)
371
        return a
372

Christian Heimes's avatar
Christian Heimes committed
373 374 375 376 377 378
    # The underlying __enter__ method returns the wrong object
    # (self.file) so override it to return the wrapper
    def __enter__(self):
        self.file.__enter__()
        return self

Christian Heimes's avatar
Christian Heimes committed
379
    # iter() doesn't use __getattr__ to find the __iter__ method
380
    def __iter__(self):
Christian Heimes's avatar
Christian Heimes committed
381
        return iter(self.file)
382

383 384 385 386 387 388 389 390 391 392 393 394 395
    # NT provides delete-on-close as a primitive, so we don't need
    # the wrapper to do anything special.  We still use it so that
    # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile.
    if _os.name != 'nt':
        # Cache the unlinker so we don't get spurious errors at
        # shutdown when the module-level "os" is None'd out.  Note
        # that this must be referenced as self.unlink, because the
        # name TemporaryFileWrapper may also get None'd out before
        # __del__ is called.
        unlink = _os.unlink

        def close(self):
            if not self.close_called:
396
                self.close_called = True
397
                self.file.close()
398 399
                if self.delete:
                    self.unlink(self.name)
400 401 402 403

        def __del__(self):
            self.close()

Christian Heimes's avatar
Christian Heimes committed
404 405 406 407 408 409
        # Need to trap __exit__ as well to ensure the file gets
        # deleted when used in a with statement
        def __exit__(self, exc, value, tb):
            result = self.file.__exit__(exc, value, tb)
            self.close()
            return result
410 411
    else:
        def __exit__(self, exc, value, tb):
412
            self.file.__exit__(exc, value, tb)
Christian Heimes's avatar
Christian Heimes committed
413 414


415 416 417
def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
                       newline=None, suffix="", prefix=template,
                       dir=None, delete=True):
418 419 420
    """Create and return a temporary file.
    Arguments:
    'prefix', 'suffix', 'dir' -- as for mkstemp.
421 422 423 424
    'mode' -- the mode argument to io.open (default "w+b").
    'buffering' -- the buffer size argument to io.open (default -1).
    'encoding' -- the encoding argument to io.open (default None)
    'newline' -- the newline argument to io.open (default None)
425
    'delete' -- whether the file is deleted on close (default True).
426 427
    The file is created as mkstemp() would do it.

428 429
    Returns an object with a file-like interface; the name of the file
    is accessible as file.name.  The file will be automatically deleted
430
    when it is closed unless the 'delete' argument is set to False.
431
    """
432

433 434 435
    if dir is None:
        dir = gettempdir()

436
    flags = _bin_openflags
437

438 439
    # Setting O_TEMPORARY in the flags causes the OS to delete
    # the file when it is closed.  This is only supported by Windows.
440
    if _os.name == 'nt' and delete:
441
        flags |= _os.O_TEMPORARY
442

443
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
444 445 446
    file = _io.open(fd, mode, buffering=buffering,
                    newline=newline, encoding=encoding)

447
    return _TemporaryFileWrapper(file, name, delete)
448

449 450 451
if _os.name != 'posix' or _os.sys.platform == 'cygwin':
    # On non-POSIX and Cygwin systems, assume that we cannot unlink a file
    # while it is open.
452
    TemporaryFile = NamedTemporaryFile
453 454

else:
455 456 457
    def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
                      newline=None, suffix="", prefix=template,
                      dir=None):
458 459
        """Create and return a temporary file.
        Arguments:
460
        'prefix', 'suffix', 'dir' -- as for mkstemp.
461 462 463 464
        'mode' -- the mode argument to io.open (default "w+b").
        'buffering' -- the buffer size argument to io.open (default -1).
        'encoding' -- the encoding argument to io.open (default None)
        'newline' -- the newline argument to io.open (default None)
465 466
        The file is created as mkstemp() would do it.

467 468
        Returns an object with a file-like interface.  The file has no
        name, and will cease to exist when it is closed.
469 470
        """

471 472 473
        if dir is None:
            dir = gettempdir()

474
        flags = _bin_openflags
475 476 477 478

        (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
        try:
            _os.unlink(name)
479 480
            return _io.open(fd, mode, buffering=buffering,
                            newline=newline, encoding=encoding)
481 482 483
        except:
            _os.close(fd)
            raise
484 485 486 487 488 489 490 491

class SpooledTemporaryFile:
    """Temporary file wrapper, specialized to switch from
    StringIO to a real file when it exceeds a certain size or
    when a fileno is needed.
    """
    _rolled = False

492 493
    def __init__(self, max_size=0, mode='w+b', buffering=-1,
                 encoding=None, newline=None,
494
                 suffix="", prefix=template, dir=None):
495 496 497
        if 'b' in mode:
            self._file = _io.BytesIO()
        else:
498 499
            # Setting newline="\n" avoids newline translation;
            # this is important because otherwise on Windows we'd
500
            # hget double newline translation upon rollover().
501
            self._file = _io.StringIO(newline="\n")
502 503
        self._max_size = max_size
        self._rolled = False
504 505 506 507
        self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering,
                                   'suffix': suffix, 'prefix': prefix,
                                   'encoding': encoding, 'newline': newline,
                                   'dir': dir}
508 509 510 511 512 513 514 515 516 517

    def _check(self, file):
        if self._rolled: return
        max_size = self._max_size
        if max_size and file.tell() > max_size:
            self.rollover()

    def rollover(self):
        if self._rolled: return
        file = self._file
518
        newfile = self._file = TemporaryFile(**self._TemporaryFileArgs)
519 520 521 522 523 524 525
        del self._TemporaryFileArgs

        newfile.write(file.getvalue())
        newfile.seek(file.tell(), 0)

        self._rolled = True

Christian Heimes's avatar
Christian Heimes committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539
    # The method caching trick from NamedTemporaryFile
    # won't work here, because _file may change from a
    # _StringIO instance to a real file. So we list
    # all the methods directly.

    # Context management protocol
    def __enter__(self):
        if self._file.closed:
            raise ValueError("Cannot enter context with closed file")
        return self

    def __exit__(self, exc, value, tb):
        self._file.close()

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    # file protocol
    def __iter__(self):
        return self._file.__iter__()

    def close(self):
        self._file.close()

    @property
    def closed(self):
        return self._file.closed

    @property
    def encoding(self):
        return self._file.encoding

    def fileno(self):
        self.rollover()
        return self._file.fileno()

    def flush(self):
        self._file.flush()

    def isatty(self):
        return self._file.isatty()

    @property
    def mode(self):
        return self._file.mode

    @property
    def name(self):
        return self._file.name

    @property
    def newlines(self):
        return self._file.newlines

    def next(self):
        return self._file.next

    def read(self, *args):
        return self._file.read(*args)

    def readline(self, *args):
        return self._file.readline(*args)

    def readlines(self, *args):
        return self._file.readlines(*args)

    def seek(self, *args):
        self._file.seek(*args)

    @property
    def softspace(self):
        return self._file.softspace

    def tell(self):
        return self._file.tell()

    def truncate(self):
        self._file.truncate()

    def write(self, s):
        file = self._file
        rv = file.write(s)
        self._check(file)
        return rv

    def writelines(self, iterable):
        file = self._file
        rv = file.writelines(iterable)
        self._check(file)
        return rv

    def xreadlines(self, *args):
        return self._file.xreadlines(*args)