Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b0a329

Browse files
committed
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Weinberg). This changes all uses of deprecated tempfile functions to the recommended ones.
1 parent 830a515 commit 3b0a329

31 files changed

Lines changed: 134 additions & 149 deletions

Demo/pdist/rcslib.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,17 @@ def checkin(self, name_rev, message=None, otherflags=""):
143143
if message and message[-1] != '\n':
144144
message = message + '\n'
145145
lockflag = "-u"
146-
textfile = None
147-
try:
148-
if new:
149-
textfile = tempfile.mktemp()
150-
f = open(textfile, 'w')
151-
f.write(message)
152-
f.close()
153-
cmd = 'ci %s%s -t%s %s %s' % \
154-
(lockflag, rev, textfile, otherflags, name)
155-
else:
156-
message = regsub.gsub('\([\\"$`]\)', '\\\\\\1', message)
157-
cmd = 'ci %s%s -m"%s" %s %s' % \
158-
(lockflag, rev, message, otherflags, name)
159-
return self._system(cmd)
160-
finally:
161-
if textfile: self._remove(textfile)
146+
if new:
147+
f = tempfile.NamedTemporaryFile()
148+
f.write(message)
149+
f.flush()
150+
cmd = 'ci %s%s -t%s %s %s' % \
151+
(lockflag, rev, f.name, otherflags, name)
152+
else:
153+
message = regsub.gsub('\([\\"$`]\)', '\\\\\\1', message)
154+
cmd = 'ci %s%s -m"%s" %s %s' % \
155+
(lockflag, rev, message, otherflags, name)
156+
return self._system(cmd)
162157

163158
# --- Exported support methods ---
164159

Demo/pdist/rcvs.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,13 @@ def diff(self, opts = []):
172172
if self.lsum == sum:
173173
return
174174
import tempfile
175-
tfn = tempfile.mktemp()
176-
try:
177-
tf = open(tfn, 'w')
178-
tf.write(data)
179-
tf.close()
180-
print 'diff %s -r%s %s' % (flags, rev, fn)
181-
sts = os.system('diff %s %s %s' % (flags, tfn, fn))
182-
if sts:
183-
print '='*70
184-
finally:
185-
remove(tfn)
175+
tf = tempfile.NamedTemporaryFile()
176+
tf.write(data)
177+
tf.flush()
178+
print 'diff %s -r%s %s' % (flags, rev, fn)
179+
sts = os.system('diff %s %s %s' % (flags, tf.name, fn))
180+
if sts:
181+
print '='*70
186182

187183
def commitcheck(self):
188184
return self.action() != 'C'

Demo/pdist/rrcs.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,13 @@ def diff(x, copts, fn):
102102
flags = flags + ' ' + o + a
103103
flags = flags[1:]
104104
data = x.get(fn)
105-
tfn = tempfile.mktemp()
106-
try:
107-
tf = open(tfn, 'w')
108-
tf.write(data)
109-
tf.close()
110-
print 'diff %s -r%s %s' % (flags, x.head(fn), fn)
111-
sts = os.system('diff %s %s %s' % (flags, tfn, fn))
112-
if sts:
113-
print '='*70
114-
finally:
115-
remove(tfn)
105+
tf = tempfile.NamedTemporaryFile()
106+
tf.write(data)
107+
tf.flush()
108+
print 'diff %s -r%s %s' % (flags, x.head(fn), fn)
109+
sts = os.system('diff %s %s %s' % (flags, tf.name, fn))
110+
if sts:
111+
print '='*70
116112

117113
def same(x, copts, fn, data = None):
118114
if data is None:

Demo/scripts/pp.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,11 @@
120120
program = program + (string.joinfields(epilogue, '\n') + '\n')
121121

122122
import tempfile
123-
tfn = tempfile.mktemp()
124-
try:
125-
fp = open(tfn, 'w')
126-
fp.write(program)
127-
fp.close()
128-
if DFLAG:
129-
import pdb
130-
pdb.run('execfile(' + `tfn` + ')')
131-
else:
132-
execfile(tfn)
133-
finally:
134-
import os
135-
try:
136-
os.unlink(tfn)
137-
except:
138-
pass
123+
fp = tempfile.NamedTemporaryFile()
124+
fp.write(program)
125+
fp.flush()
126+
if DFLAG:
127+
import pdb
128+
pdb.run('execfile(' + `tfn` + ')')
129+
else:
130+
execfile(tfn)

Lib/cgitb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ def handle(self, info=None):
193193

194194
if self.logdir is not None:
195195
import os, tempfile
196-
name = tempfile.mktemp(['.html', '.txt'][text])
197-
path = os.path.join(self.logdir, os.path.basename(name))
196+
(fd, name) = tempfile.mkstemp(suffix=['.html', '.txt'][text],
197+
dir=self.logdir)
198198
try:
199-
file = open(path, 'w')
199+
file = os.fdopen(fd, 'w')
200200
file.write(doc)
201201
file.close()
202202
msg = '<p> %s contains the description of this error.' % path

Lib/distutils/command/bdist_wininst.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,17 @@ def run (self):
130130

131131
# And make an archive relative to the root of the
132132
# pseudo-installation tree.
133-
from tempfile import mktemp
134-
archive_basename = mktemp()
133+
from tempfile import NamedTemporaryFile
134+
arc = NamedTemporaryFile(".zip")
135+
archive_basename = arc.name[:-4]
135136
fullname = self.distribution.get_fullname()
136137
arcname = self.make_archive(archive_basename, "zip",
137138
root_dir=self.bdist_dir)
138139
# create an exe containing the zip-file
139140
self.create_exe(arcname, fullname, self.bitmap)
140141
# remove the zip-file again
141142
log.debug("removing temporary file '%s'", arcname)
142-
os.remove(arcname)
143+
arc.close()
143144

144145
if not self.keep_temp:
145146
remove_tree(self.bdist_dir, dry_run=self.dry_run)

Lib/distutils/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,11 @@ def byte_compile (py_files,
359359
# "Indirect" byte-compilation: write a temporary script and then
360360
# run it with the appropriate flags.
361361
if not direct:
362-
from tempfile import mktemp
363-
script_name = mktemp(".py")
362+
from tempfile import mkstemp
363+
(script_fd, script_name) = mkstemp(".py")
364364
log.info("writing byte-compilation script '%s'", script_name)
365365
if not dry_run:
366-
script = open(script_name, "w")
366+
script = os.fdopen(script_fd, "w")
367367

368368
script.write("""\
369369
from distutils.util import byte_compile

Lib/hotshot/stones.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
if sys.argv[1:]:
1010
logfile = sys.argv[1]
11-
cleanup = 0
1211
else:
1312
import tempfile
14-
logfile = tempfile.mktemp()
15-
cleanup = 1
16-
13+
logf = tempfile.NamedTemporaryFile()
14+
logfile = logf.name
1715

1816
p = hotshot.Profile(logfile)
1917
benchtime, stones = p.runcall(test.pystone.pystones)
@@ -24,8 +22,6 @@
2422
print "This machine benchmarks at %g pystones/second" % stones
2523

2624
stats = hotshot.stats.load(logfile)
27-
if cleanup:
28-
os.unlink(logfile)
2925
stats.strip_dirs()
3026
stats.sort_stats('time', 'calls')
3127
try:

Lib/mimetools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ def pipeto(input, command):
202202
pipe.close()
203203

204204
def pipethrough(input, command, output):
205-
tempname = tempfile.mktemp()
206-
temp = open(tempname, 'w')
205+
(fd, tempname) = tempfile.mkstemp()
206+
temp = os.fdopen(fd, 'w')
207207
copyliteral(input, temp)
208208
temp.close()
209209
pipe = os.popen(command + ' <' + tempname, 'r')

Lib/pipes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ def makepipeline(infile, steps, outfile):
225225
lkind = list[i-1][2]
226226
rkind = list[i][2]
227227
if lkind[1] == 'f' or rkind[0] == 'f':
228-
temp = tempfile.mktemp()
228+
(fd, temp) = tempfile.mkstemp()
229+
os.close(fd)
229230
garbage.append(temp)
230231
list[i-1][-1] = list[i][0] = temp
231232
#

0 commit comments

Comments
 (0)