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

@@ -105,11 +105,10 @@ class PiWrapperGenerator(generators.WrapperGeneratorBase, FixWxPrefix):
def _checkAndWriteHeader(destFile, header, docstring):
if not os.path.exists(destFile):
# create the file and write the header
f = textfile_open(destFile, 'wt')
f.write(header)
if docstring:
f.write('\n"""\n%s"""\n' % docstring)
f.close()
with textfile_open(destFile, 'wt') as f:
f.write(header)
if docstring:
f.write('\n"""\n%s"""\n' % docstring)
if not SKIP_PI_FILE:
_checkAndWriteHeader(destFile_pi, header_pi, module.docstring)

View File

@@ -48,10 +48,8 @@ class SipWrapperGenerator(generators.WrapperGeneratorBase):
# Write the contents of the stream to the destination file
if not destFile:
destFile = os.path.join(phoenixRoot, 'sip/gen', module.name + '.sip')
f = textfile_open(destFile, 'wt')
f.write(stream.getvalue())
f.close()
with textfile_open(destFile, 'wt') as f:
f.write(stream.getvalue())
#-----------------------------------------------------------------------
def generateModule(self, module, stream):

View File

@@ -1416,9 +1416,8 @@ class Snippet(Node):
if not os.path.exists(os.path.dirname(self.cpp_file)):
os.makedirs(os.path.dirname(self.cpp_file))
fid = open(self.cpp_file, 'wt')
fid.write(self.snippet)
fid.close()
with open(self.cpp_file, 'wt') as fid:
fid.write(self.snippet)
if not os.path.isfile(self.converted_py):
@@ -1443,10 +1442,8 @@ class Snippet(Node):
for code in py_code.splitlines():
new_py_code += spacer + code + '\n'
fid = open(self.python_file, 'wt')
fid.write(new_py_code)
fid.close()
with open(self.python_file, 'wt') as fid:
fid.write(new_py_code)
else:
fid = open(self.converted_py, 'rt')