bpo-35416: Fix potential resource warnings in distutils · python/cpython@bbcfda4
@@ -247,47 +247,49 @@ def create_exe(self, arcname, fullname, bitmap=None):
247247self.announce("creating %s" % installer_name)
248248249249if bitmap:
250-bitmapdata = open(bitmap, "rb").read()
250+with open(bitmap, "rb") as f:
251+bitmapdata = f.read()
251252bitmaplen = len(bitmapdata)
252253else:
253254bitmaplen = 0
254255255-file = open(installer_name, "wb")
256-file.write(self.get_exe_bytes())
257-if bitmap:
258-file.write(bitmapdata)
259-260-# Convert cfgdata from unicode to ascii, mbcs encoded
261-if isinstance(cfgdata, str):
262-cfgdata = cfgdata.encode("mbcs")
263-264-# Append the pre-install script
265-cfgdata = cfgdata + b"\0"
266-if self.pre_install_script:
267-# We need to normalize newlines, so we open in text mode and
268-# convert back to bytes. "latin-1" simply avoids any possible
269-# failures.
270-with open(self.pre_install_script, "r",
271-encoding="latin-1") as script:
272-script_data = script.read().encode("latin-1")
273-cfgdata = cfgdata + script_data + b"\n\0"
274-else:
275-# empty pre-install script
256+with open(installer_name, "wb") as file:
257+file.write(self.get_exe_bytes())
258+if bitmap:
259+file.write(bitmapdata)
260+261+# Convert cfgdata from unicode to ascii, mbcs encoded
262+if isinstance(cfgdata, str):
263+cfgdata = cfgdata.encode("mbcs")
264+265+# Append the pre-install script
276266cfgdata = cfgdata + b"\0"
277-file.write(cfgdata)
278-279-# The 'magic number' 0x1234567B is used to make sure that the
280-# binary layout of 'cfgdata' is what the wininst.exe binary
281-# expects. If the layout changes, increment that number, make
282-# the corresponding changes to the wininst.exe sources, and
283-# recompile them.
284-header = struct.pack("<iii",
285-0x1234567B, # tag
286-len(cfgdata), # length
287-bitmaplen, # number of bytes in bitmap
288- )
289-file.write(header)
290-file.write(open(arcname, "rb").read())
267+if self.pre_install_script:
268+# We need to normalize newlines, so we open in text mode and
269+# convert back to bytes. "latin-1" simply avoids any possible
270+# failures.
271+with open(self.pre_install_script, "r",
272+encoding="latin-1") as script:
273+script_data = script.read().encode("latin-1")
274+cfgdata = cfgdata + script_data + b"\n\0"
275+else:
276+# empty pre-install script
277+cfgdata = cfgdata + b"\0"
278+file.write(cfgdata)
279+280+# The 'magic number' 0x1234567B is used to make sure that the
281+# binary layout of 'cfgdata' is what the wininst.exe binary
282+# expects. If the layout changes, increment that number, make
283+# the corresponding changes to the wininst.exe sources, and
284+# recompile them.
285+header = struct.pack("<iii",
286+0x1234567B, # tag
287+len(cfgdata), # length
288+bitmaplen, # number of bytes in bitmap
289+ )
290+file.write(header)
291+with open(arcname, "rb") as f:
292+file.write(f.read())
291293292294def get_installer_filename(self, fullname):
293295# Factored out to allow overriding in subclasses