mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-08 13:00:07 +01:00
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:
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user