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:
err(substfile + ': cannot read substfile: ' + str(msg) + '\n')
sys.exit(1)
with fp:
lineno = 0
while 1:
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: previous: %r\n' % (substfile, lineno, Dict[key]))
Dict[key] = value
fp.close()
if __name__ == '__main__':
main()

View File

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

View File

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

View File

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

View File

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