summaryrefslogtreecommitdiffstats
path: root/Tools/scripts
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-03-30 06:33:02 (GMT)
committerGitHub <noreply@github.com>2019-03-30 06:33:02 (GMT)
commit172bb39452ae8b3ccdf5d1f23ead46f44200cd49 (patch)
tree5e1effbca3664b839a81eb7a7d62fa4974cfbfb1 /Tools/scripts
parentafbb7a371fb44edc731344eab5b474ad8f7b57d7 (diff)
downloadcpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.zip
cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.gz
cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.bz2
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
Diffstat (limited to 'Tools/scripts')
-rwxr-xr-xTools/scripts/cleanfuture.py15
-rwxr-xr-xTools/scripts/combinerefs.py9
-rwxr-xr-xTools/scripts/dutree.py22
-rwxr-xr-xTools/scripts/eptags.py37
-rwxr-xr-xTools/scripts/finddiv.py22
-rwxr-xr-xTools/scripts/fixnotice.py20
-rwxr-xr-xTools/scripts/fixps.py20
-rwxr-xr-xTools/scripts/get-remote-certificate.py15
-rwxr-xr-xTools/scripts/h2py.py39
-rwxr-xr-xTools/scripts/ifdef.py5
-rwxr-xr-xTools/scripts/md5sum.py8
-rwxr-xr-xTools/scripts/mkreal.py15
-rwxr-xr-xTools/scripts/nm2def.py5
-rwxr-xr-xTools/scripts/objgraph.py3
-rwxr-xr-xTools/scripts/parseentities.py16
-rwxr-xr-xTools/scripts/pathfix.py44
-rwxr-xr-xTools/scripts/pdeps.py45
-rwxr-xr-xTools/scripts/ptags.py37
-rwxr-xr-xTools/scripts/rgrep.py47
19 files changed, 208 insertions, 216 deletions
diff --git a/Tools/scripts/cleanfuture.py b/Tools/scripts/cleanfuture.py
index b48ab60..94f6912 100755
--- a/Tools/scripts/cleanfuture.py
+++ b/Tools/scripts/cleanfuture.py
@@ -96,11 +96,11 @@ def check(file):
errprint("%r: I/O Error: %s" % (file, str(msg)))
return
- ff = FutureFinder(f, file)
- changed = ff.run()
- if changed:
- ff.gettherest()
- f.close()
+ with f:
+ ff = FutureFinder(f, file)
+ changed = ff.run()
+ if changed:
+ ff.gettherest()
if changed:
if verbose:
print("changed.")
@@ -122,9 +122,8 @@ def check(file):
os.rename(file, bak)
if verbose:
print("renamed", file, "to", bak)
- g = open(file, "w")
- ff.write(g)
- g.close()
+ with open(file, "w") as g:
+ ff.write(g)
if verbose:
print("wrote new", file)
else:
diff --git a/Tools/scripts/combinerefs.py b/Tools/scripts/combinerefs.py
index 7ca9526..49ccca7 100755
--- a/Tools/scripts/combinerefs.py
+++ b/Tools/scripts/combinerefs.py
@@ -85,9 +85,7 @@ def read(fileiter, pat, whilematch):
else:
break
-def combine(fname):
- f = open(fname)
-
+def combinefile(f):
fi = iter(f)
for line in read(fi, re.compile(r'^Remaining objects:$'), False):
@@ -121,8 +119,11 @@ def combine(fname):
print('[%s->%s]' % (addr2rc[addr], rc), end=' ')
print(guts, addr2guts[addr])
- f.close()
print("%d objects before, %d after" % (before, after))
+def combine(fname):
+ with open(fname) as f:
+ combinefile(f)
+
if __name__ == '__main__':
combine(sys.argv[1])
diff --git a/Tools/scripts/dutree.py b/Tools/scripts/dutree.py
index 6b4361a..d25cf72 100755
--- a/Tools/scripts/dutree.py
+++ b/Tools/scripts/dutree.py
@@ -4,18 +4,18 @@
import os, sys, errno
def main():
- p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
total, d = None, {}
- for line in p.readlines():
- i = 0
- while line[i] in '0123456789': i = i+1
- size = eval(line[:i])
- while line[i] in ' \t': i = i+1
- filename = line[i:-1]
- comps = filename.split('/')
- if comps[0] == '': comps[0] = '/'
- if comps[len(comps)-1] == '': del comps[len(comps)-1]
- total, d = store(size, comps, total, d)
+ with os.popen('du ' + ' '.join(sys.argv[1:])) as p:
+ for line in p:
+ i = 0
+ while line[i] in '0123456789': i = i+1
+ size = eval(line[:i])
+ while line[i] in ' \t': i = i+1
+ filename = line[i:-1]
+ comps = filename.split('/')
+ if comps[0] == '': comps[0] = '/'
+ if comps[len(comps)-1] == '': del comps[len(comps)-1]
+ total, d = store(size, comps, total, d)
try:
display(total, d)
except IOError as e:
diff --git a/Tools/scripts/eptags.py b/Tools/scripts/eptags.py
index 401ac7e..7f8059b 100755
--- a/Tools/scripts/eptags.py
+++ b/Tools/scripts/eptags.py
@@ -28,29 +28,30 @@ def treat_file(filename, outfp):
except OSError:
sys.stderr.write('Cannot open %s\n'%filename)
return
- charno = 0
- lineno = 0
- tags = []
- size = 0
- while 1:
- line = fp.readline()
- if not line:
- break
- lineno = lineno + 1
- m = matcher.search(line)
- if m:
- tag = m.group(0) + '\177%d,%d\n' % (lineno, charno)
- tags.append(tag)
- size = size + len(tag)
- charno = charno + len(line)
+ with fp:
+ charno = 0
+ lineno = 0
+ tags = []
+ size = 0
+ while 1:
+ line = fp.readline()
+ if not line:
+ break
+ lineno = lineno + 1
+ m = matcher.search(line)
+ if m:
+ tag = m.group(0) + '\177%d,%d\n' % (lineno, charno)
+ tags.append(tag)
+ size = size + len(tag)
+ charno = charno + len(line)
outfp.write('\f\n%s,%d\n' % (filename,size))
for tag in tags:
outfp.write(tag)
def main():
- outfp = open('TAGS', 'w')
- for filename in sys.argv[1:]:
- treat_file(filename, outfp)
+ with open('TAGS', 'w') as outfp:
+ for filename in sys.argv[1:]:
+ treat_file(filename, outfp)
if __name__=="__main__":
main()
diff --git a/Tools/scripts/finddiv.py b/Tools/scripts/finddiv.py
index a705f56..d21253c 100755
--- a/Tools/scripts/finddiv.py
+++ b/Tools/scripts/finddiv.py
@@ -55,17 +55,17 @@ def process(filename, listnames):
except IOError as msg:
sys.stderr.write("Can't open: %s\n" % msg)
return 1
- g = tokenize.generate_tokens(fp.readline)
- lastrow = None
- for type, token, (row, col), end, line in g:
- if token in ("/", "/="):
- if listnames:
- print(filename)
- break
- if row != lastrow:
- lastrow = row
- print("%s:%d:%s" % (filename, row, line), end=' ')
- fp.close()
+ with fp:
+ g = tokenize.generate_tokens(fp.readline)
+ lastrow = None
+ for type, token, (row, col), end, line in g:
+ if token in ("/", "/="):
+ if listnames:
+ print(filename)
+ break
+ if row != lastrow:
+ lastrow = row
+ print("%s:%d:%s" % (filename, row, line), end=' ')
def processdir(dir, listnames):
try:
diff --git a/Tools/scripts/fixnotice.py b/Tools/scripts/fixnotice.py
index ad967f9..317051d 100755
--- a/Tools/scripts/fixnotice.py
+++ b/Tools/scripts/fixnotice.py
@@ -73,22 +73,19 @@ def main():
elif opt == '--dry-run':
DRYRUN = 1
elif opt == '--oldnotice':
- fp = open(arg)
- OLD_NOTICE = fp.read()
- fp.close()
+ with open(arg) as fp:
+ OLD_NOTICE = fp.read()
elif opt == '--newnotice':
- fp = open(arg)
- NEW_NOTICE = fp.read()
- fp.close()
+ with open(arg) as fp:
+ NEW_NOTICE = fp.read()
for arg in args:
process(arg)
def process(file):
- f = open(file)
- data = f.read()
- f.close()
+ with open(file) as f:
+ data = f.read()
i = data.find(OLD_NOTICE)
if i < 0:
if VERBOSE:
@@ -102,9 +99,8 @@ def process(file):
data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
new = file + ".new"
backup = file + ".bak"
- f = open(new, "w")
- f.write(data)
- f.close()
+ with open(new, "w") as f:
+ f.write(data)
os.rename(file, backup)
os.rename(new, file)
diff --git a/Tools/scripts/fixps.py b/Tools/scripts/fixps.py
index b002261..725300e 100755
--- a/Tools/scripts/fixps.py
+++ b/Tools/scripts/fixps.py
@@ -14,20 +14,18 @@ def main():
except IOError as msg:
print(filename, ': can\'t open :', msg)
continue
- line = f.readline()
- if not re.match('^#! */usr/local/bin/python', line):
- print(filename, ': not a /usr/local/bin/python script')
- f.close()
- continue
- rest = f.read()
- f.close()
+ with f:
+ line = f.readline()
+ if not re.match('^#! */usr/local/bin/python', line):
+ print(filename, ': not a /usr/local/bin/python script')
+ continue
+ rest = f.read()
line = re.sub('/usr/local/bin/python',
'/usr/bin/env python', line)
print(filename, ':', repr(line))
- f = open(filename, "w")
- f.write(line)
- f.write(rest)
- f.close()
+ with open(filename, "w") as f:
+ f.write(line)
+ f.write(rest)
if __name__ == '__main__':
main()
diff --git a/Tools/scripts/get-remote-certificate.py b/Tools/scripts/get-remote-certificate.py
index 5811f20..3890128 100755
--- a/Tools/scripts/get-remote-certificate.py
+++ b/Tools/scripts/get-remote-certificate.py
@@ -29,9 +29,8 @@ def fetch_server_certificate (host, port):
return None
else:
tn = tempfile.mktemp()
- fp = open(tn, "wb")
- fp.write(m.group(1) + b"\n")
- fp.close()
+ with open(tn, "wb") as fp:
+ fp.write(m.group(1) + b"\n")
try:
tn2 = (outfile or tempfile.mktemp())
status, output = subproc(r'openssl x509 -in "%s" -out "%s"' %
@@ -39,9 +38,8 @@ def fetch_server_certificate (host, port):
if status != 0:
raise RuntimeError('OpenSSL x509 failed with status %s and '
'output: %r' % (status, output))
- fp = open(tn2, 'rb')
- data = fp.read()
- fp.close()
+ with open(tn2, 'rb') as fp:
+ data = fp.read()
os.unlink(tn2)
return data
finally:
@@ -49,9 +47,8 @@ def fetch_server_certificate (host, port):
if sys.platform.startswith("win"):
tfile = tempfile.mktemp()
- fp = open(tfile, "w")
- fp.write("quit\n")
- fp.close()
+ with open(tfile, "w") as fp:
+ fp.write("quit\n")
try:
status, output = subproc(
'openssl s_client -connect "%s:%s" -showcerts < "%s"' %
diff --git a/Tools/scripts/h2py.py b/Tools/scripts/h2py.py
index 4363c0c..ea37c04 100755
--- a/Tools/scripts/h2py.py
+++ b/Tools/scripts/h2py.py
@@ -69,23 +69,21 @@ def main():
sys.stdout.write('# Generated by h2py from stdin\n')
process(sys.stdin, sys.stdout)
else:
- fp = open(filename, 'r')
- outfile = os.path.basename(filename)
- i = outfile.rfind('.')
- if i > 0: outfile = outfile[:i]
- modname = outfile.upper()
- outfile = modname + '.py'
- outfp = open(outfile, 'w')
- outfp.write('# Generated by h2py from %s\n' % filename)
- filedict = {}
- for dir in searchdirs:
- if filename[:len(dir)] == dir:
- filedict[filename[len(dir)+1:]] = None # no '/' trailing
- importable[filename[len(dir)+1:]] = modname
- break
- process(fp, outfp)
- outfp.close()
- fp.close()
+ with open(filename) as fp:
+ outfile = os.path.basename(filename)
+ i = outfile.rfind('.')
+ if i > 0: outfile = outfile[:i]
+ modname = outfile.upper()
+ outfile = modname + '.py'
+ with open(outfile, 'w') as outfp:
+ outfp.write('# Generated by h2py from %s\n' % filename)
+ filedict = {}
+ for dir in searchdirs:
+ if filename[:len(dir)] == dir:
+ filedict[filename[len(dir)+1:]] = None # no '/' trailing
+ importable[filename[len(dir)+1:]] = modname
+ break
+ process(fp, outfp)
def pytify(body):
# replace ignored patterns by spaces
@@ -161,9 +159,10 @@ def process(fp, outfp, env = {}):
except IOError:
pass
if inclfp:
- outfp.write(
- '\n# Included from %s\n' % filename)
- process(inclfp, outfp, env)
+ with inclfp:
+ outfp.write(
+ '\n# Included from %s\n' % filename)
+ process(inclfp, outfp, env)
else:
sys.stderr.write('Warning - could not find file %s\n' %
filename)
diff --git a/Tools/scripts/ifdef.py b/Tools/scripts/ifdef.py
index b1711ce..22249b2 100755
--- a/Tools/scripts/ifdef.py
+++ b/Tools/scripts/ifdef.py
@@ -45,9 +45,8 @@ def main():
if filename == '-':
process(sys.stdin, sys.stdout)
else:
- f = open(filename, 'r')
- process(f, sys.stdout)
- f.close()
+ with open(filename) as f:
+ process(f, sys.stdout)
def process(fpi, fpo):
keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
diff --git a/Tools/scripts/md5sum.py b/Tools/scripts/md5sum.py
index 9cf4bdc..f910576 100755
--- a/Tools/scripts/md5sum.py
+++ b/Tools/scripts/md5sum.py
@@ -47,10 +47,10 @@ def printsum(filename, out=sys.stdout):
except IOError as msg:
sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg))
return 1
- if fnfilter:
- filename = fnfilter(filename)
- sts = printsumfp(fp, filename, out)
- fp.close()
+ with fp:
+ if fnfilter:
+ filename = fnfilter(filename)
+ sts = printsumfp(fp, filename, out)
return sts
def printsumfp(fp, filename, out=sys.stdout):
diff --git a/Tools/scripts/mkreal.py b/Tools/scripts/mkreal.py
index b21909e..f169da4 100755
--- a/Tools/scripts/mkreal.py
+++ b/Tools/scripts/mkreal.py
@@ -18,14 +18,13 @@ def mkrealfile(name):
st = os.stat(name) # Get the mode
mode = S_IMODE(st[ST_MODE])
linkto = os.readlink(name) # Make sure again it's a symlink
- f_in = open(name, 'r') # This ensures it's a file
- os.unlink(name)
- f_out = open(name, 'w')
- while 1:
- buf = f_in.read(BUFSIZE)
- if not buf: break
- f_out.write(buf)
- del f_out # Flush data to disk before changing mode
+ with open(name, 'rb') as f_in: # This ensures it's a file
+ os.unlink(name)
+ with open(name, 'wb') as f_out:
+ while 1:
+ buf = f_in.read(BUFSIZE)
+ if not buf: break
+ f_out.write(buf)
os.chmod(name, mode)
def mkrealdir(name):
diff --git a/Tools/scripts/nm2def.py b/Tools/scripts/nm2def.py
index 83bbcd7..a885ebd 100755
--- a/Tools/scripts/nm2def.py
+++ b/Tools/scripts/nm2def.py
@@ -42,7 +42,8 @@ NM = 'nm -p -g %s' # For Linux, use "nm -g %s"
def symbols(lib=PYTHONLIB,types=('T','C','D')):
- lines = os.popen(NM % lib).readlines()
+ with os.popen(NM % lib) as pipe:
+ lines = pipe.readlines()
lines = [s.strip() for s in lines]
symbols = {}
for line in lines:
@@ -97,7 +98,7 @@ def main():
exports = export_list(s)
f = sys.stdout # open('PC/python_nt.def','w')
f.write(DEF_TEMPLATE % (exports))
- f.close()
+ # f.close()
if __name__ == '__main__':
main()
diff --git a/Tools/scripts/objgraph.py b/Tools/scripts/objgraph.py
index 3bb1712..add41e6 100755
--- a/Tools/scripts/objgraph.py
+++ b/Tools/scripts/objgraph.py
@@ -180,7 +180,8 @@ def main():
if filename == '-':
readinput(sys.stdin)
else:
- readinput(open(filename, 'r'))
+ with open(filename) as f:
+ readinput(f)
#
warndups()
#
diff --git a/Tools/scripts/parseentities.py b/Tools/scripts/parseentities.py
index c686b02..0229d3a 100755
--- a/Tools/scripts/parseentities.py
+++ b/Tools/scripts/parseentities.py
@@ -50,13 +50,15 @@ def writefile(f,defs):
if __name__ == '__main__':
if len(sys.argv) > 1:
- infile = open(sys.argv[1])
+ with open(sys.argv[1]) as infile:
+ text = infile.read()
else:
- infile = sys.stdin
+ text = sys.stdin.read()
+
+ defs = parse(text)
+
if len(sys.argv) > 2:
- outfile = open(sys.argv[2],'w')
+ with open(sys.argv[2],'w') as outfile:
+ writefile(outfile, defs)
else:
- outfile = sys.stdout
- text = infile.read()
- defs = parse(text)
- writefile(outfile,defs)
+ writefile(sys.stdout, defs)
diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py
index c5bf984..1a0cf1c 100755
--- a/Tools/scripts/pathfix.py
+++ b/Tools/scripts/pathfix.py
@@ -103,29 +103,27 @@ def fix(filename):
except IOError as msg:
err('%s: cannot open: %r\n' % (filename, msg))
return 1
- line = f.readline()
- fixed = fixline(line)
- if line == fixed:
- rep(filename+': no change\n')
- f.close()
- return
- head, tail = os.path.split(filename)
- tempname = os.path.join(head, '@' + tail)
- try:
- g = open(tempname, 'wb')
- except IOError as msg:
- f.close()
- err('%s: cannot create: %r\n' % (tempname, msg))
- return 1
- rep(filename + ': updating\n')
- g.write(fixed)
- BUFSIZE = 8*1024
- while 1:
- buf = f.read(BUFSIZE)
- if not buf: break
- g.write(buf)
- g.close()
- f.close()
+ with f:
+ line = f.readline()
+ fixed = fixline(line)
+ if line == fixed:
+ rep(filename+': no change\n')
+ return
+ head, tail = os.path.split(filename)
+ tempname = os.path.join(head, '@' + tail)
+ try:
+ g = open(tempname, 'wb')
+ except IOError as msg:
+ err('%s: cannot create: %r\n' % (tempname, msg))
+ return 1
+ with g:
+ rep(filename + ': updating\n')
+ g.write(fixed)
+ BUFSIZE = 8*1024
+ while 1:
+ buf = f.read(BUFSIZE)
+ if not buf: break
+ g.write(buf)
# Finishing touch -- move files
diff --git a/Tools/scripts/pdeps.py b/Tools/scripts/pdeps.py
index f8218ac..4e8e930 100755
--- a/Tools/scripts/pdeps.py
+++ b/Tools/scripts/pdeps.py
@@ -64,29 +64,28 @@ m_from = re.compile('^[ \t]*import[ \t]+([^#]+)')
# Collect data from one file
#
def process(filename, table):
- fp = open(filename, 'r')
- mod = os.path.basename(filename)
- if mod[-3:] == '.py':
- mod = mod[:-3]
- table[mod] = list = []
- while 1:
- line = fp.readline()
- if not line: break
- while line[-1:] == '\\':
- nextline = fp.readline()
- if not nextline: break
- line = line[:-1] + nextline
- m_found = m_import.match(line) or m_from.match(line)
- if m_found:
- (a, b), (a1, b1) = m_found.regs[:2]
- else: continue
- words = line[a1:b1].split(',')
- # print '#', line, words
- for word in words:
- word = word.strip()
- if word not in list:
- list.append(word)
- fp.close()
+ with open(filename) as fp:
+ mod = os.path.basename(filename)
+ if mod[-3:] == '.py':
+ mod = mod[:-3]
+ table[mod] = list = []
+ while 1:
+ line = fp.readline()
+ if not line: break
+ while line[-1:] == '\\':
+ nextline = fp.readline()
+ if not nextline: break
+ line = line[:-1] + nextline
+ m_found = m_import.match(line) or m_from.match(line)
+ if m_found:
+ (a, b), (a1, b1) = m_found.regs[:2]
+ else: continue
+ words = line[a1:b1].split(',')
+ # print '#', line, words
+ for word in words:
+ word = word.strip()
+ if word not in list:
+ list.append(word)
# Compute closure (this is in fact totally general)
diff --git a/Tools/scripts/ptags.py b/Tools/scripts/ptags.py
index 396cbd0..eedd411 100755
--- a/Tools/scripts/ptags.py
+++ b/Tools/scripts/ptags.py
@@ -19,9 +19,9 @@ def main():
for filename in args:
treat_file(filename)
if tags:
- fp = open('tags', 'w')
- tags.sort()
- for s in tags: fp.write(s)
+ with open('tags', 'w') as fp:
+ tags.sort()
+ for s in tags: fp.write(s)
expr = r'^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
@@ -33,21 +33,22 @@ def treat_file(filename):
except:
sys.stderr.write('Cannot open %s\n' % filename)
return
- base = os.path.basename(filename)
- if base[-3:] == '.py':
- base = base[:-3]
- s = base + '\t' + filename + '\t' + '1\n'
- tags.append(s)
- while 1:
- line = fp.readline()
- if not line:
- break
- m = matcher.match(line)
- if m:
- content = m.group(0)
- name = m.group(2)
- s = name + '\t' + filename + '\t/^' + content + '/\n'
- tags.append(s)
+ with fp:
+ base = os.path.basename(filename)
+ if base[-3:] == '.py':
+ base = base[:-3]
+ s = base + '\t' + filename + '\t' + '1\n'
+ tags.append(s)
+ while 1:
+ line = fp.readline()
+ if not line:
+ break
+ m = matcher.match(line)
+ if m:
+ content = m.group(0)
+ name = m.group(2)
+ s = name + '\t' + filename + '\t/^' + content + '/\n'
+ tags.append(s)
if __name__ == '__main__':
main()
diff --git a/Tools/scripts/rgrep.py b/Tools/scripts/rgrep.py
index 1917e05..c39bf93 100755
--- a/Tools/scripts/rgrep.py
+++ b/Tools/scripts/rgrep.py
@@ -30,29 +30,30 @@ def main():
f = open(filename)
except IOError as msg:
usage("can't open %r: %s" % (filename, msg), 1)
- f.seek(0, 2)
- pos = f.tell()
- leftover = None
- while pos > 0:
- size = min(pos, bufsize)
- pos = pos - size
- f.seek(pos)
- buffer = f.read(size)
- lines = buffer.split("\n")
- del buffer
- if leftover is None:
- if not lines[-1]:
- del lines[-1]
- else:
- lines[-1] = lines[-1] + leftover
- if pos > 0:
- leftover = lines[0]
- del lines[0]
- else:
- leftover = None
- for line in reversed(lines):
- if prog.search(line):
- print(line)
+ with f:
+ f.seek(0, 2)
+ pos = f.tell()
+ leftover = None
+ while pos > 0:
+ size = min(pos, bufsize)
+ pos = pos - size
+ f.seek(pos)
+ buffer = f.read(size)
+ lines = buffer.split("\n")
+ del buffer
+ if leftover is None:
+ if not lines[-1]:
+ del lines[-1]
+ else:
+ lines[-1] = lines[-1] + leftover
+ if pos > 0:
+ leftover = lines[0]
+ del lines[0]
+ else:
+ leftover = None
+ for line in reversed(lines):
+ if prog.search(line):
+ print(line)
def usage(msg, code=2):