summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-03-31 19:02:01 (GMT)
committerGuido van Rossum <guido@python.org>1992-03-31 19:02:01 (GMT)
commit41f9503c1147416761053c11abc8c4f85bef1b3b (patch)
tree6942cb34cb06d9e4a63aa41a1163dca18d1ab353 /Lib/tempfile.py
parent1a76ef260df9378d796ca85b08fcecdd134ff6b6 (diff)
downloadcpython-41f9503c1147416761053c11abc8c4f85bef1b3b.zip
cpython-41f9503c1147416761053c11abc8c4f85bef1b3b.tar.gz
cpython-41f9503c1147416761053c11abc8c4f85bef1b3b.tar.bz2
Mostly rewritten to be more flexible and more portable
./
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py59
1 files changed, 41 insertions, 18 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 793c23f..db750d4 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -1,19 +1,44 @@
# Temporary file name allocation
+#
+# XXX This tries to be not UNIX specific, but I don't know beans about
+# how to choose a temp directory or filename on MS-DOS or other
+# systems so it may have to be changed...
-import posix
-import path
+import os
-# Changeable parameters (by clients!)...
-tempdir = '/usr/tmp'
-template = '@'
+# Parameters that the caller may set to override the defaults
-# Use environment variable $TMPDIR to override default tempdir.
+tempdir = None
+template = None
-if posix.environ.has_key('TMPDIR'):
- # XXX Could check that it's a writable directory...
- tempdir = posix.environ['TMPDIR']
+
+# 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
+
+
+# Function to calculate a prefix of the filename to use
+
+def gettempprefix():
+ global template
+ if template == None:
+ if os.name == 'posix':
+ template = '@' + `os.getpid()` + '.'
+ else:
+ template = 'tmp' # XXX might choose a better one
+ return template
# Counter for generating unique names
@@ -21,16 +46,14 @@ if posix.environ.has_key('TMPDIR'):
counter = 0
-# User-callable function
-# XXX Should this have a parameter, like C's mktemp()?
-# XXX Should we instead use the model of Standard C's tempnam()?
-# XXX By all means, avoid a mess with four different functions like C...
+# User-callable function to return a unique temporary file name
def mktemp():
global counter
+ dir = gettempdir()
+ pre = gettempprefix()
while 1:
- counter = counter+1
- file = tempdir+'/'+template+`posix.getpid()`+'.'+`counter`
- if not path.exists(file):
- break
- return file
+ counter = counter + 1
+ file = os.path.join(dir, pre + `counter`)
+ if not os.path.exists(file):
+ return file