diff options
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r-- | Lib/tempfile.py | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 34dff30..39ebf5a 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -10,8 +10,6 @@ This module also provides some data items to the user: 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. @@ -33,7 +31,6 @@ import warnings as _warnings import sys as _sys import io as _io import os as _os -import errno as _errno from random import Random as _Random try: @@ -45,7 +42,7 @@ else: def _set_cloexec(fd): try: flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0) - except IOError: + except OSError: pass else: # flags read successfully, modify @@ -74,6 +71,8 @@ if hasattr(_os, 'TMP_MAX'): else: TMP_MAX = 10000 +# Although it does not have an underscore for historical reasons, this +# variable is an internal implementation detail (see issue 10354). template = "tmp" # Internal routines. @@ -85,19 +84,16 @@ if hasattr(_os, "lstat"): elif hasattr(_os, "stat"): _stat = _os.stat else: - # Fallback. All we need is something that raises os.error if the + # Fallback. All we need is something that raises OSError if the # file doesn't exist. def _stat(fn): - try: - f = open(fn) - except IOError: - raise _os.error + f = open(fn) f.close() def _exists(fn): try: _stat(fn) - except _os.error: + except OSError: return False else: return True @@ -149,7 +145,7 @@ def _candidate_tempdir_list(): # As a last resort, the current directory. try: dirlist.append(_os.getcwd()) - except (AttributeError, _os.error): + except (AttributeError, OSError): dirlist.append(_os.curdir) return dirlist @@ -181,12 +177,11 @@ def _get_default_tempdir(): _os.unlink(filename) del fp, fd return dir - except (OSError, IOError) as e: - if e.args[0] != _errno.EEXIST: - break # no point trying more names in this directory + except FileExistsError: pass - raise IOError(_errno.ENOENT, - "No usable temporary directory found in %s" % dirlist) + except OSError: + break # no point trying more names in this directory + raise FileNotFoundError("No usable temporary directory found in %s" % dirlist) _name_sequence = None @@ -216,12 +211,10 @@ def _mkstemp_inner(dir, pre, suf, flags): fd = _os.open(file, flags, 0o600) _set_cloexec(fd) return (fd, _os.path.abspath(file)) - except OSError as e: - if e.errno == _errno.EEXIST: - continue # try again - raise + except FileExistsError: + continue # try again - raise IOError(_errno.EEXIST, "No usable temporary file name found") + raise FileExistsError("No usable temporary file name found") # User visible interfaces. @@ -305,12 +298,10 @@ def mkdtemp(suffix="", prefix=template, dir=None): try: _os.mkdir(file, 0o700) return file - except OSError as e: - if e.errno == _errno.EEXIST: - continue # try again - raise + except FileExistsError: + continue # try again - raise IOError(_errno.EEXIST, "No usable temporary directory name found") + raise FileExistsError("No usable temporary directory name found") def mktemp(suffix="", prefix=template, dir=None): """User-callable function to return a unique temporary file name. The @@ -339,7 +330,7 @@ def mktemp(suffix="", prefix=template, dir=None): if not _exists(file): return file - raise IOError(_errno.EEXIST, "No usable temporary filename found") + raise FileExistsError("No usable temporary filename found") class _TemporaryFileWrapper: @@ -592,8 +583,13 @@ class SpooledTemporaryFile: def tell(self): return self._file.tell() - def truncate(self): - self._file.truncate() + def truncate(self, size=None): + if size is None: + self._file.truncate() + else: + if size > self._max_size: + self.rollover() + self._file.truncate(size) def write(self, s): file = self._file @@ -669,7 +665,7 @@ class TemporaryDirectory(object): _islink = staticmethod(_os.path.islink) _remove = staticmethod(_os.remove) _rmdir = staticmethod(_os.rmdir) - _os_error = _os.error + _os_error = OSError _warn = _warnings.warn def _rmtree(self, path): |