diff options
author | Guido van Rossum <guido@python.org> | 1998-03-26 21:13:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-03-26 21:13:24 (GMT) |
commit | 45e2fbc2e70ef28b1f0327207f33dab3a4e825c5 (patch) | |
tree | 24cafdb6ffb07170188292a02440935291327cde /Lib/tempfile.py | |
parent | 9ea7024754f0e42d7fc70fd1c8f6f6cfbf7e1cf0 (diff) | |
download | cpython-45e2fbc2e70ef28b1f0327207f33dab3a4e825c5.zip cpython-45e2fbc2e70ef28b1f0327207f33dab3a4e825c5.tar.gz cpython-45e2fbc2e70ef28b1f0327207f33dab3a4e825c5.tar.bz2 |
Mass check-in after untabifying all files that need it.
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r-- | Lib/tempfile.py | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 5b4e388..bd0ba60 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -19,55 +19,55 @@ template = None def gettempdir(): global tempdir if tempdir is not None: - return tempdir + return tempdir attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir] if os.name == 'nt': - attempdirs.insert(0, 'C:\\TEMP') - attempdirs.insert(0, '\\TEMP') + attempdirs.insert(0, 'C:\\TEMP') + attempdirs.insert(0, '\\TEMP') elif os.name == 'mac': - import macfs, MACFS - try: - refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, - MACFS.kTemporaryFolderType, 0) - dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname() - attempdirs.insert(0, dirname) - except macfs.error: - pass + import macfs, MACFS + try: + refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, + MACFS.kTemporaryFolderType, 0) + dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname() + attempdirs.insert(0, dirname) + except macfs.error: + pass for envname in 'TMPDIR', 'TEMP', 'TMP': - if os.environ.has_key(envname): - attempdirs.insert(0, os.environ[envname]) + if os.environ.has_key(envname): + attempdirs.insert(0, os.environ[envname]) testfile = gettempprefix() + 'test' for dir in attempdirs: - try: - filename = os.path.join(dir, testfile) - fp = open(filename, 'w') - fp.write('blat') - fp.close() - os.unlink(filename) - tempdir = dir - break - except IOError: - pass + try: + filename = os.path.join(dir, testfile) + fp = open(filename, 'w') + fp.write('blat') + fp.close() + os.unlink(filename) + tempdir = dir + break + except IOError: + pass if tempdir is None: - msg = "Can't find a usable temporary directory amongst " + `attempdirs` - raise IOError, msg + msg = "Can't find a usable temporary directory amongst " + `attempdirs` + raise IOError, msg return tempdir # Function to calculate a prefix of the filename to use def gettempprefix(): - global template - if template == None: - if os.name == 'posix': - template = '@' + `os.getpid()` + '.' - elif os.name == 'nt': - template = '~' + `os.getpid()` + '-' - elif os.name == 'mac': - template = 'Python-Tmp-' - else: - template = 'tmp' # XXX might choose a better one - return template + global template + if template == None: + if os.name == 'posix': + template = '@' + `os.getpid()` + '.' + elif os.name == 'nt': + template = '~' + `os.getpid()` + '-' + elif os.name == 'mac': + template = 'Python-Tmp-' + else: + template = 'tmp' # XXX might choose a better one + return template # Counter for generating unique names @@ -78,14 +78,14 @@ counter = 0 # User-callable function to return a unique temporary file name def mktemp(suffix=""): - global counter - dir = gettempdir() - pre = gettempprefix() - while 1: - counter = counter + 1 - file = os.path.join(dir, pre + `counter` + suffix) - if not os.path.exists(file): - return file + global counter + dir = gettempdir() + pre = gettempprefix() + while 1: + counter = counter + 1 + file = os.path.join(dir, pre + `counter` + suffix) + if not os.path.exists(file): + return file class TemporaryFileWrapper: @@ -96,31 +96,31 @@ class TemporaryFileWrapper: no longer needed. """ def __init__(self, file, path): - self.file = file - self.path = path + self.file = file + self.path = path def close(self): - self.file.close() - os.unlink(self.path) + self.file.close() + os.unlink(self.path) def __del__(self): - try: self.close() - except: pass + try: self.close() + except: pass def __getattr__(self, name): - file = self.__dict__['file'] - a = getattr(file, name) - setattr(self, name, a) - return a + file = self.__dict__['file'] + a = getattr(file, name) + setattr(self, name, a) + return a def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): name = mktemp(suffix) file = open(name, mode, bufsize) try: - os.unlink(name) + os.unlink(name) except os.error: - # Non-unix -- can't unlink file that's still open, use wrapper - return TemporaryFileWrapper(file, name) + # Non-unix -- can't unlink file that's still open, use wrapper + return TemporaryFileWrapper(file, name) else: - return file + return file |