Commit 817e0bd7 authored by Mickaël Schoentgen's avatar Mickaël Schoentgen Committed by Inada Naoki

bpo-35416: fix potential resource warnings in distutils (GH-10918)

parent 868d8498
...@@ -537,7 +537,8 @@ class bdist_rpm(Command): ...@@ -537,7 +537,8 @@ class bdist_rpm(Command):
'', '',
'%' + rpm_opt,]) '%' + rpm_opt,])
if val: if val:
spec_file.extend(open(val, 'r').read().split('\n')) with open(val) as f:
spec_file.extend(f.read().split('\n'))
else: else:
spec_file.append(default) spec_file.append(default)
......
...@@ -247,47 +247,49 @@ class bdist_wininst(Command): ...@@ -247,47 +247,49 @@ class bdist_wininst(Command):
self.announce("creating %s" % installer_name) self.announce("creating %s" % installer_name)
if bitmap: if bitmap:
bitmapdata = open(bitmap, "rb").read() with open(bitmap, "rb") as f:
bitmapdata = f.read()
bitmaplen = len(bitmapdata) bitmaplen = len(bitmapdata)
else: else:
bitmaplen = 0 bitmaplen = 0
file = open(installer_name, "wb") with open(installer_name, "wb") as file:
file.write(self.get_exe_bytes()) file.write(self.get_exe_bytes())
if bitmap: if bitmap:
file.write(bitmapdata) file.write(bitmapdata)
# Convert cfgdata from unicode to ascii, mbcs encoded # Convert cfgdata from unicode to ascii, mbcs encoded
if isinstance(cfgdata, str): if isinstance(cfgdata, str):
cfgdata = cfgdata.encode("mbcs") cfgdata = cfgdata.encode("mbcs")
# Append the pre-install script # Append the pre-install script
cfgdata = cfgdata + b"\0"
if self.pre_install_script:
# We need to normalize newlines, so we open in text mode and
# convert back to bytes. "latin-1" simply avoids any possible
# failures.
with open(self.pre_install_script, "r",
encoding="latin-1") as script:
script_data = script.read().encode("latin-1")
cfgdata = cfgdata + script_data + b"\n\0"
else:
# empty pre-install script
cfgdata = cfgdata + b"\0" cfgdata = cfgdata + b"\0"
file.write(cfgdata) if self.pre_install_script:
# We need to normalize newlines, so we open in text mode and
# The 'magic number' 0x1234567B is used to make sure that the # convert back to bytes. "latin-1" simply avoids any possible
# binary layout of 'cfgdata' is what the wininst.exe binary # failures.
# expects. If the layout changes, increment that number, make with open(self.pre_install_script, "r",
# the corresponding changes to the wininst.exe sources, and encoding="latin-1") as script:
# recompile them. script_data = script.read().encode("latin-1")
header = struct.pack("<iii", cfgdata = cfgdata + script_data + b"\n\0"
0x1234567B, # tag else:
len(cfgdata), # length # empty pre-install script
bitmaplen, # number of bytes in bitmap cfgdata = cfgdata + b"\0"
) file.write(cfgdata)
file.write(header)
file.write(open(arcname, "rb").read()) # The 'magic number' 0x1234567B is used to make sure that the
# binary layout of 'cfgdata' is what the wininst.exe binary
# expects. If the layout changes, increment that number, make
# the corresponding changes to the wininst.exe sources, and
# recompile them.
header = struct.pack("<iii",
0x1234567B, # tag
len(cfgdata), # length
bitmaplen, # number of bytes in bitmap
)
file.write(header)
with open(arcname, "rb") as f:
file.write(f.read())
def get_installer_filename(self, fullname): def get_installer_filename(self, fullname):
# Factored out to allow overriding in subclasses # Factored out to allow overriding in subclasses
......
...@@ -125,8 +125,9 @@ class upload(PyPIRCCommand): ...@@ -125,8 +125,9 @@ class upload(PyPIRCCommand):
data['comment'] = '' data['comment'] = ''
if self.sign: if self.sign:
data['gpg_signature'] = (os.path.basename(filename) + ".asc", with open(filename + ".asc", "rb") as f:
open(filename+".asc", "rb").read()) data['gpg_signature'] = (os.path.basename(filename) + ".asc",
f.read())
# set up the authentication # set up the authentication
user_pass = (self.username + ":" + self.password).encode('ascii') user_pass = (self.username + ":" + self.password).encode('ascii')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment