Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58721a9

Browse files
BoboTiGmethane
authored andcommitted
bpo-35416: fix potential resource warnings in distutils (GH-10918)
1 parent 7a0630c commit 58721a9

4 files changed

Lines changed: 44 additions & 39 deletions

File tree

Lib/distutils/command/bdist_rpm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ def _make_spec_file(self):
537537
'',
538538
'%' + rpm_opt,])
539539
if val:
540-
spec_file.extend(open(val, 'r').read().split('\n'))
540+
with open(val) as f:
541+
spec_file.extend(f.read().split('\n'))
541542
else:
542543
spec_file.append(default)
543544

Lib/distutils/command/bdist_wininst.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -247,47 +247,49 @@ def create_exe(self, arcname, fullname, bitmap=None):
247247
self.announce("creating %s" % installer_name)
248248

249249
if bitmap:
250-
bitmapdata = open(bitmap, "rb").read()
250+
with open(bitmap, "rb") as f:
251+
bitmapdata = f.read()
251252
bitmaplen = len(bitmapdata)
252253
else:
253254
bitmaplen = 0
254255

255-
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
276266
cfgdata = 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())
291293

292294
def get_installer_filename(self, fullname):
293295
# Factored out to allow overriding in subclasses

Lib/distutils/command/upload.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ def upload_file(self, command, pyversion, filename):
125125
data['comment'] = ''
126126

127127
if self.sign:
128-
data['gpg_signature'] = (os.path.basename(filename) + ".asc",
129-
open(filename+".asc", "rb").read())
128+
with open(filename + ".asc", "rb") as f:
129+
data['gpg_signature'] = (os.path.basename(filename) + ".asc",
130+
f.read())
130131

131132
# set up the authentication
132133
user_pass = (self.username + ":" + self.password).encode('ascii')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix potential resource warnings in distutils. Patch by Mickaël Schoentgen.

0 commit comments

Comments
 (0)