bpo-22831: Use "with" to avoid possible fd leaks in distutils. (GH-10921)

This commit is contained in:
Serhiy Storchaka 2018-12-20 19:00:14 +02:00 committed by GitHub
parent 71f82a2f20
commit c5d5dfdb22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 67 deletions

View File

@ -166,6 +166,7 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
zip = zipfile.ZipFile(zip_filename, "w", zip = zipfile.ZipFile(zip_filename, "w",
compression=zipfile.ZIP_STORED) compression=zipfile.ZIP_STORED)
with zip:
if base_dir != os.curdir: if base_dir != os.curdir:
path = os.path.normpath(os.path.join(base_dir, '')) path = os.path.normpath(os.path.join(base_dir, ''))
zip.write(path, path) zip.write(path, path)
@ -180,7 +181,6 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
if os.path.isfile(path): if os.path.isfile(path):
zip.write(path, path) zip.write(path, path)
log.info("adding '%s'", path) log.info("adding '%s'", path)
zip.close()
return zip_filename return zip_filename

View File

@ -390,7 +390,7 @@ class bdist_msi(Command):
# entries for each version as the above code does # entries for each version as the above code does
if self.pre_install_script: if self.pre_install_script:
scriptfn = os.path.join(self.bdist_dir, "preinstall.bat") scriptfn = os.path.join(self.bdist_dir, "preinstall.bat")
f = open(scriptfn, "w") with open(scriptfn, "w") as f:
# The batch file will be executed with [PYTHON], so that %1 # The batch file will be executed with [PYTHON], so that %1
# is the path to the Python interpreter; %0 will be the path # is the path to the Python interpreter; %0 will be the path
# of the batch file. # of the batch file.
@ -400,8 +400,8 @@ class bdist_msi(Command):
# """ # """
# <actual script> # <actual script>
f.write('rem ="""\n%1 %0\nexit\n"""\n') f.write('rem ="""\n%1 %0\nexit\n"""\n')
f.write(open(self.pre_install_script).read()) with open(self.pre_install_script) as fin:
f.close() f.write(fin.read())
add_data(self.db, "Binary", add_data(self.db, "Binary",
[("PreInstall", msilib.Binary(scriptfn)) [("PreInstall", msilib.Binary(scriptfn))
]) ])

View File

@ -106,7 +106,7 @@ class config(Command):
def _gen_temp_sourcefile(self, body, headers, lang): def _gen_temp_sourcefile(self, body, headers, lang):
filename = "_configtest" + LANG_EXT[lang] filename = "_configtest" + LANG_EXT[lang]
file = open(filename, "w") with open(filename, "w") as file:
if headers: if headers:
for header in headers: for header in headers:
file.write("#include <%s>\n" % header) file.write("#include <%s>\n" % header)
@ -114,7 +114,6 @@ class config(Command):
file.write(body) file.write(body)
if body[-1] != "\n": if body[-1] != "\n":
file.write("\n") file.write("\n")
file.close()
return filename return filename
def _preprocess(self, body, headers, include_dirs, lang): def _preprocess(self, body, headers, include_dirs, lang):
@ -203,7 +202,7 @@ class config(Command):
if isinstance(pattern, str): if isinstance(pattern, str):
pattern = re.compile(pattern) pattern = re.compile(pattern)
file = open(out) with open(out) as file:
match = False match = False
while True: while True:
line = file.readline() line = file.readline()
@ -213,7 +212,6 @@ class config(Command):
match = True match = True
break break
file.close()
self._clean() self._clean()
return match return match

View File

@ -407,14 +407,13 @@ class sdist(Command):
distribution. distribution.
""" """
log.info("reading manifest file '%s'", self.manifest) log.info("reading manifest file '%s'", self.manifest)
manifest = open(self.manifest) with open(self.manifest) as manifest:
for line in manifest: for line in manifest:
# ignore comments and blank lines # ignore comments and blank lines
line = line.strip() line = line.strip()
if line.startswith('#') or not line: if line.startswith('#') or not line:
continue continue
self.filelist.append(line) self.filelist.append(line)
manifest.close()
def make_release_tree(self, base_dir, files): def make_release_tree(self, base_dir, files):
"""Create the directory tree that will become the source """Create the directory tree that will become the source

View File

@ -378,6 +378,7 @@ def byte_compile (py_files,
else: else:
script = open(script_name, "w") script = open(script_name, "w")
with script:
script.write("""\ script.write("""\
from distutils.util import byte_compile from distutils.util import byte_compile
files = [ files = [
@ -405,8 +406,6 @@ byte_compile(files, optimize=%r, force=%r,
direct=1) direct=1)
""" % (optimize, force, prefix, base_dir, verbose)) """ % (optimize, force, prefix, base_dir, verbose))
script.close()
cmd = [sys.executable] cmd = [sys.executable]
cmd.extend(subprocess._optim_args_from_interpreter_flags()) cmd.extend(subprocess._optim_args_from_interpreter_flags())
cmd.append(script_name) cmd.append(script_name)