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

This commit is contained in:
Serhiy Storchaka 2019-03-30 08:32:18 +02:00 committed by GitHub
parent 2524fdefc9
commit afbb7a371f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 226 additions and 228 deletions

View File

@ -281,6 +281,7 @@ def addsubst(substfile):
except IOError as msg: except IOError as msg:
err(substfile + ': cannot read substfile: ' + str(msg) + '\n') err(substfile + ': cannot read substfile: ' + str(msg) + '\n')
sys.exit(1) sys.exit(1)
with fp:
lineno = 0 lineno = 0
while 1: while 1:
line = fp.readline() line = fp.readline()
@ -310,7 +311,6 @@ def addsubst(substfile):
err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value)) err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value))
err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key])) err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key]))
Dict[key] = value Dict[key] = value
fp.close()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -179,12 +179,13 @@ PATTERN = (r"^(.+?):(\d+): DeprecationWarning: "
def readwarnings(warningsfile): def readwarnings(warningsfile):
prog = re.compile(PATTERN) prog = re.compile(PATTERN)
warnings = {}
try: try:
f = open(warningsfile) f = open(warningsfile)
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 return
warnings = {} with f:
while 1: while 1:
line = f.readline() line = f.readline()
if not line: if not line:
@ -199,7 +200,6 @@ def readwarnings(warningsfile):
if list is None: if list is None:
warnings[filename] = list = [] warnings[filename] = list = []
list.append((int(lineno), sys.intern(what))) list.append((int(lineno), sys.intern(what)))
f.close()
return warnings return warnings
def process(filename, list): def process(filename, list):
@ -210,6 +210,7 @@ def process(filename, list):
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
with fp:
print("Index:", filename) print("Index:", filename)
f = FileContext(fp) f = FileContext(fp)
list.sort() list.sort()
@ -284,10 +285,9 @@ def process(filename, list):
print("True division / operator at line %d:" % row) print("True division / operator at line %d:" % row)
print("=", line) print("=", line)
elif intlong and floatcomplex: elif intlong and floatcomplex:
print("*** Ambiguous / operator (%s, %s) at line %d:" % ( print("*** Ambiguous / operator (%s, %s) at line %d:" %
"|".join(intlong), "|".join(floatcomplex), row)) ("|".join(intlong), "|".join(floatcomplex), row))
print("?", line) print("?", line)
fp.close()
def reportphantomwarnings(warnings, f): def reportphantomwarnings(warnings, f):
blocks = [] blocks = []

View File

@ -15,8 +15,8 @@ def process(filename):
except IOError as msg: except IOError as msg:
sys.stderr.write('%s: can\'t open: %s\n' % (filename, str(msg))) sys.stderr.write('%s: can\'t open: %s\n' % (filename, str(msg)))
return return
with f:
data = f.read() data = f.read()
f.close()
if data[:2] != '/*': if data[:2] != '/*':
sys.stderr.write('%s does not begin with C comment\n' % filename) sys.stderr.write('%s does not begin with C comment\n' % filename)
return return
@ -25,25 +25,25 @@ def process(filename):
except IOError as msg: except IOError as msg:
sys.stderr.write('%s: can\'t write: %s\n' % (filename, str(msg))) sys.stderr.write('%s: can\'t write: %s\n' % (filename, str(msg)))
return return
with f:
sys.stderr.write('Processing %s ...\n' % filename) sys.stderr.write('Processing %s ...\n' % filename)
magic = 'Py_' magic = 'Py_'
for c in filename: for c in filename:
if ord(c)<=0x80 and c.isalnum(): if ord(c)<=0x80 and c.isalnum():
magic = magic + c.upper() magic = magic + c.upper()
else: magic = magic + '_' else: magic = magic + '_'
sys.stdout = f print('#ifndef', magic, file=f)
print('#ifndef', magic) print('#define', magic, file=f)
print('#define', magic) print('#ifdef __cplusplus', file=f)
print('#ifdef __cplusplus') print('extern "C" {', file=f)
print('extern "C" {') print('#endif', file=f)
print('#endif') print(file=f)
print()
f.write(data) f.write(data)
print() print(file=f)
print('#ifdef __cplusplus') print('#ifdef __cplusplus', file=f)
print('}') print('}', file=f)
print('#endif') print('#endif', file=f)
print('#endif /*', '!'+magic, '*/') print('#endif /*', '!'+magic, '*/', file=f)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -28,14 +28,7 @@ def add_escapes(filename):
for line in fp: for line in fp:
yield html.escape(line) yield html.escape(line)
def gprof2html(input, output, filename):
def main():
filename = "gprof.out"
if sys.argv[1:]:
filename = sys.argv[1]
outputfilename = filename + ".html"
input = add_escapes(filename)
output = open(outputfilename, "w")
output.write(header % filename) output.write(header % filename)
for line in input: for line in input:
output.write(line) output.write(line)
@ -78,7 +71,16 @@ def main():
part = '<a href="#call:%s">%s</a>' % (part, part) part = '<a href="#call:%s">%s</a>' % (part, part)
output.write(part) output.write(part)
output.write(trailer) output.write(trailer)
output.close()
def main():
filename = "gprof.out"
if sys.argv[1:]:
filename = sys.argv[1]
outputfilename = filename + ".html"
input = add_escapes(filename)
with open(outputfilename, "w") as output:
gprof2html(input, output, filename)
webbrowser.open("file:" + os.path.abspath(outputfilename)) webbrowser.open("file:" + os.path.abspath(outputfilename))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -118,11 +118,10 @@ class HTMLNode:
self.lines.append(line) self.lines.append(line)
def flush(self): def flush(self):
fp = open(self.dirname + '/' + makefile(self.name), 'w') with open(self.dirname + '/' + makefile(self.name), 'w') as fp:
fp.write(self.prologue) fp.write(self.prologue)
fp.write(self.text) fp.write(self.text)
fp.write(self.epilogue) fp.write(self.epilogue)
fp.close()
def link(self, label, nodename, rel=None, rev=None): def link(self, label, nodename, rel=None, rev=None):
if nodename: if nodename:
@ -558,6 +557,7 @@ class TexinfoParser:
except IOError as msg: except IOError as msg:
print('*** Can\'t open include file', repr(file)) print('*** Can\'t open include file', repr(file))
return return
with fp:
print('!'*self.debugging, '--> file', repr(file)) print('!'*self.debugging, '--> file', repr(file))
save_done = self.done save_done = self.done
save_skip = self.skip save_skip = self.skip
@ -565,7 +565,6 @@ class TexinfoParser:
self.includedepth = self.includedepth + 1 self.includedepth = self.includedepth + 1
self.parserest(fp, 0) self.parserest(fp, 0)
self.includedepth = self.includedepth - 1 self.includedepth = self.includedepth - 1
fp.close()
self.done = save_done self.done = save_done
self.skip = save_skip self.skip = save_skip
self.stack = save_stack self.stack = save_stack
@ -1770,7 +1769,7 @@ class HTMLHelp:
# PROJECT FILE # PROJECT FILE
try: try:
fp = open(projectfile,'w') with open(projectfile, 'w') as fp:
print('[OPTIONS]', file=fp) print('[OPTIONS]', file=fp)
print('Auto Index=Yes', file=fp) print('Auto Index=Yes', file=fp)
print('Binary TOC=No', file=fp) print('Binary TOC=No', file=fp)
@ -1794,14 +1793,13 @@ class HTMLHelp:
print('[FILES]', file=fp) print('[FILES]', file=fp)
print('', file=fp) print('', file=fp)
self.dumpfiles(fp) self.dumpfiles(fp)
fp.close()
except IOError as msg: except IOError as msg:
print(projectfile, ':', msg) print(projectfile, ':', msg)
sys.exit(1) sys.exit(1)
# CONTENT FILE # CONTENT FILE
try: try:
fp = open(contentfile,'w') with open(contentfile, 'w') as fp:
print('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', file=fp) print('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', file=fp)
print('<!-- This file defines the table of contents -->', file=fp) print('<!-- This file defines the table of contents -->', file=fp)
print('<HTML>', file=fp) print('<HTML>', file=fp)
@ -1819,14 +1817,13 @@ class HTMLHelp:
self.dumpnodes(fp) self.dumpnodes(fp)
print('</BODY>', file=fp) print('</BODY>', file=fp)
print('</HTML>', file=fp) print('</HTML>', file=fp)
fp.close()
except IOError as msg: except IOError as msg:
print(contentfile, ':', msg) print(contentfile, ':', msg)
sys.exit(1) sys.exit(1)
# INDEX FILE # INDEX FILE
try: try:
fp = open(indexfile ,'w') with open(indexfile, 'w') as fp:
print('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', file=fp) print('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', file=fp)
print('<!-- This file defines the index -->', file=fp) print('<!-- This file defines the index -->', file=fp)
print('<HTML>', file=fp) print('<HTML>', file=fp)
@ -1841,7 +1838,6 @@ class HTMLHelp:
self.dumpindex(fp) self.dumpindex(fp)
print('</BODY>', file=fp) print('</BODY>', file=fp)
print('</HTML>', file=fp) print('</HTML>', file=fp)
fp.close()
except IOError as msg: except IOError as msg:
print(indexfile , ':', msg) print(indexfile , ':', msg)
sys.exit(1) sys.exit(1)
@ -2064,8 +2060,8 @@ def test():
print(file, ':', msg) print(file, ':', msg)
sys.exit(1) sys.exit(1)
with fp:
parser.parse(fp) parser.parse(fp)
fp.close()
parser.report() parser.report()
htmlhelp.finalize() htmlhelp.finalize()