packaging: use with open() instead of try/finally: close
This commit is contained in:
parent
0e3f3a7076
commit
21a9c748aa
|
@ -110,7 +110,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)
|
||||||
|
@ -118,7 +118,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):
|
||||||
|
@ -207,7 +206,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()
|
||||||
|
@ -217,7 +216,6 @@ class config(Command):
|
||||||
match = True
|
match = True
|
||||||
break
|
break
|
||||||
|
|
||||||
file.close()
|
|
||||||
self._clean()
|
self._clean()
|
||||||
return match
|
return match
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,13 @@ from packaging.command.cmd import Command
|
||||||
def zip_dir(directory):
|
def zip_dir(directory):
|
||||||
"""Compresses recursively contents of directory into a BytesIO object"""
|
"""Compresses recursively contents of directory into a BytesIO object"""
|
||||||
destination = BytesIO()
|
destination = BytesIO()
|
||||||
zip_file = zipfile.ZipFile(destination, "w")
|
with zipfile.ZipFile(destination, "w") as zip_file:
|
||||||
for root, dirs, files in os.walk(directory):
|
for root, dirs, files in os.walk(directory):
|
||||||
for name in files:
|
for name in files:
|
||||||
full = os.path.join(root, name)
|
full = os.path.join(root, name)
|
||||||
relative = root[len(directory):].lstrip(os.path.sep)
|
relative = root[len(directory):].lstrip(os.path.sep)
|
||||||
dest = os.path.join(relative, name)
|
dest = os.path.join(relative, name)
|
||||||
zip_file.write(full, dest)
|
zip_file.write(full, dest)
|
||||||
zip_file.close()
|
|
||||||
return destination
|
return destination
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -728,8 +728,7 @@ class CCompiler:
|
||||||
if library_dirs is None:
|
if library_dirs is None:
|
||||||
library_dirs = []
|
library_dirs = []
|
||||||
fd, fname = tempfile.mkstemp(".c", funcname, text=True)
|
fd, fname = tempfile.mkstemp(".c", funcname, text=True)
|
||||||
f = os.fdopen(fd, "w")
|
with os.fdopen(fd, "w") as f:
|
||||||
try:
|
|
||||||
for incl in includes:
|
for incl in includes:
|
||||||
f.write("""#include "%s"\n""" % incl)
|
f.write("""#include "%s"\n""" % incl)
|
||||||
f.write("""\
|
f.write("""\
|
||||||
|
@ -737,8 +736,6 @@ main (int argc, char **argv) {
|
||||||
%s();
|
%s();
|
||||||
}
|
}
|
||||||
""" % funcname)
|
""" % funcname)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
try:
|
try:
|
||||||
objects = self.compile([fname], include_dirs=include_dirs)
|
objects = self.compile([fname], include_dirs=include_dirs)
|
||||||
except CompileError:
|
except CompileError:
|
||||||
|
|
|
@ -146,11 +146,8 @@ class TempdirManager:
|
||||||
"""
|
"""
|
||||||
if isinstance(path, (list, tuple)):
|
if isinstance(path, (list, tuple)):
|
||||||
path = os.path.join(*path)
|
path = os.path.join(*path)
|
||||||
f = open(path, 'w')
|
with open(path, 'w') as f:
|
||||||
try:
|
|
||||||
f.write(content)
|
f.write(content)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def create_dist(self, **kw):
|
def create_dist(self, **kw):
|
||||||
"""Create a stub distribution object and files.
|
"""Create a stub distribution object and files.
|
||||||
|
|
|
@ -71,11 +71,8 @@ class BuildScriptsTestCase(support.TempdirManager,
|
||||||
return expected
|
return expected
|
||||||
|
|
||||||
def write_script(self, dir, name, text):
|
def write_script(self, dir, name, text):
|
||||||
f = open(os.path.join(dir, name), "w")
|
with open(os.path.join(dir, name), "w") as f:
|
||||||
try:
|
|
||||||
f.write(text)
|
f.write(text)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_version_int(self):
|
def test_version_int(self):
|
||||||
source = self.mkdtemp()
|
source = self.mkdtemp()
|
||||||
|
|
|
@ -193,11 +193,8 @@ class InstallTestCase(support.TempdirManager,
|
||||||
# let's check the record file was created with four
|
# let's check the record file was created with four
|
||||||
# lines, one for each .dist-info entry: METADATA,
|
# lines, one for each .dist-info entry: METADATA,
|
||||||
# INSTALLER, REQUSTED, RECORD
|
# INSTALLER, REQUSTED, RECORD
|
||||||
f = open(cmd.record)
|
with open(cmd.record) as f:
|
||||||
try:
|
|
||||||
self.assertEqual(len(f.readlines()), 4)
|
self.assertEqual(len(f.readlines()), 4)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
# XXX test that fancy_getopt is okay with options named
|
# XXX test that fancy_getopt is okay with options named
|
||||||
# record and no-record but unrelated
|
# record and no-record but unrelated
|
||||||
|
|
|
@ -38,11 +38,8 @@ class InstallScriptsTestCase(support.TempdirManager,
|
||||||
|
|
||||||
def write_script(name, text):
|
def write_script(name, text):
|
||||||
expected.append(name)
|
expected.append(name)
|
||||||
f = open(os.path.join(source, name), "w")
|
with open(os.path.join(source, name), "w") as f:
|
||||||
try:
|
|
||||||
f.write(text)
|
f.write(text)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
write_script("script1.py", ("#! /usr/bin/env python2.3\n"
|
write_script("script1.py", ("#! /usr/bin/env python2.3\n"
|
||||||
"# bogus script w/ Python sh-bang\n"
|
"# bogus script w/ Python sh-bang\n"
|
||||||
|
|
|
@ -173,11 +173,8 @@ class CreateTestCase(support.TempdirManager,
|
||||||
dedent("""
|
dedent("""
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
fp = open('README.txt')
|
with open('README.txt') as fp:
|
||||||
try:
|
|
||||||
long_description = fp.read()
|
long_description = fp.read()
|
||||||
finally:
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
setup(name='pyxfoil',
|
setup(name='pyxfoil',
|
||||||
version='0.2',
|
version='0.2',
|
||||||
|
|
|
@ -54,11 +54,9 @@ class PyPIServerTest(unittest.TestCase):
|
||||||
url = server.full_address + url_path
|
url = server.full_address + url_path
|
||||||
request = urllib.request.Request(url)
|
request = urllib.request.Request(url)
|
||||||
response = urllib.request.urlopen(request)
|
response = urllib.request.urlopen(request)
|
||||||
file = open(PYPI_DEFAULT_STATIC_PATH + "/test_pypi_server" +
|
with open(PYPI_DEFAULT_STATIC_PATH + "/test_pypi_server"
|
||||||
url_path)
|
+ url_path) as file:
|
||||||
answer = response.read().decode() == file.read()
|
return response.read().decode() == file.read()
|
||||||
file.close()
|
|
||||||
return answer
|
|
||||||
|
|
||||||
server = PyPIServer(static_uri_paths=["simple", "external"],
|
server = PyPIServer(static_uri_paths=["simple", "external"],
|
||||||
static_filesystem_paths=["test_pypi_server"])
|
static_filesystem_paths=["test_pypi_server"])
|
||||||
|
|
|
@ -720,17 +720,15 @@ class EggInfoToDistInfoTestCase(support.TempdirManager,
|
||||||
dir_paths.append(path)
|
dir_paths.append(path)
|
||||||
for f in files:
|
for f in files:
|
||||||
path = os.path.join(tempdir, f)
|
path = os.path.join(tempdir, f)
|
||||||
_f = open(path, 'w')
|
with open(path, 'w') as _f:
|
||||||
_f.write(f)
|
_f.write(f)
|
||||||
_f.close()
|
|
||||||
file_paths.append(path)
|
file_paths.append(path)
|
||||||
|
|
||||||
record_file = open(record_file_path, 'w')
|
with open(record_file_path, 'w') as record_file:
|
||||||
for fpath in file_paths:
|
for fpath in file_paths:
|
||||||
record_file.write(fpath + '\n')
|
record_file.write(fpath + '\n')
|
||||||
for dpath in dir_paths:
|
for dpath in dir_paths:
|
||||||
record_file.write(dpath + '\n')
|
record_file.write(dpath + '\n')
|
||||||
record_file.close()
|
|
||||||
|
|
||||||
return (tempdir, record_file_path)
|
return (tempdir, record_file_path)
|
||||||
|
|
||||||
|
|
|
@ -350,7 +350,7 @@ def byte_compile(py_files, optimize=0, force=False, prefix=None,
|
||||||
else:
|
else:
|
||||||
script = open(script_name, "w")
|
script = open(script_name, "w")
|
||||||
|
|
||||||
try:
|
with script:
|
||||||
script.write("""\
|
script.write("""\
|
||||||
from packaging.util import byte_compile
|
from packaging.util import byte_compile
|
||||||
files = [
|
files = [
|
||||||
|
@ -378,9 +378,6 @@ byte_compile(files, optimize=%r, force=%r,
|
||||||
direct=True)
|
direct=True)
|
||||||
""" % (optimize, force, prefix, base_dir, verbose))
|
""" % (optimize, force, prefix, base_dir, verbose))
|
||||||
|
|
||||||
finally:
|
|
||||||
script.close()
|
|
||||||
|
|
||||||
cmd = [sys.executable, script_name]
|
cmd = [sys.executable, script_name]
|
||||||
if optimize == 1:
|
if optimize == 1:
|
||||||
cmd.insert(1, "-O")
|
cmd.insert(1, "-O")
|
||||||
|
|
Loading…
Reference in New Issue