Skip to content

Commit bbcfda4

Browse files
committed
bpo-35416: Fix potential resource warnings in distutils
1 parent 2a89343 commit bbcfda4

7 files changed

Lines changed: 79 additions & 78 deletions

File tree

Lib/distutils/command/bdist_msi.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,17 @@ def add_scripts(self):
390390
# entries for each version as the above code does
391391
if self.pre_install_script:
392392
scriptfn = os.path.join(self.bdist_dir, "preinstall.bat")
393-
f = open(scriptfn, "w")
394-
# The batch file will be executed with [PYTHON], so that %1
395-
# is the path to the Python interpreter; %0 will be the path
396-
# of the batch file.
397-
# rem ="""
398-
# %1 %0
399-
# exit
400-
# """
401-
# <actual script>
402-
f.write('rem ="""\n%1 %0\nexit\n"""\n')
403-
f.write(open(self.pre_install_script).read())
404-
f.close()
393+
with open(scriptfn, "w") as fw, open(self.pre_install_script) as fr:
394+
# The batch file will be executed with [PYTHON], so that %1
395+
# is the path to the Python interpreter; %0 will be the path
396+
# of the batch file.
397+
# rem ="""
398+
# %1 %0
399+
# exit
400+
# """
401+
# <actual script>
402+
fw.write('rem ="""\n%1 %0\nexit\n"""\n')
403+
fw.write(fr.read())
405404
add_data(self.db, "Binary",
406405
[("PreInstall", msilib.Binary(scriptfn))
407406
])

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/config.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,14 @@ def _check_compiler(self):
106106

107107
def _gen_temp_sourcefile(self, body, headers, lang):
108108
filename = "_configtest" + LANG_EXT[lang]
109-
file = open(filename, "w")
110-
if headers:
111-
for header in headers:
112-
file.write("#include <%s>\n" % header)
113-
file.write("\n")
114-
file.write(body)
115-
if body[-1] != "\n":
116-
file.write("\n")
117-
file.close()
109+
with open(filename, "w") as file:
110+
if headers:
111+
for header in headers:
112+
file.write("#include <%s>\n" % header)
113+
file.write("\n")
114+
file.write(body)
115+
if body[-1] != "\n":
116+
file.write("\n")
118117
return filename
119118

120119
def _preprocess(self, body, headers, include_dirs, lang):
@@ -203,17 +202,16 @@ def search_cpp(self, pattern, body=None, headers=None, include_dirs=None,
203202
if isinstance(pattern, str):
204203
pattern = re.compile(pattern)
205204

206-
file = open(out)
207-
match = False
208-
while True:
209-
line = file.readline()
210-
if line == '':
211-
break
212-
if pattern.search(line):
213-
match = True
214-
break
205+
with open(out) as file:
206+
match = False
207+
while True:
208+
line = file.readline()
209+
if line == '':
210+
break
211+
if pattern.search(line):
212+
match = True
213+
break
215214

216-
file.close()
217215
self._clean()
218216
return match
219217

Lib/distutils/command/sdist.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,13 @@ def read_manifest(self):
407407
distribution.
408408
"""
409409
log.info("reading manifest file '%s'", self.manifest)
410-
manifest = open(self.manifest)
411-
for line in manifest:
412-
# ignore comments and blank lines
413-
line = line.strip()
414-
if line.startswith('#') or not line:
415-
continue
416-
self.filelist.append(line)
417-
manifest.close()
410+
with open(self.manifest) as manifest:
411+
for line in manifest:
412+
# ignore comments and blank lines
413+
line = line.strip()
414+
if line.startswith('#') or not line:
415+
continue
416+
self.filelist.append(line)
418417

419418
def make_release_tree(self, base_dir, files):
420419
"""Create the directory tree that will become the source

Lib/distutils/command/upload.py

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

133133
if self.sign:
134-
data['gpg_signature'] = (os.path.basename(filename) + ".asc",
135-
open(filename+".asc", "rb").read())
134+
with open(filename + ".asc", "rb") as f:
135+
data['gpg_signature'] = (os.path.basename(filename) + ".asc",
136+
f.read())
136137

137138
# set up the authentication
138139
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)