Fixes issue 1571:

Adding missing close for open.
If the "close()" call is missing after a "open(filename)" call, the filename isn't guaranteed to be closed before the interpreter exits.
This is generally a bad practice as explained here: https://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important

Also replaced "fid=open(filename) fid.close()" statements for files with the safer
"with open(filename) as fid:" blocks. See https://www.python.org/dev/peps/pep-0343/
This commit is contained in:
Per A. Brodtkorb
2020-03-23 17:16:44 +01:00
parent 8e2627e8e3
commit e4e8bf8317
38 changed files with 230 additions and 266 deletions

View File

@@ -232,9 +232,8 @@ class InheritanceDiagram(object):
mapfile = outfn + '.map'
if os.path.isfile(outfn) and os.path.isfile(mapfile):
fid = open(mapfile, 'rt')
map = fid.read()
fid.close()
with open(mapfile, 'rt') as fid:
map = fid.read()
return os.path.split(outfn)[1], map.replace('\n', ' ')
code = self.generate_dot(class_summary)
@@ -288,8 +287,7 @@ class InheritanceDiagram(object):
if p.returncode != 0:
print(('\nERROR: Graphviz `dot` command exited with error:\n[stderr]\n%s\n[stdout]\n%s\n\n' % (stderr, stdout)))
fid = open(mapfile, 'rt')
map = fid.read()
fid.close()
with open(mapfile, 'rt') as fid:
map = fid.read()
return os.path.split(outfn)[1], map.replace('\n', ' ')

View File

@@ -621,7 +621,8 @@ def ModuleHunter(init_name, import_name, version):
print('Message: %s' % message)
module_name = os.path.splitext(getfile(mainmod))[0] + '.py'
contents = open(module_name, 'rt').read()
with open(module_name, 'rt') as fid:
contents = fid.read()
constants = CONSTANT_RE.findall(contents)
library_class, count = describe_module(mainmod, kind=object_types.LIBRARY, constants=constants)

View File

@@ -122,9 +122,8 @@ def buildEnumsAndMethods(sphinxDir):
for input in textfiles:
fid = textfile_open(input, 'rt')
orig_text = text = fid.read()
fid.close()
with textfile_open(input, 'rt') as fid:
orig_text = text = fid.read()
for old, new in list(enum_dict.items()):
text = text.replace(old, new)
@@ -191,9 +190,8 @@ def buildEnumsAndMethods(sphinxDir):
text = addSpacesToLinks(text)
if text != orig_text:
fid = textfile_open(input, 'wt')
fid.write(text)
fid.close()
with textfile_open(input, 'wt') as fid:
fid.write(text)
if not unreferenced_classes:
return
@@ -208,17 +206,17 @@ def buildEnumsAndMethods(sphinxDir):
'appear.\n\n'
fid = textfile_open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt')
fid.write('\n')
fid.write('='*50 + ' ' + '='*50 + '\n')
fid.write('%-50s %-50s\n'%('Reference', 'File Name(s)'))
fid.write('='*50 + ' ' + '='*50 + '\n')
with textfile_open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt') as fid:
fid.write('\n')
fid.write('='*50 + ' ' + '='*50 + '\n')
fid.write('%-50s %-50s\n'%('Reference', 'File Name(s)'))
fid.write('='*50 + ' ' + '='*50 + '\n')
for key in sorted(unreferenced_classes):
fid.write('%-50s %-50s\n'%(key, ', '.join(unreferenced_classes[key])))
for key in sorted(unreferenced_classes):
fid.write('%-50s %-50s\n'%(key, ', '.join(unreferenced_classes[key])))
fid.write('='*50 + ' ' + '='*50 + '\n')
fid.write('='*50 + ' ' + '='*50 + '\n')
fid.close()
print((warn%(len(unreferenced_classes))))