summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-05-28 23:31:34 (GMT)
committerGuido van Rossum <guido@python.org>1996-05-28 23:31:34 (GMT)
commitf4aaf862fd3f29cc6a3409186dc2f7421ca0ba89 (patch)
tree4f900a93645ec042ef7f03f660b7463a5353d367 /Lib
parent7a623d7e7c678181291b7842fdc6ff0887e0a0dd (diff)
downloadcpython-f4aaf862fd3f29cc6a3409186dc2f7421ca0ba89.zip
cpython-f4aaf862fd3f29cc6a3409186dc2f7421ca0ba89.tar.gz
cpython-f4aaf862fd3f29cc6a3409186dc2f7421ca0ba89.tar.bz2
Be more careful about default temp dir
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tempfile.py30
1 files changed, 20 insertions, 10 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index db750d4..f95920d 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -17,16 +17,26 @@ template = None
# Function to calculate the directory to use
def gettempdir():
- global tempdir
- if tempdir == None:
- try:
- tempdir = os.environ['TMPDIR']
- except (KeyError, AttributeError):
- if os.name == 'posix':
- tempdir = '/usr/tmp' # XXX Why not /tmp?
- else:
- tempdir = os.getcwd() # XXX Is this OK?
- return tempdir
+ global tempdir
+ attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir]
+ if os.environ.has_key('TMPDIR'):
+ attempdirs.insert(0, os.environ['TMPDIR'])
+ testfile = gettempprefix() + '-*-writetest-*-'
+ 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
+ if tempdir is None:
+ 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