Be more careful about default temp dir

This commit is contained in:
Guido van Rossum 1996-05-28 23:31:34 +00:00
parent 7a623d7e7c
commit f4aaf862fd
1 changed files with 20 additions and 10 deletions

View File

@ -17,16 +17,26 @@
# Function to calculate the directory to use # Function to calculate the directory to use
def gettempdir(): def gettempdir():
global tempdir global tempdir
if tempdir == None: attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir]
try: if os.environ.has_key('TMPDIR'):
tempdir = os.environ['TMPDIR'] attempdirs.insert(0, os.environ['TMPDIR'])
except (KeyError, AttributeError): testfile = gettempprefix() + '-*-writetest-*-'
if os.name == 'posix': for dir in attempdirs:
tempdir = '/usr/tmp' # XXX Why not /tmp? try:
else: filename = os.path.join(dir, testfile)
tempdir = os.getcwd() # XXX Is this OK? fp = open(filename, 'w')
return tempdir 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 # Function to calculate a prefix of the filename to use