From bbcfda4c2ae70159501430ca64e5d316910d258e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Wed, 5 Dec 2018 09:55:49 +0100 Subject: [PATCH] bpo-35416: Fix potential resource warnings in distutils --- Lib/distutils/command/bdist_msi.py | 23 +++--- Lib/distutils/command/bdist_rpm.py | 3 +- Lib/distutils/command/bdist_wininst.py | 74 ++++++++++--------- Lib/distutils/command/config.py | 36 +++++---- Lib/distutils/command/sdist.py | 15 ++-- Lib/distutils/command/upload.py | 5 +- .../2018-12-05-09-55-05.bpo-35416.XALKZG.rst | 1 + 7 files changed, 79 insertions(+), 78 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-05-09-55-05.bpo-35416.XALKZG.rst diff --git a/Lib/distutils/command/bdist_msi.py b/Lib/distutils/command/bdist_msi.py index 80104c372d9aef..0b45efba331e40 100644 --- a/Lib/distutils/command/bdist_msi.py +++ b/Lib/distutils/command/bdist_msi.py @@ -390,18 +390,17 @@ def add_scripts(self): # entries for each version as the above code does if self.pre_install_script: scriptfn = os.path.join(self.bdist_dir, "preinstall.bat") - f = open(scriptfn, "w") - # The batch file will be executed with [PYTHON], so that %1 - # is the path to the Python interpreter; %0 will be the path - # of the batch file. - # rem =""" - # %1 %0 - # exit - # """ - # - f.write('rem ="""\n%1 %0\nexit\n"""\n') - f.write(open(self.pre_install_script).read()) - f.close() + with open(scriptfn, "w") as fw, open(self.pre_install_script) as fr: + # The batch file will be executed with [PYTHON], so that %1 + # is the path to the Python interpreter; %0 will be the path + # of the batch file. + # rem =""" + # %1 %0 + # exit + # """ + # + fw.write('rem ="""\n%1 %0\nexit\n"""\n') + fw.write(fr.read()) add_data(self.db, "Binary", [("PreInstall", msilib.Binary(scriptfn)) ]) diff --git a/Lib/distutils/command/bdist_rpm.py b/Lib/distutils/command/bdist_rpm.py index 02f10dd89d913b..20ca7ac6dcffaa 100644 --- a/Lib/distutils/command/bdist_rpm.py +++ b/Lib/distutils/command/bdist_rpm.py @@ -537,7 +537,8 @@ def _make_spec_file(self): '', '%' + rpm_opt,]) if val: - spec_file.extend(open(val, 'r').read().split('\n')) + with open(val) as f: + spec_file.extend(f.read().split('\n')) else: spec_file.append(default) diff --git a/Lib/distutils/command/bdist_wininst.py b/Lib/distutils/command/bdist_wininst.py index fde56754e8916e..1cf2e963e0e731 100644 --- a/Lib/distutils/command/bdist_wininst.py +++ b/Lib/distutils/command/bdist_wininst.py @@ -247,47 +247,49 @@ def create_exe(self, arcname, fullname, bitmap=None): self.announce("creating %s" % installer_name) if bitmap: - bitmapdata = open(bitmap, "rb").read() + with open(bitmap, "rb") as f: + bitmapdata = f.read() bitmaplen = len(bitmapdata) else: bitmaplen = 0 - file = open(installer_name, "wb") - file.write(self.get_exe_bytes()) - if bitmap: - file.write(bitmapdata) - - # Convert cfgdata from unicode to ascii, mbcs encoded - if isinstance(cfgdata, str): - cfgdata = cfgdata.encode("mbcs") - - # 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 + with open(installer_name, "wb") as file: + file.write(self.get_exe_bytes()) + if bitmap: + file.write(bitmapdata) + + # Convert cfgdata from unicode to ascii, mbcs encoded + if isinstance(cfgdata, str): + cfgdata = cfgdata.encode("mbcs") + + # Append the pre-install script cfgdata = cfgdata + b"\0" - file.write(cfgdata) - - # 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("\n" % header) - file.write("\n") - file.write(body) - if body[-1] != "\n": - file.write("\n") - file.close() + with open(filename, "w") as file: + if headers: + for header in headers: + file.write("#include <%s>\n" % header) + file.write("\n") + file.write(body) + if body[-1] != "\n": + file.write("\n") return filename def _preprocess(self, body, headers, include_dirs, lang): @@ -203,17 +202,16 @@ def search_cpp(self, pattern, body=None, headers=None, include_dirs=None, if isinstance(pattern, str): pattern = re.compile(pattern) - file = open(out) - match = False - while True: - line = file.readline() - if line == '': - break - if pattern.search(line): - match = True - break + with open(out) as file: + match = False + while True: + line = file.readline() + if line == '': + break + if pattern.search(line): + match = True + break - file.close() self._clean() return match diff --git a/Lib/distutils/command/sdist.py b/Lib/distutils/command/sdist.py index 52eaa15d4712c3..b4996fcb1d276c 100644 --- a/Lib/distutils/command/sdist.py +++ b/Lib/distutils/command/sdist.py @@ -407,14 +407,13 @@ def read_manifest(self): distribution. """ log.info("reading manifest file '%s'", self.manifest) - manifest = open(self.manifest) - for line in manifest: - # ignore comments and blank lines - line = line.strip() - if line.startswith('#') or not line: - continue - self.filelist.append(line) - manifest.close() + with open(self.manifest) as manifest: + for line in manifest: + # ignore comments and blank lines + line = line.strip() + if line.startswith('#') or not line: + continue + self.filelist.append(line) def make_release_tree(self, base_dir, files): """Create the directory tree that will become the source diff --git a/Lib/distutils/command/upload.py b/Lib/distutils/command/upload.py index 32dda359badb32..9332cc35d36c43 100644 --- a/Lib/distutils/command/upload.py +++ b/Lib/distutils/command/upload.py @@ -131,8 +131,9 @@ def upload_file(self, command, pyversion, filename): data['comment'] = comment if self.sign: - data['gpg_signature'] = (os.path.basename(filename) + ".asc", - open(filename+".asc", "rb").read()) + with open(filename + ".asc", "rb") as f: + data['gpg_signature'] = (os.path.basename(filename) + ".asc", + f.read()) # set up the authentication user_pass = (self.username + ":" + self.password).encode('ascii') diff --git a/Misc/NEWS.d/next/Library/2018-12-05-09-55-05.bpo-35416.XALKZG.rst b/Misc/NEWS.d/next/Library/2018-12-05-09-55-05.bpo-35416.XALKZG.rst new file mode 100644 index 00000000000000..66603bcd31adf3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-05-09-55-05.bpo-35416.XALKZG.rst @@ -0,0 +1 @@ +Fix potential resource warnings in distutils. Patch by Mickaƫl Schoentgen.