##// END OF EJS Templates
explicitly close files...
Dan Villiom Podlaski Christiansen -
r13400:14f3795a default
parent child Browse files
Show More
@@ -248,7 +248,9 b' def checkfile(f, logfunc=_defaultlogger.'
248 fc = 0
248 fc = 0
249 if not re.match(match, f):
249 if not re.match(match, f):
250 continue
250 continue
251 pre = post = open(f).read()
251 fp = open(f)
252 pre = post = fp.read()
253 fp.close()
252 if "no-" + "check-code" in pre:
254 if "no-" + "check-code" in pre:
253 break
255 break
254 for p, r in filters:
256 for p, r in filters:
@@ -244,7 +244,9 b' def sign(ui, repo, *revs, **opts):'
244 "(please commit .hgsigs manually "
244 "(please commit .hgsigs manually "
245 "or use --force)"))
245 "or use --force)"))
246
246
247 repo.wfile(".hgsigs", "ab").write(sigmessage)
247 sigsfile = repo.wfile(".hgsigs", "ab")
248 sigsfile.write(sigmessage)
249 sigsfile.close()
248
250
249 if '.hgsigs' not in repo.dirstate:
251 if '.hgsigs' not in repo.dirstate:
250 repo[None].add([".hgsigs"])
252 repo[None].add([".hgsigs"])
@@ -26,7 +26,10 b" procfs_path = '/proc/sys/fs/inotify'"
26 def _read_procfs_value(name):
26 def _read_procfs_value(name):
27 def read_value():
27 def read_value():
28 try:
28 try:
29 return int(open(procfs_path + '/' + name).read())
29 fp = open(procfs_path + '/' + name)
30 r = int(fp.read())
31 fp.close()
32 return r
30 except OSError:
33 except OSError:
31 return None
34 return None
32
35
@@ -251,6 +251,7 b' class queue(object):'
251 try:
251 try:
252 fh = open(os.path.join(path, 'patches.queue'))
252 fh = open(os.path.join(path, 'patches.queue'))
253 cur = fh.read().rstrip()
253 cur = fh.read().rstrip()
254 fh.close()
254 if not cur:
255 if not cur:
255 curpath = os.path.join(path, 'patches')
256 curpath = os.path.join(path, 'patches')
256 else:
257 else:
@@ -1781,7 +1782,9 b' class queue(object):'
1781 _('need --name to import a patch from -'))
1782 _('need --name to import a patch from -'))
1782 text = sys.stdin.read()
1783 text = sys.stdin.read()
1783 else:
1784 else:
1784 text = url.open(self.ui, filename).read()
1785 fp = url.open(self.ui, filename)
1786 text = fp.read()
1787 fp.close()
1785 except (OSError, IOError):
1788 except (OSError, IOError):
1786 raise util.Abort(_("unable to read file %s") % filename)
1789 raise util.Abort(_("unable to read file %s") % filename)
1787 if not patchname:
1790 if not patchname:
@@ -1790,6 +1793,7 b' class queue(object):'
1790 checkfile(patchname)
1793 checkfile(patchname)
1791 patchf = self.opener(patchname, "w")
1794 patchf = self.opener(patchname, "w")
1792 patchf.write(text)
1795 patchf.write(text)
1796 patchf.close()
1793 if not force:
1797 if not force:
1794 checkseries(patchname)
1798 checkseries(patchname)
1795 if patchname not in self.series:
1799 if patchname not in self.series:
@@ -2787,6 +2791,7 b' def qqueue(ui, repo, name=None, **opts):'
2787 try:
2791 try:
2788 fh = repo.opener(_allqueues, 'r')
2792 fh = repo.opener(_allqueues, 'r')
2789 queues = [queue.strip() for queue in fh if queue.strip()]
2793 queues = [queue.strip() for queue in fh if queue.strip()]
2794 fh.close()
2790 if current not in queues:
2795 if current not in queues:
2791 queues.append(current)
2796 queues.append(current)
2792 except IOError:
2797 except IOError:
@@ -261,7 +261,10 b' def patchbomb(ui, repo, *revs, **opts):'
261 tmpfn = os.path.join(tmpdir, 'bundle')
261 tmpfn = os.path.join(tmpdir, 'bundle')
262 try:
262 try:
263 commands.bundle(ui, repo, tmpfn, dest, **opts)
263 commands.bundle(ui, repo, tmpfn, dest, **opts)
264 return open(tmpfn, 'rb').read()
264 fp = open(tmpfn, 'rb')
265 data = fp.read()
266 fp.close()
267 return data
265 finally:
268 finally:
266 try:
269 try:
267 os.unlink(tmpfn)
270 os.unlink(tmpfn)
@@ -84,6 +84,7 b' class tarit(object):'
84
84
85 def __init__(self, dest, mtime, kind=''):
85 def __init__(self, dest, mtime, kind=''):
86 self.mtime = mtime
86 self.mtime = mtime
87 self.fileobj = None
87
88
88 def taropen(name, mode, fileobj=None):
89 def taropen(name, mode, fileobj=None):
89 if kind == 'gz':
90 if kind == 'gz':
@@ -93,8 +94,10 b' class tarit(object):'
93 gzfileobj = self.GzipFileWithTime(name, mode + 'b',
94 gzfileobj = self.GzipFileWithTime(name, mode + 'b',
94 zlib.Z_BEST_COMPRESSION,
95 zlib.Z_BEST_COMPRESSION,
95 fileobj, timestamp=mtime)
96 fileobj, timestamp=mtime)
97 self.fileobj = gzfileobj
96 return tarfile.TarFile.taropen(name, mode, gzfileobj)
98 return tarfile.TarFile.taropen(name, mode, gzfileobj)
97 else:
99 else:
100 self.fileobj = fileobj
98 return tarfile.open(name, mode + kind, fileobj)
101 return tarfile.open(name, mode + kind, fileobj)
99
102
100 if isinstance(dest, str):
103 if isinstance(dest, str):
@@ -120,6 +123,8 b' class tarit(object):'
120
123
121 def done(self):
124 def done(self):
122 self.z.close()
125 self.z.close()
126 if self.fileobj:
127 self.fileobj.close()
123
128
124 class tellable(object):
129 class tellable(object):
125 '''provide tell method for zipfile.ZipFile when writing to http
130 '''provide tell method for zipfile.ZipFile when writing to http
@@ -679,7 +679,9 b" def export(repo, revs, template='hg-%h.p"
679 parents.reverse()
679 parents.reverse()
680 prev = (parents and parents[0]) or nullid
680 prev = (parents and parents[0]) or nullid
681
681
682 shouldclose = False
682 if not fp:
683 if not fp:
684 shouldclose = True
683 fp = make_file(repo, template, node, total=total, seqno=seqno,
685 fp = make_file(repo, template, node, total=total, seqno=seqno,
684 revwidth=revwidth, mode='ab')
686 revwidth=revwidth, mode='ab')
685 if fp != sys.stdout and hasattr(fp, 'name'):
687 if fp != sys.stdout and hasattr(fp, 'name'):
@@ -700,7 +702,8 b" def export(repo, revs, template='hg-%h.p"
700 for chunk in patch.diff(repo, prev, node, opts=opts):
702 for chunk in patch.diff(repo, prev, node, opts=opts):
701 fp.write(chunk)
703 fp.write(chunk)
702
704
703 fp.flush()
705 if shouldclose:
706 fp.close()
704
707
705 for seqno, rev in enumerate(revs):
708 for seqno, rev in enumerate(revs):
706 single(rev, seqno + 1, fp)
709 single(rev, seqno + 1, fp)
@@ -750,6 +750,7 b' def cat(ui, repo, file1, *pats, **opts):'
750 if opts.get('decode'):
750 if opts.get('decode'):
751 data = repo.wwritedata(abs, data)
751 data = repo.wwritedata(abs, data)
752 fp.write(data)
752 fp.write(data)
753 fp.close()
753 err = 0
754 err = 0
754 return err
755 return err
755
756
@@ -80,7 +80,9 b' class dirstate(object):'
80 @propertycache
80 @propertycache
81 def _pl(self):
81 def _pl(self):
82 try:
82 try:
83 st = self._opener("dirstate").read(40)
83 fp = self._opener("dirstate")
84 st = fp.read(40)
85 fp.close()
84 l = len(st)
86 l = len(st)
85 if l == 40:
87 if l == 40:
86 return st[:20], st[20:40]
88 return st[:20], st[20:40]
@@ -119,7 +119,10 b' def staticfile(directory, fname, req):'
119 os.stat(path)
119 os.stat(path)
120 ct = mimetypes.guess_type(path)[0] or "text/plain"
120 ct = mimetypes.guess_type(path)[0] or "text/plain"
121 req.respond(HTTP_OK, ct, length = os.path.getsize(path))
121 req.respond(HTTP_OK, ct, length = os.path.getsize(path))
122 return open(path, 'rb').read()
122 fp = open(path, 'rb')
123 data = fp.read()
124 fp.close()
125 return data
123 except TypeError:
126 except TypeError:
124 raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename')
127 raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename')
125 except OSError, err:
128 except OSError, err:
@@ -283,6 +283,8 b' class localrepository(repo.repository):'
283 # committed tags are stored in UTF-8
283 # committed tags are stored in UTF-8
284 writetags(fp, names, encoding.fromlocal, prevtags)
284 writetags(fp, names, encoding.fromlocal, prevtags)
285
285
286 fp.close()
287
286 if '.hgtags' not in self.dirstate:
288 if '.hgtags' not in self.dirstate:
287 self[None].add(['.hgtags'])
289 self[None].add(['.hgtags'])
288
290
@@ -32,6 +32,7 b' class mergestate(object):'
32 else:
32 else:
33 bits = l[:-1].split("\0")
33 bits = l[:-1].split("\0")
34 self._state[bits[0]] = bits[1:]
34 self._state[bits[0]] = bits[1:]
35 f.close()
35 except IOError, err:
36 except IOError, err:
36 if err.errno != errno.ENOENT:
37 if err.errno != errno.ENOENT:
37 raise
38 raise
@@ -42,6 +43,7 b' class mergestate(object):'
42 f.write(hex(self._local) + "\n")
43 f.write(hex(self._local) + "\n")
43 for d, v in self._state.iteritems():
44 for d, v in self._state.iteritems():
44 f.write("\0".join([d] + v) + "\n")
45 f.write("\0".join([d] + v) + "\n")
46 f.close()
45 self._dirty = False
47 self._dirty = False
46 def add(self, fcl, fco, fca, fd, flags):
48 def add(self, fcl, fco, fca, fd, flags):
47 hash = util.sha1(fcl.path()).hexdigest()
49 hash = util.sha1(fcl.path()).hexdigest()
@@ -67,6 +69,7 b' class mergestate(object):'
67 state, hash, lfile, afile, anode, ofile, flags = self._state[dfile]
69 state, hash, lfile, afile, anode, ofile, flags = self._state[dfile]
68 f = self._repo.opener("merge/" + hash)
70 f = self._repo.opener("merge/" + hash)
69 self._repo.wwrite(dfile, f.read(), flags)
71 self._repo.wwrite(dfile, f.read(), flags)
72 f.close()
70 fcd = wctx[dfile]
73 fcd = wctx[dfile]
71 fco = octx[ofile]
74 fco = octx[ofile]
72 fca = self._repo.filectx(afile, fileid=anode)
75 fca = self._repo.filectx(afile, fileid=anode)
@@ -77,20 +77,26 b' def set_flags(f, l, x):'
77 if l:
77 if l:
78 if not stat.S_ISLNK(s):
78 if not stat.S_ISLNK(s):
79 # switch file to link
79 # switch file to link
80 data = open(f).read()
80 fp = open(f)
81 data = fp.read()
82 fp.close()
81 os.unlink(f)
83 os.unlink(f)
82 try:
84 try:
83 os.symlink(data, f)
85 os.symlink(data, f)
84 except:
86 except:
85 # failed to make a link, rewrite file
87 # failed to make a link, rewrite file
86 open(f, "w").write(data)
88 fp = open(f, "w")
89 fp.write(data)
90 fp.close()
87 # no chmod needed at this point
91 # no chmod needed at this point
88 return
92 return
89 if stat.S_ISLNK(s):
93 if stat.S_ISLNK(s):
90 # switch link to file
94 # switch link to file
91 data = os.readlink(f)
95 data = os.readlink(f)
92 os.unlink(f)
96 os.unlink(f)
93 open(f, "w").write(data)
97 fp = open(f, "w")
98 fp.write(data)
99 fp.close()
94 s = 0666 & ~umask # avoid restatting for chmod
100 s = 0666 & ~umask # avoid restatting for chmod
95
101
96 sx = s & 0100
102 sx = s & 0100
@@ -243,6 +243,7 b' class revlog(object):'
243 try:
243 try:
244 f = self.opener(self.indexfile)
244 f = self.opener(self.indexfile)
245 i = f.read()
245 i = f.read()
246 f.close()
246 if len(i) > 0:
247 if len(i) > 0:
247 v = struct.unpack(versionformat, i[:4])[0]
248 v = struct.unpack(versionformat, i[:4])[0]
248 except IOError, inst:
249 except IOError, inst:
@@ -1167,6 +1168,7 b' class revlog(object):'
1167 if not dfh and not self._inline:
1168 if not dfh and not self._inline:
1168 # addrevision switched from inline to conventional
1169 # addrevision switched from inline to conventional
1169 # reopen the index
1170 # reopen the index
1171 ifh.close()
1170 dfh = self.opener(self.datafile, "a")
1172 dfh = self.opener(self.datafile, "a")
1171 ifh = self.opener(self.indexfile, "a")
1173 ifh = self.opener(self.indexfile, "a")
1172 finally:
1174 finally:
@@ -1226,6 +1228,7 b' class revlog(object):'
1226 f = self.opener(self.datafile)
1228 f = self.opener(self.datafile)
1227 f.seek(0, 2)
1229 f.seek(0, 2)
1228 actual = f.tell()
1230 actual = f.tell()
1231 f.close()
1229 dd = actual - expected
1232 dd = actual - expected
1230 except IOError, inst:
1233 except IOError, inst:
1231 if inst.errno != errno.ENOENT:
1234 if inst.errno != errno.ENOENT:
@@ -1236,6 +1239,7 b' class revlog(object):'
1236 f = self.opener(self.indexfile)
1239 f = self.opener(self.indexfile)
1237 f.seek(0, 2)
1240 f.seek(0, 2)
1238 actual = f.tell()
1241 actual = f.tell()
1242 f.close()
1239 s = self._io.size
1243 s = self._io.size
1240 i = max(0, actual // s)
1244 i = max(0, actual // s)
1241 di = actual - (i * s)
1245 di = actual - (i * s)
@@ -98,7 +98,9 b' class statichttprepository(localrepo.loc'
98 raise
98 raise
99 # check if it is a non-empty old-style repository
99 # check if it is a non-empty old-style repository
100 try:
100 try:
101 self.opener("00changelog.i").read(1)
101 fp = self.opener("00changelog.i")
102 fp.read(1)
103 fp.close()
102 except IOError, inst:
104 except IOError, inst:
103 if inst.errno != errno.ENOENT:
105 if inst.errno != errno.ENOENT:
104 raise
106 raise
@@ -27,13 +27,17 b' def _playback(journal, report, opener, e'
27 for f, o, ignore in entries:
27 for f, o, ignore in entries:
28 if o or not unlink:
28 if o or not unlink:
29 try:
29 try:
30 opener(f, 'a').truncate(o)
30 fp = opener(f, 'a')
31 fp.truncate(o)
32 fp.close()
31 except IOError:
33 except IOError:
32 report(_("failed to truncate %s\n") % f)
34 report(_("failed to truncate %s\n") % f)
33 raise
35 raise
34 else:
36 else:
35 try:
37 try:
36 fn = opener(f).name
38 fp = opener(f)
39 fn = fp.name
40 fp.close()
37 os.unlink(fn)
41 os.unlink(fn)
38 except (IOError, OSError), inst:
42 except (IOError, OSError), inst:
39 if inst.errno != errno.ENOENT:
43 if inst.errno != errno.ENOENT:
@@ -169,7 +173,10 b' class transaction(object):'
169 def rollback(opener, file, report):
173 def rollback(opener, file, report):
170 entries = []
174 entries = []
171
175
172 for l in open(file).readlines():
176 fp = open(file)
177 lines = fp.readlines()
178 fp.close()
179 for l in lines:
173 f, o = l.split('\0')
180 f, o = l.split('\0')
174 entries.append((f, int(o), None))
181 entries.append((f, int(o), None))
175
182
@@ -198,7 +198,10 b' def tempfilter(s, cmd):'
198 if code:
198 if code:
199 raise Abort(_("command '%s' failed: %s") %
199 raise Abort(_("command '%s' failed: %s") %
200 (cmd, explain_exit(code)))
200 (cmd, explain_exit(code)))
201 return open(outname, 'rb').read()
201 fp = open(outname, 'rb')
202 r = fp.read()
203 fp.close()
204 return r
202 finally:
205 finally:
203 try:
206 try:
204 if inname:
207 if inname:
@@ -591,7 +594,10 b' def readlock(pathname):'
591 raise
594 raise
592 except AttributeError: # no symlink in os
595 except AttributeError: # no symlink in os
593 pass
596 pass
594 return posixfile(pathname).read()
597 fp = posixfile(pathname)
598 r = fp.read()
599 fp.close()
600 return r
595
601
596 def fstat(fp):
602 def fstat(fp):
597 '''stat file object that may not have fileno method.'''
603 '''stat file object that may not have fileno method.'''
@@ -294,14 +294,18 b' class hginstallscripts(install_scripts):'
294 libdir = uplevel * ('..' + os.sep) + self.install_lib[len(common):]
294 libdir = uplevel * ('..' + os.sep) + self.install_lib[len(common):]
295
295
296 for outfile in self.outfiles:
296 for outfile in self.outfiles:
297 data = open(outfile, 'rb').read()
297 fp = open(outfile, 'rb')
298 data = fp.read()
299 fp.close()
298
300
299 # skip binary files
301 # skip binary files
300 if '\0' in data:
302 if '\0' in data:
301 continue
303 continue
302
304
303 data = data.replace('@LIBDIR@', libdir.encode('string_escape'))
305 data = data.replace('@LIBDIR@', libdir.encode('string_escape'))
304 open(outfile, 'wb').write(data)
306 fp = open(outfile, 'wb')
307 fp.write(data)
308 fp.close()
305
309
306 cmdclass = {'build_mo': hgbuildmo,
310 cmdclass = {'build_mo': hgbuildmo,
307 'build_ext': hgbuildext,
311 'build_ext': hgbuildext,
@@ -231,6 +231,8 b' def parseargs():'
231 if line and not line.startswith('#'):
231 if line and not line.startswith('#'):
232 blacklist[line] = filename
232 blacklist[line] = filename
233
233
234 f.close()
235
234 options.blacklist = blacklist
236 options.blacklist = blacklist
235
237
236 return (options, args)
238 return (options, args)
@@ -491,6 +493,8 b' def tsttest(test, options, replacements)'
491 # non-command/result - queue up for merged output
493 # non-command/result - queue up for merged output
492 after.setdefault(pos, []).append(l)
494 after.setdefault(pos, []).append(l)
493
495
496 t.close()
497
494 script.append('echo %s %s $?\n' % (salt, n + 1))
498 script.append('echo %s %s $?\n' % (salt, n + 1))
495
499
496 fd, name = tempfile.mkstemp(suffix='hg-tst')
500 fd, name = tempfile.mkstemp(suffix='hg-tst')
@@ -927,7 +931,9 b' def runtests(options, tests):'
927 continue
931 continue
928
932
929 if options.keywords:
933 if options.keywords:
930 t = open(test).read().lower() + test.lower()
934 fp = open(test)
935 t = fp.read().lower() + test.lower()
936 fp.close()
931 for k in options.keywords.lower().split():
937 for k in options.keywords.lower().split():
932 if k in t:
938 if k in t:
933 break
939 break
General Comments 0
You need to be logged in to leave comments. Login now