Commit 172bb394 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)

parent afbb7a37
...@@ -78,9 +78,9 @@ def test(): ...@@ -78,9 +78,9 @@ def test():
continue continue
else: else:
f = open(filename, 'r') f = open(filename, 'r')
if debug: print('processing', filename, '...') with f:
text = f.read() if debug: print('processing', filename, '...')
f.close() text = f.read()
paralist = text.split('\n\n') paralist = text.split('\n\n')
for para in paralist: for para in paralist:
if debug > 1: print('feeding ...') if debug > 1: print('feeding ...')
......
...@@ -22,17 +22,16 @@ def main(): ...@@ -22,17 +22,16 @@ def main():
port = int(port[i+1:]) port = int(port[i+1:])
host = host[:i] host = host[:i]
command = ' '.join(sys.argv[2:]) command = ' '.join(sys.argv[2:])
s = socket(AF_INET, SOCK_STREAM) with socket(AF_INET, SOCK_STREAM) as s:
s.connect((host, port)) s.connect((host, port))
s.send(command.encode()) s.send(command.encode())
s.shutdown(SHUT_WR) s.shutdown(SHUT_WR)
reply = b'' reply = b''
while True: while True:
data = s.recv(BUFSIZE) data = s.recv(BUFSIZE)
if not data: if not data:
break break
reply += data reply += data
print(reply.decode(), end=' ') print(reply.decode(), end=' ')
s.close()
main() main()
...@@ -26,16 +26,16 @@ def main(): ...@@ -26,16 +26,16 @@ def main():
s.listen(1) s.listen(1)
while True: while True:
conn, (remotehost, remoteport) = s.accept() conn, (remotehost, remoteport) = s.accept()
print('connection from', remotehost, remoteport) with conn:
request = b'' print('connection from', remotehost, remoteport)
while 1: request = b''
data = conn.recv(BUFSIZE) while 1:
if not data: data = conn.recv(BUFSIZE)
break if not data:
request += data break
reply = execute(request.decode()) request += data
conn.send(reply.encode()) reply = execute(request.decode())
conn.close() conn.send(reply.encode())
def execute(request): def execute(request):
stdout = sys.stdout stdout = sys.stdout
......
...@@ -130,7 +130,8 @@ def parse_dsp(dsp): ...@@ -130,7 +130,8 @@ def parse_dsp(dsp):
ret = [] ret = []
dsp_path, dsp_name = os.path.split(dsp) dsp_path, dsp_name = os.path.split(dsp)
try: try:
lines = open(dsp, "r").readlines() with open(dsp, "r") as fp:
lines = fp.readlines()
except IOError as msg: except IOError as msg:
sys.stderr.write("%s: %s\n" % (dsp, msg)) sys.stderr.write("%s: %s\n" % (dsp, msg))
return None return None
......
...@@ -142,7 +142,8 @@ def main(): ...@@ -142,7 +142,8 @@ def main():
# last option can not be "-i", so this ensures "pos+1" is in range! # last option can not be "-i", so this ensures "pos+1" is in range!
if sys.argv[pos] == '-i': if sys.argv[pos] == '-i':
try: try:
options = open(sys.argv[pos+1]).read().split() with open(sys.argv[pos+1]) as infp:
options = infp.read().split()
except IOError as why: except IOError as why:
usage("File name '%s' specified with the -i option " usage("File name '%s' specified with the -i option "
"can not be read - %s" % (sys.argv[pos+1], why) ) "can not be read - %s" % (sys.argv[pos+1], why) )
......
...@@ -561,9 +561,8 @@ def main(): ...@@ -561,9 +561,8 @@ def main():
# initialize list of strings to exclude # initialize list of strings to exclude
if options.excludefilename: if options.excludefilename:
try: try:
fp = open(options.excludefilename) with open(options.excludefilename) as fp:
options.toexclude = fp.readlines() options.toexclude = fp.readlines()
fp.close()
except IOError: except IOError:
print(_( print(_(
"Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr) "Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr)
......
...@@ -96,11 +96,11 @@ def check(file): ...@@ -96,11 +96,11 @@ def check(file):
errprint("%r: I/O Error: %s" % (file, str(msg))) errprint("%r: I/O Error: %s" % (file, str(msg)))
return return
ff = FutureFinder(f, file) with f:
changed = ff.run() ff = FutureFinder(f, file)
if changed: changed = ff.run()
ff.gettherest() if changed:
f.close() ff.gettherest()
if changed: if changed:
if verbose: if verbose:
print("changed.") print("changed.")
...@@ -122,9 +122,8 @@ def check(file): ...@@ -122,9 +122,8 @@ def check(file):
os.rename(file, bak) os.rename(file, bak)
if verbose: if verbose:
print("renamed", file, "to", bak) print("renamed", file, "to", bak)
g = open(file, "w") with open(file, "w") as g:
ff.write(g) ff.write(g)
g.close()
if verbose: if verbose:
print("wrote new", file) print("wrote new", file)
else: else:
......
...@@ -85,9 +85,7 @@ def read(fileiter, pat, whilematch): ...@@ -85,9 +85,7 @@ def read(fileiter, pat, whilematch):
else: else:
break break
def combine(fname): def combinefile(f):
f = open(fname)
fi = iter(f) fi = iter(f)
for line in read(fi, re.compile(r'^Remaining objects:$'), False): for line in read(fi, re.compile(r'^Remaining objects:$'), False):
...@@ -121,8 +119,11 @@ def combine(fname): ...@@ -121,8 +119,11 @@ def combine(fname):
print('[%s->%s]' % (addr2rc[addr], rc), end=' ') print('[%s->%s]' % (addr2rc[addr], rc), end=' ')
print(guts, addr2guts[addr]) print(guts, addr2guts[addr])
f.close()
print("%d objects before, %d after" % (before, after)) print("%d objects before, %d after" % (before, after))
def combine(fname):
with open(fname) as f:
combinefile(f)
if __name__ == '__main__': if __name__ == '__main__':
combine(sys.argv[1]) combine(sys.argv[1])
...@@ -4,18 +4,18 @@ ...@@ -4,18 +4,18 @@
import os, sys, errno import os, sys, errno
def main(): def main():
p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
total, d = None, {} total, d = None, {}
for line in p.readlines(): with os.popen('du ' + ' '.join(sys.argv[1:])) as p:
i = 0 for line in p:
while line[i] in '0123456789': i = i+1 i = 0
size = eval(line[:i]) while line[i] in '0123456789': i = i+1
while line[i] in ' \t': i = i+1 size = eval(line[:i])
filename = line[i:-1] while line[i] in ' \t': i = i+1
comps = filename.split('/') filename = line[i:-1]
if comps[0] == '': comps[0] = '/' comps = filename.split('/')
if comps[len(comps)-1] == '': del comps[len(comps)-1] if comps[0] == '': comps[0] = '/'
total, d = store(size, comps, total, d) if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
try: try:
display(total, d) display(total, d)
except IOError as e: except IOError as e:
......
...@@ -28,29 +28,30 @@ def treat_file(filename, outfp): ...@@ -28,29 +28,30 @@ def treat_file(filename, outfp):
except OSError: except OSError:
sys.stderr.write('Cannot open %s\n'%filename) sys.stderr.write('Cannot open %s\n'%filename)
return return
charno = 0 with fp:
lineno = 0 charno = 0
tags = [] lineno = 0
size = 0 tags = []
while 1: size = 0
line = fp.readline() while 1:
if not line: line = fp.readline()
break if not line:
lineno = lineno + 1 break
m = matcher.search(line) lineno = lineno + 1
if m: m = matcher.search(line)
tag = m.group(0) + '\177%d,%d\n' % (lineno, charno) if m:
tags.append(tag) tag = m.group(0) + '\177%d,%d\n' % (lineno, charno)
size = size + len(tag) tags.append(tag)
charno = charno + len(line) size = size + len(tag)
charno = charno + len(line)
outfp.write('\f\n%s,%d\n' % (filename,size)) outfp.write('\f\n%s,%d\n' % (filename,size))
for tag in tags: for tag in tags:
outfp.write(tag) outfp.write(tag)
def main(): def main():
outfp = open('TAGS', 'w') with open('TAGS', 'w') as outfp:
for filename in sys.argv[1:]: for filename in sys.argv[1:]:
treat_file(filename, outfp) treat_file(filename, outfp)
if __name__=="__main__": if __name__=="__main__":
main() main()
...@@ -55,17 +55,17 @@ def process(filename, listnames): ...@@ -55,17 +55,17 @@ def process(filename, listnames):
except IOError as msg: except IOError as msg:
sys.stderr.write("Can't open: %s\n" % msg) sys.stderr.write("Can't open: %s\n" % msg)
return 1 return 1
g = tokenize.generate_tokens(fp.readline) with fp:
lastrow = None g = tokenize.generate_tokens(fp.readline)
for type, token, (row, col), end, line in g: lastrow = None
if token in ("/", "/="): for type, token, (row, col), end, line in g:
if listnames: if token in ("/", "/="):
print(filename) if listnames:
break print(filename)
if row != lastrow: break
lastrow = row if row != lastrow:
print("%s:%d:%s" % (filename, row, line), end=' ') lastrow = row
fp.close() print("%s:%d:%s" % (filename, row, line), end=' ')
def processdir(dir, listnames): def processdir(dir, listnames):
try: try:
......
...@@ -73,22 +73,19 @@ def main(): ...@@ -73,22 +73,19 @@ def main():
elif opt == '--dry-run': elif opt == '--dry-run':
DRYRUN = 1 DRYRUN = 1
elif opt == '--oldnotice': elif opt == '--oldnotice':
fp = open(arg) with open(arg) as fp:
OLD_NOTICE = fp.read() OLD_NOTICE = fp.read()
fp.close()
elif opt == '--newnotice': elif opt == '--newnotice':
fp = open(arg) with open(arg) as fp:
NEW_NOTICE = fp.read() NEW_NOTICE = fp.read()
fp.close()
for arg in args: for arg in args:
process(arg) process(arg)
def process(file): def process(file):
f = open(file) with open(file) as f:
data = f.read() data = f.read()
f.close()
i = data.find(OLD_NOTICE) i = data.find(OLD_NOTICE)
if i < 0: if i < 0:
if VERBOSE: if VERBOSE:
...@@ -102,9 +99,8 @@ def process(file): ...@@ -102,9 +99,8 @@ def process(file):
data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):] data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
new = file + ".new" new = file + ".new"
backup = file + ".bak" backup = file + ".bak"
f = open(new, "w") with open(new, "w") as f:
f.write(data) f.write(data)
f.close()
os.rename(file, backup) os.rename(file, backup)
os.rename(new, file) os.rename(new, file)
......
...@@ -14,20 +14,18 @@ def main(): ...@@ -14,20 +14,18 @@ def main():
except IOError as msg: except IOError as msg:
print(filename, ': can\'t open :', msg) print(filename, ': can\'t open :', msg)
continue continue
line = f.readline() with f:
if not re.match('^#! */usr/local/bin/python', line): line = f.readline()
print(filename, ': not a /usr/local/bin/python script') if not re.match('^#! */usr/local/bin/python', line):
f.close() print(filename, ': not a /usr/local/bin/python script')
continue continue
rest = f.read() rest = f.read()
f.close()
line = re.sub('/usr/local/bin/python', line = re.sub('/usr/local/bin/python',
'/usr/bin/env python', line) '/usr/bin/env python', line)
print(filename, ':', repr(line)) print(filename, ':', repr(line))
f = open(filename, "w") with open(filename, "w") as f:
f.write(line) f.write(line)
f.write(rest) f.write(rest)
f.close()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -29,9 +29,8 @@ def fetch_server_certificate (host, port): ...@@ -29,9 +29,8 @@ def fetch_server_certificate (host, port):
return None return None
else: else:
tn = tempfile.mktemp() tn = tempfile.mktemp()
fp = open(tn, "wb") with open(tn, "wb") as fp:
fp.write(m.group(1) + b"\n") fp.write(m.group(1) + b"\n")
fp.close()
try: try:
tn2 = (outfile or tempfile.mktemp()) tn2 = (outfile or tempfile.mktemp())
status, output = subproc(r'openssl x509 -in "%s" -out "%s"' % status, output = subproc(r'openssl x509 -in "%s" -out "%s"' %
...@@ -39,9 +38,8 @@ def fetch_server_certificate (host, port): ...@@ -39,9 +38,8 @@ def fetch_server_certificate (host, port):
if status != 0: if status != 0:
raise RuntimeError('OpenSSL x509 failed with status %s and ' raise RuntimeError('OpenSSL x509 failed with status %s and '
'output: %r' % (status, output)) 'output: %r' % (status, output))
fp = open(tn2, 'rb') with open(tn2, 'rb') as fp:
data = fp.read() data = fp.read()
fp.close()
os.unlink(tn2) os.unlink(tn2)
return data return data
finally: finally:
...@@ -49,9 +47,8 @@ def fetch_server_certificate (host, port): ...@@ -49,9 +47,8 @@ def fetch_server_certificate (host, port):
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
tfile = tempfile.mktemp() tfile = tempfile.mktemp()
fp = open(tfile, "w") with open(tfile, "w") as fp:
fp.write("quit\n") fp.write("quit\n")
fp.close()
try: try:
status, output = subproc( status, output = subproc(
'openssl s_client -connect "%s:%s" -showcerts < "%s"' % 'openssl s_client -connect "%s:%s" -showcerts < "%s"' %
......
...@@ -69,23 +69,21 @@ def main(): ...@@ -69,23 +69,21 @@ def main():
sys.stdout.write('# Generated by h2py from stdin\n') sys.stdout.write('# Generated by h2py from stdin\n')
process(sys.stdin, sys.stdout) process(sys.stdin, sys.stdout)
else: else:
fp = open(filename, 'r') with open(filename) as fp:
outfile = os.path.basename(filename) outfile = os.path.basename(filename)
i = outfile.rfind('.') i = outfile.rfind('.')
if i > 0: outfile = outfile[:i] if i > 0: outfile = outfile[:i]
modname = outfile.upper() modname = outfile.upper()
outfile = modname + '.py' outfile = modname + '.py'
outfp = open(outfile, 'w') with open(outfile, 'w') as outfp:
outfp.write('# Generated by h2py from %s\n' % filename) outfp.write('# Generated by h2py from %s\n' % filename)
filedict = {} filedict = {}
for dir in searchdirs: for dir in searchdirs:
if filename[:len(dir)] == dir: if filename[:len(dir)] == dir:
filedict[filename[len(dir)+1:]] = None # no '/' trailing filedict[filename[len(dir)+1:]] = None # no '/' trailing
importable[filename[len(dir)+1:]] = modname importable[filename[len(dir)+1:]] = modname
break break
process(fp, outfp) process(fp, outfp)
outfp.close()
fp.close()
def pytify(body): def pytify(body):
# replace ignored patterns by spaces # replace ignored patterns by spaces
...@@ -161,9 +159,10 @@ def process(fp, outfp, env = {}): ...@@ -161,9 +159,10 @@ def process(fp, outfp, env = {}):
except IOError: except IOError:
pass pass
if inclfp: if inclfp:
outfp.write( with inclfp:
'\n# Included from %s\n' % filename) outfp.write(
process(inclfp, outfp, env) '\n# Included from %s\n' % filename)
process(inclfp, outfp, env)
else: else:
sys.stderr.write('Warning - could not find file %s\n' % sys.stderr.write('Warning - could not find file %s\n' %
filename) filename)
......
...@@ -45,9 +45,8 @@ def main(): ...@@ -45,9 +45,8 @@ def main():
if filename == '-': if filename == '-':
process(sys.stdin, sys.stdout) process(sys.stdin, sys.stdout)
else: else:
f = open(filename, 'r') with open(filename) as f:
process(f, sys.stdout) process(f, sys.stdout)
f.close()
def process(fpi, fpo): def process(fpi, fpo):
keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif') keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
......
...@@ -47,10 +47,10 @@ def printsum(filename, out=sys.stdout): ...@@ -47,10 +47,10 @@ def printsum(filename, out=sys.stdout):
except IOError as msg: except IOError as msg:
sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg)) sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg))
return 1 return 1
if fnfilter: with fp:
filename = fnfilter(filename) if fnfilter:
sts = printsumfp(fp, filename, out) filename = fnfilter(filename)
fp.close() sts = printsumfp(fp, filename, out)
return sts return sts
def printsumfp(fp, filename, out=sys.stdout): def printsumfp(fp, filename, out=sys.stdout):
......
...@@ -18,14 +18,13 @@ def mkrealfile(name): ...@@ -18,14 +18,13 @@ def mkrealfile(name):
st = os.stat(name) # Get the mode st = os.stat(name) # Get the mode
mode = S_IMODE(st[ST_MODE]) mode = S_IMODE(st[ST_MODE])
linkto = os.readlink(name) # Make sure again it's a symlink linkto = os.readlink(name) # Make sure again it's a symlink
f_in = open(name, 'r') # This ensures it's a file with open(name, 'rb') as f_in: # This ensures it's a file
os.unlink(name) os.unlink(name)
f_out = open(name, 'w') with open(name, 'wb') as f_out:
while 1: while 1:
buf = f_in.read(BUFSIZE) buf = f_in.read(BUFSIZE)
if not buf: break if not buf: break
f_out.write(buf) f_out.write(buf)
del f_out # Flush data to disk before changing mode
os.chmod(name, mode) os.chmod(name, mode)
def mkrealdir(name): def mkrealdir(name):
......
...@@ -42,7 +42,8 @@ NM = 'nm -p -g %s' # For Linux, use "nm -g %s" ...@@ -42,7 +42,8 @@ NM = 'nm -p -g %s' # For Linux, use "nm -g %s"
def symbols(lib=PYTHONLIB,types=('T','C','D')): 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] lines = [s.strip() for s in lines]
symbols = {} symbols = {}
for line in lines: for line in lines:
...@@ -97,7 +98,7 @@ def main(): ...@@ -97,7 +98,7 @@ def main():
exports = export_list(s) exports = export_list(s)
f = sys.stdout # open('PC/python_nt.def','w') f = sys.stdout # open('PC/python_nt.def','w')
f.write(DEF_TEMPLATE % (exports)) f.write(DEF_TEMPLATE % (exports))
f.close() # f.close()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -180,7 +180,8 @@ def main(): ...@@ -180,7 +180,8 @@ def main():
if filename == '-': if filename == '-':
readinput(sys.stdin) readinput(sys.stdin)
else: else:
readinput(open(filename, 'r')) with open(filename) as f:
readinput(f)
# #
warndups() warndups()
# #
......
...@@ -50,13 +50,15 @@ def writefile(f,defs): ...@@ -50,13 +50,15 @@ def writefile(f,defs):
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
infile = open(sys.argv[1]) with open(sys.argv[1]) as infile:
text = infile.read()
else: else:
infile = sys.stdin text = sys.stdin.read()
defs = parse(text)
if len(sys.argv) > 2: if len(sys.argv) > 2:
outfile = open(sys.argv[2],'w') with open(sys.argv[2],'w') as outfile:
writefile(outfile, defs)
else: else:
outfile = sys.stdout writefile(sys.stdout, defs)
text = infile.read()
defs = parse(text)
writefile(outfile,defs)
...@@ -103,29 +103,27 @@ def fix(filename): ...@@ -103,29 +103,27 @@ def fix(filename):
except IOError as msg: except IOError as msg:
err('%s: cannot open: %r\n' % (filename, msg)) err('%s: cannot open: %r\n' % (filename, msg))
return 1 return 1
line = f.readline() with f:
fixed = fixline(line) line = f.readline()
if line == fixed: fixed = fixline(line)
rep(filename+': no change\n') if line == fixed:
f.close() rep(filename+': no change\n')
return return
head, tail = os.path.split(filename) head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail) tempname = os.path.join(head, '@' + tail)
try: try:
g = open(tempname, 'wb') g = open(tempname, 'wb')
except IOError as msg: except IOError as msg:
f.close() err('%s: cannot create: %r\n' % (tempname, msg))
err('%s: cannot create: %r\n' % (tempname, msg)) return 1
return 1 with g:
rep(filename + ': updating\n') rep(filename + ': updating\n')
g.write(fixed) g.write(fixed)
BUFSIZE = 8*1024 BUFSIZE = 8*1024
while 1: while 1:
buf = f.read(BUFSIZE) buf = f.read(BUFSIZE)
if not buf: break if not buf: break
g.write(buf) g.write(buf)
g.close()
f.close()
# Finishing touch -- move files # Finishing touch -- move files
......
...@@ -64,29 +64,28 @@ m_from = re.compile('^[ \t]*import[ \t]+([^#]+)') ...@@ -64,29 +64,28 @@ m_from = re.compile('^[ \t]*import[ \t]+([^#]+)')
# Collect data from one file # Collect data from one file
# #
def process(filename, table): def process(filename, table):
fp = open(filename, 'r') with open(filename) as fp:
mod = os.path.basename(filename) mod = os.path.basename(filename)
if mod[-3:] == '.py': if mod[-3:] == '.py':
mod = mod[:-3] mod = mod[:-3]
table[mod] = list = [] table[mod] = list = []
while 1: while 1:
line = fp.readline() line = fp.readline()
if not line: break if not line: break
while line[-1:] == '\\': while line[-1:] == '\\':
nextline = fp.readline() nextline = fp.readline()
if not nextline: break if not nextline: break
line = line[:-1] + nextline line = line[:-1] + nextline
m_found = m_import.match(line) or m_from.match(line) m_found = m_import.match(line) or m_from.match(line)
if m_found: if m_found:
(a, b), (a1, b1) = m_found.regs[:2] (a, b), (a1, b1) = m_found.regs[:2]
else: continue else: continue
words = line[a1:b1].split(',') words = line[a1:b1].split(',')
# print '#', line, words # print '#', line, words
for word in words: for word in words:
word = word.strip() word = word.strip()
if word not in list: if word not in list:
list.append(word) list.append(word)
fp.close()
# Compute closure (this is in fact totally general) # Compute closure (this is in fact totally general)
......
...@@ -19,9 +19,9 @@ def main(): ...@@ -19,9 +19,9 @@ def main():
for filename in args: for filename in args:
treat_file(filename) treat_file(filename)
if tags: if tags:
fp = open('tags', 'w') with open('tags', 'w') as fp:
tags.sort() tags.sort()
for s in tags: fp.write(s) for s in tags: fp.write(s)
expr = r'^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]' expr = r'^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
...@@ -33,21 +33,22 @@ def treat_file(filename): ...@@ -33,21 +33,22 @@ def treat_file(filename):
except: except:
sys.stderr.write('Cannot open %s\n' % filename) sys.stderr.write('Cannot open %s\n' % filename)
return return
base = os.path.basename(filename) with fp:
if base[-3:] == '.py': base = os.path.basename(filename)
base = base[:-3] if base[-3:] == '.py':
s = base + '\t' + filename + '\t' + '1\n' base = base[:-3]
tags.append(s) s = base + '\t' + filename + '\t' + '1\n'
while 1: tags.append(s)
line = fp.readline() while 1:
if not line: line = fp.readline()
break if not line:
m = matcher.match(line) break
if m: m = matcher.match(line)
content = m.group(0) if m:
name = m.group(2) content = m.group(0)
s = name + '\t' + filename + '\t/^' + content + '/\n' name = m.group(2)
tags.append(s) s = name + '\t' + filename + '\t/^' + content + '/\n'
tags.append(s)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -30,29 +30,30 @@ def main(): ...@@ -30,29 +30,30 @@ def main():
f = open(filename) f = open(filename)
except IOError as msg: except IOError as msg:
usage("can't open %r: %s" % (filename, msg), 1) usage("can't open %r: %s" % (filename, msg), 1)
f.seek(0, 2) with f:
pos = f.tell() f.seek(0, 2)
leftover = None pos = f.tell()
while pos > 0: leftover = None
size = min(pos, bufsize) while pos > 0:
pos = pos - size size = min(pos, bufsize)
f.seek(pos) pos = pos - size
buffer = f.read(size) f.seek(pos)
lines = buffer.split("\n") buffer = f.read(size)
del buffer lines = buffer.split("\n")
if leftover is None: del buffer
if not lines[-1]: if leftover is None:
del lines[-1] if not lines[-1]:
else: del lines[-1]
lines[-1] = lines[-1] + leftover else:
if pos > 0: lines[-1] = lines[-1] + leftover
leftover = lines[0] if pos > 0:
del lines[0] leftover = lines[0]
else: del lines[0]
leftover = None else:
for line in reversed(lines): leftover = None
if prog.search(line): for line in reversed(lines):
print(line) if prog.search(line):
print(line)
def usage(msg, code=2): def usage(msg, code=2):
......
...@@ -61,7 +61,8 @@ def gencodecs(prefix): ...@@ -61,7 +61,8 @@ def gencodecs(prefix):
encoding=enc.lower(), encoding=enc.lower(),
owner=loc) owner=loc)
codecpath = os.path.join(prefix, enc + '.py') codecpath = os.path.join(prefix, enc + '.py')
open(codecpath, 'w').write(code) with open(codecpath, 'w') as f:
f.write(code)
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
......
...@@ -72,9 +72,8 @@ def parsecodes(codes, len=len, range=range): ...@@ -72,9 +72,8 @@ def parsecodes(codes, len=len, range=range):
def readmap(filename): def readmap(filename):
f = open(filename,'r') with open(filename) as f:
lines = f.readlines() lines = f.readlines()
f.close()
enc2uni = {} enc2uni = {}
identity = [] identity = []
unmapped = list(range(256)) unmapped = list(range(256))
...@@ -359,18 +358,16 @@ encoding_table = codecs.charmap_build(decoding_table) ...@@ -359,18 +358,16 @@ encoding_table = codecs.charmap_build(decoding_table)
def pymap(name,map,pyfile,encodingname,comments=1): def pymap(name,map,pyfile,encodingname,comments=1):
code = codegen(name,map,encodingname,comments) code = codegen(name,map,encodingname,comments)
f = open(pyfile,'w') with open(pyfile,'w') as f:
f.write(code) f.write(code)
f.close()
def marshalmap(name,map,marshalfile): def marshalmap(name,map,marshalfile):
d = {} d = {}
for e,(u,c) in map.items(): for e,(u,c) in map.items():
d[e] = (u,c) d[e] = (u,c)
f = open(marshalfile,'wb') with open(marshalfile,'wb') as f:
marshal.dump(d,f) marshal.dump(d,f)
f.close()
def convertdir(dir, dirprefix='', nameprefix='', comments=1): def convertdir(dir, dirprefix='', nameprefix='', comments=1):
...@@ -411,8 +408,8 @@ def rewritepythondir(dir, dirprefix='', comments=1): ...@@ -411,8 +408,8 @@ def rewritepythondir(dir, dirprefix='', comments=1):
print('converting %s to %s' % (mapname, print('converting %s to %s' % (mapname,
dirprefix + codefile)) dirprefix + codefile))
try: try:
map = marshal.load(open(os.path.join(dir,mapname), with open(os.path.join(dir, mapname), 'rb') as f:
'rb')) map = marshal.load(f)
if not map: if not map:
print('* map is empty; skipping') print('* map is empty; skipping')
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment