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

@ -118,11 +118,10 @@ class HTMLNode:
self.lines.append(line)
def flush(self):
fp = open(self.dirname + '/' + makefile(self.name), 'w')
fp.write(self.prologue)
fp.write(self.text)
fp.write(self.epilogue)
fp.close()
with open(self.dirname + '/' + makefile(self.name), 'w') as fp:
fp.write(self.prologue)
fp.write(self.text)
fp.write(self.epilogue)
def link(self, label, nodename, rel=None, rev=None):
if nodename:
@ -558,14 +557,14 @@ class TexinfoParser:
except IOError as msg:
print('*** Can\'t open include file', repr(file))
return
print('!'*self.debugging, '--> file', repr(file))
save_done = self.done
save_skip = self.skip
save_stack = self.stack
self.includedepth = self.includedepth + 1
self.parserest(fp, 0)
self.includedepth = self.includedepth - 1
fp.close()
with fp:
print('!'*self.debugging, '--> file', repr(file))
save_done = self.done
save_skip = self.skip
save_stack = self.stack
self.includedepth = self.includedepth + 1
self.parserest(fp, 0)
self.includedepth = self.includedepth - 1
self.done = save_done
self.skip = save_skip
self.stack = save_stack
@ -1770,78 +1769,75 @@ class HTMLHelp:
# PROJECT FILE
try:
fp = open(projectfile,'w')
print('[OPTIONS]', file=fp)
print('Auto Index=Yes', file=fp)
print('Binary TOC=No', file=fp)
print('Binary Index=Yes', file=fp)
print('Compatibility=1.1', file=fp)
print('Compiled file=' + resultfile + '', file=fp)
print('Contents file=' + contentfile + '', file=fp)
print('Default topic=' + defaulttopic + '', file=fp)
print('Error log file=ErrorLog.log', file=fp)
print('Index file=' + indexfile + '', file=fp)
print('Title=' + title + '', file=fp)
print('Display compile progress=Yes', file=fp)
print('Full-text search=Yes', file=fp)
print('Default window=main', file=fp)
print('', file=fp)
print('[WINDOWS]', file=fp)
print('main=,"' + contentfile + '","' + indexfile
+ '","","",,,,,0x23520,222,0x1046,[10,10,780,560],'
'0xB0000,,,,,,0', file=fp)
print('', file=fp)
print('[FILES]', file=fp)
print('', file=fp)
self.dumpfiles(fp)
fp.close()
with open(projectfile, 'w') as fp:
print('[OPTIONS]', file=fp)
print('Auto Index=Yes', file=fp)
print('Binary TOC=No', file=fp)
print('Binary Index=Yes', file=fp)
print('Compatibility=1.1', file=fp)
print('Compiled file=' + resultfile + '', file=fp)
print('Contents file=' + contentfile + '', file=fp)
print('Default topic=' + defaulttopic + '', file=fp)
print('Error log file=ErrorLog.log', file=fp)
print('Index file=' + indexfile + '', file=fp)
print('Title=' + title + '', file=fp)
print('Display compile progress=Yes', file=fp)
print('Full-text search=Yes', file=fp)
print('Default window=main', file=fp)
print('', file=fp)
print('[WINDOWS]', file=fp)
print('main=,"' + contentfile + '","' + indexfile
+ '","","",,,,,0x23520,222,0x1046,[10,10,780,560],'
'0xB0000,,,,,,0', file=fp)
print('', file=fp)
print('[FILES]', file=fp)
print('', file=fp)
self.dumpfiles(fp)
except IOError as msg:
print(projectfile, ':', msg)
sys.exit(1)
# CONTENT FILE
try:
fp = open(contentfile,'w')
print('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', file=fp)
print('<!-- This file defines the table of contents -->', file=fp)
print('<HTML>', file=fp)
print('<HEAD>', file=fp)
print('<meta name="GENERATOR" '
'content="Microsoft&reg; HTML Help Workshop 4.1">', file=fp)
print('<!-- Sitemap 1.0 -->', file=fp)
print('</HEAD>', file=fp)
print('<BODY>', file=fp)
print(' <OBJECT type="text/site properties">', file=fp)
print(' <param name="Window Styles" value="0x800025">', file=fp)
print(' <param name="comment" value="title:">', file=fp)
print(' <param name="comment" value="base:">', file=fp)
print(' </OBJECT>', file=fp)
self.dumpnodes(fp)
print('</BODY>', file=fp)
print('</HTML>', file=fp)
fp.close()
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)
print('<HEAD>', file=fp)
print('<meta name="GENERATOR" '
'content="Microsoft&reg; HTML Help Workshop 4.1">', file=fp)
print('<!-- Sitemap 1.0 -->', file=fp)
print('</HEAD>', file=fp)
print('<BODY>', file=fp)
print(' <OBJECT type="text/site properties">', file=fp)
print(' <param name="Window Styles" value="0x800025">', file=fp)
print(' <param name="comment" value="title:">', file=fp)
print(' <param name="comment" value="base:">', file=fp)
print(' </OBJECT>', file=fp)
self.dumpnodes(fp)
print('</BODY>', file=fp)
print('</HTML>', file=fp)
except IOError as msg:
print(contentfile, ':', msg)
sys.exit(1)
# INDEX FILE
try:
fp = open(indexfile ,'w')
print('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', file=fp)
print('<!-- This file defines the index -->', file=fp)
print('<HTML>', file=fp)
print('<HEAD>', file=fp)
print('<meta name="GENERATOR" '
'content="Microsoft&reg; HTML Help Workshop 4.1">', file=fp)
print('<!-- Sitemap 1.0 -->', file=fp)
print('</HEAD>', file=fp)
print('<BODY>', file=fp)
print('<OBJECT type="text/site properties">', file=fp)
print('</OBJECT>', file=fp)
self.dumpindex(fp)
print('</BODY>', file=fp)
print('</HTML>', file=fp)
fp.close()
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)
print('<HEAD>', file=fp)
print('<meta name="GENERATOR" '
'content="Microsoft&reg; HTML Help Workshop 4.1">', file=fp)
print('<!-- Sitemap 1.0 -->', file=fp)
print('</HEAD>', file=fp)
print('<BODY>', file=fp)
print('<OBJECT type="text/site properties">', file=fp)
print('</OBJECT>', file=fp)
self.dumpindex(fp)
print('</BODY>', file=fp)
print('</HTML>', file=fp)
except IOError as msg:
print(indexfile , ':', msg)
sys.exit(1)
@ -2064,8 +2060,8 @@ def test():
print(file, ':', msg)
sys.exit(1)
parser.parse(fp)
fp.close()
with fp:
parser.parse(fp)
parser.report()
htmlhelp.finalize()