annotate Lib/tempfile.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 10c084942cf1
children 332fe02bf02d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Temporary file name allocation
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
2 #
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
3 # XXX This tries to be not UNIX specific, but I don't know beans about
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
4 # how to choose a temp directory or filename on MS-DOS or other
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
5 # systems so it may have to be changed...
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
6
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
8 import os
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
9
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
10
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
11 # Parameters that the caller may set to override the defaults
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
12
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
13 tempdir = None
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
14 template = None
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
17 # Function to calculate the directory to use
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
19 def gettempdir():
3647
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
20 global tempdir
3954
9d8a965de7ec Restore 1.3 behavior of gettempdir(): if tempdir is already set, believe it.
Guido van Rossum <guido@python.org>
parents: 3647
diff changeset
21 if tempdir is not None:
9d8a965de7ec Restore 1.3 behavior of gettempdir(): if tempdir is already set, believe it.
Guido van Rossum <guido@python.org>
parents: 3647
diff changeset
22 return tempdir
3647
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
23 attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir]
4077
10c084942cf1 Attempt at Windows-NT compatibility.
Guido van Rossum <guido@python.org>
parents: 3954
diff changeset
24 if os.name == 'nt':
10c084942cf1 Attempt at Windows-NT compatibility.
Guido van Rossum <guido@python.org>
parents: 3954
diff changeset
25 attempdirs.insert(0, 'C:\\TEMP')
10c084942cf1 Attempt at Windows-NT compatibility.
Guido van Rossum <guido@python.org>
parents: 3954
diff changeset
26 attempdirs.insert(0, '\\TEMP')
3647
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
27 if os.environ.has_key('TMPDIR'):
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
28 attempdirs.insert(0, os.environ['TMPDIR'])
4077
10c084942cf1 Attempt at Windows-NT compatibility.
Guido van Rossum <guido@python.org>
parents: 3954
diff changeset
29 testfile = gettempprefix() + 'test'
3647
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
30 for dir in attempdirs:
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
31 try:
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
32 filename = os.path.join(dir, testfile)
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
33 fp = open(filename, 'w')
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
34 fp.write('blat')
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
35 fp.close()
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
36 os.unlink(filename)
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
37 tempdir = dir
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
38 break
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
39 except IOError:
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
40 pass
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
41 if tempdir is None:
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
42 msg = "Can't find a usable temporary directory amongst " + `attempdirs`
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
43 raise IOError, msg
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
44 return tempdir
586
dbf408918201 Use $TMPDIR if it is set in the environment.
Guido van Rossum <guido@python.org>
parents: 534
diff changeset
45
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
46
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
47 # Function to calculate a prefix of the filename to use
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
48
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
49 def gettempprefix():
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
50 global template
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
51 if template == None:
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
52 if os.name == 'posix':
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
53 template = '@' + `os.getpid()` + '.'
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
54 else:
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
55 template = 'tmp' # XXX might choose a better one
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
56 return template
586
dbf408918201 Use $TMPDIR if it is set in the environment.
Guido van Rossum <guido@python.org>
parents: 534
diff changeset
57
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
534
96b2128f00ae Use 'global' instead of struct kludge.
Guido van Rossum <guido@python.org>
parents: 531
diff changeset
59 # Counter for generating unique names
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
534
96b2128f00ae Use 'global' instead of struct kludge.
Guido van Rossum <guido@python.org>
parents: 531
diff changeset
61 counter = 0
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
64 # User-callable function to return a unique temporary file name
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def mktemp():
534
96b2128f00ae Use 'global' instead of struct kludge.
Guido van Rossum <guido@python.org>
parents: 531
diff changeset
67 global counter
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
68 dir = gettempdir()
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
69 pre = gettempprefix()
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 while 1:
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
71 counter = counter + 1
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
72 file = os.path.join(dir, pre + `counter`)
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
73 if not os.path.exists(file):
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
74 return file