Address some consider-using-with.

This commit is contained in:
Felix Fontein 2025-10-11 14:42:42 +02:00
parent 9bb3f8c3b3
commit c7399b7c38
7 changed files with 244 additions and 248 deletions

View File

@ -182,9 +182,9 @@ class Connection(ConnectionBase):
old_version_subcommand = ["version"] old_version_subcommand = ["version"]
old_docker_cmd = [self.docker_cmd] + cmd_args + old_version_subcommand old_docker_cmd = [self.docker_cmd] + cmd_args + old_version_subcommand
p = subprocess.Popen( with subprocess.Popen(
old_docker_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE old_docker_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) ) as p:
cmd_output, err = p.communicate() cmd_output, err = p.communicate()
return old_docker_cmd, to_native(cmd_output), err, p.returncode return old_docker_cmd, to_native(cmd_output), err, p.returncode
@ -196,9 +196,9 @@ class Connection(ConnectionBase):
new_version_subcommand = ["version", "--format", "'{{.Server.Version}}'"] new_version_subcommand = ["version", "--format", "'{{.Server.Version}}'"]
new_docker_cmd = [self.docker_cmd] + cmd_args + new_version_subcommand new_docker_cmd = [self.docker_cmd] + cmd_args + new_version_subcommand
p = subprocess.Popen( with subprocess.Popen(
new_docker_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE new_docker_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) ) as p:
cmd_output, err = p.communicate() cmd_output, err = p.communicate()
return new_docker_cmd, to_native(cmd_output), err, p.returncode return new_docker_cmd, to_native(cmd_output), err, p.returncode
@ -223,12 +223,11 @@ class Connection(ConnectionBase):
container = self.get_option("remote_addr") container = self.get_option("remote_addr")
if container in self._container_user_cache: if container in self._container_user_cache:
return self._container_user_cache[container] return self._container_user_cache[container]
p = subprocess.Popen( with subprocess.Popen(
[self.docker_cmd, "inspect", "--format", "{{.Config.User}}", container], [self.docker_cmd, "inspect", "--format", "{{.Config.User}}", container],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) ) as p:
out, err = p.communicate() out, err = p.communicate()
out = to_text(out, errors="surrogate_or_strict") out = to_text(out, errors="surrogate_or_strict")
@ -392,12 +391,12 @@ class Connection(ConnectionBase):
local_cmd = [to_bytes(i, errors="surrogate_or_strict") for i in local_cmd] local_cmd = [to_bytes(i, errors="surrogate_or_strict") for i in local_cmd]
p = subprocess.Popen( with subprocess.Popen(
local_cmd, local_cmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) ) as p:
display.debug("done running command with Popen()") display.debug("done running command with Popen()")
if self.become and self.become.expect_prompt() and sudoable: if self.become and self.become.expect_prompt() and sudoable:
@ -559,9 +558,9 @@ class Connection(ConnectionBase):
] ]
args = [to_bytes(i, errors="surrogate_or_strict") for i in args] args = [to_bytes(i, errors="surrogate_or_strict") for i in args]
p = subprocess.Popen( with subprocess.Popen(
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) ) as p:
p.communicate() p.communicate()
if getattr(self._shell, "_IS_WINDOWS", False): if getattr(self._shell, "_IS_WINDOWS", False):
@ -582,7 +581,7 @@ class Connection(ConnectionBase):
to_bytes(actual_out_path, errors="surrogate_or_strict"), "wb" to_bytes(actual_out_path, errors="surrogate_or_strict"), "wb"
) as out_file: ) as out_file:
try: try:
p = subprocess.Popen( pp = subprocess.Popen(
args, args,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=out_file, stdout=out_file,
@ -592,9 +591,9 @@ class Connection(ConnectionBase):
raise AnsibleError( raise AnsibleError(
"docker connection requires dd command in the container to put files" "docker connection requires dd command in the container to put files"
) )
stdout, stderr = p.communicate() stdout, stderr = pp.communicate()
if p.returncode != 0: if pp.returncode != 0:
raise AnsibleError( raise AnsibleError(
f"failed to fetch file {in_path} to {out_path}:\n{stdout}\n{stderr}" f"failed to fetch file {in_path} to {out_path}:\n{stdout}\n{stderr}"
) )

View File

@ -134,7 +134,7 @@ class Connection(ConnectionBase):
except (IOError, OSError) as e: except (IOError, OSError) as e:
display.debug(f"Unable to open pty: {e}") display.debug(f"Unable to open pty: {e}")
p = subprocess.Popen( with subprocess.Popen(
cmd, cmd,
shell=isinstance(cmd, (str, bytes)), shell=isinstance(cmd, (str, bytes)),
executable=executable if isinstance(cmd, (str, bytes)) else None, executable=executable if isinstance(cmd, (str, bytes)) else None,
@ -142,8 +142,7 @@ class Connection(ConnectionBase):
stdin=stdin, stdin=stdin,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) ) as p:
# if we created a master, we can close the other half of the pty now, otherwise master is stdin # if we created a master, we can close the other half of the pty now, otherwise master is stdin
if master is not None: if master is not None:
os.close(stdin) os.close(stdin)

View File

@ -72,7 +72,7 @@ class SSHSocket(socket.socket):
env.pop("LD_LIBRARY_PATH", None) env.pop("LD_LIBRARY_PATH", None)
env.pop("SSL_CERT_FILE", None) env.pop("SSL_CERT_FILE", None)
self.proc = subprocess.Popen( self.proc = subprocess.Popen( # pylint: disable=consider-using-with
args, args,
env=env, env=env,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,

View File

@ -125,8 +125,9 @@ def create_archive(root, files=None, fileobj=None, gzip=False, extra_files=None)
def mkbuildcontext(dockerfile): def mkbuildcontext(dockerfile):
f = tempfile.NamedTemporaryFile() f = tempfile.NamedTemporaryFile() # pylint: disable=consider-using-with
t = tarfile.open(mode="w", fileobj=f) try:
with tarfile.open(mode="w", fileobj=f) as t:
if isinstance(dockerfile, io.StringIO): if isinstance(dockerfile, io.StringIO):
raise TypeError("Please use io.BytesIO to create in-memory Dockerfiles") raise TypeError("Please use io.BytesIO to create in-memory Dockerfiles")
elif isinstance(dockerfile, io.BytesIO): elif isinstance(dockerfile, io.BytesIO):
@ -136,8 +137,10 @@ def mkbuildcontext(dockerfile):
else: else:
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname="Dockerfile") dfinfo = t.gettarinfo(fileobj=dockerfile, arcname="Dockerfile")
t.addfile(dfinfo, dockerfile) t.addfile(dfinfo, dockerfile)
t.close()
f.seek(0) f.seek(0)
except Exception: # noqa: E722
f.close()
raise
return f return f

View File

@ -417,7 +417,7 @@ class TarTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, base) self.addCleanup(shutil.rmtree, base)
with tar(base, exclude=exclude) as archive: with tar(base, exclude=exclude) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert sorted(tar_data.getnames()) == sorted(expected_names) assert sorted(tar_data.getnames()) == sorted(expected_names)
def test_tar_with_empty_directory(self): def test_tar_with_empty_directory(self):
@ -426,7 +426,7 @@ class TarTest(unittest.TestCase):
for d in ["foo", "bar"]: for d in ["foo", "bar"]:
os.makedirs(os.path.join(base, d)) os.makedirs(os.path.join(base, d))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert sorted(tar_data.getnames()) == ["bar", "foo"] assert sorted(tar_data.getnames()) == ["bar", "foo"]
@pytest.mark.skipif( @pytest.mark.skipif(
@ -454,7 +454,7 @@ class TarTest(unittest.TestCase):
os.makedirs(os.path.join(base, "bar")) os.makedirs(os.path.join(base, "bar"))
os.symlink("../foo", os.path.join(base, "bar/foo")) os.symlink("../foo", os.path.join(base, "bar/foo"))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"] assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows") @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows")
@ -465,7 +465,7 @@ class TarTest(unittest.TestCase):
os.makedirs(os.path.join(base, d)) os.makedirs(os.path.join(base, d))
os.symlink("../foo", os.path.join(base, "bar/foo")) os.symlink("../foo", os.path.join(base, "bar/foo"))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"] assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows") @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No symlinks on Windows")
@ -477,7 +477,7 @@ class TarTest(unittest.TestCase):
os.symlink("../baz", os.path.join(base, "bar/foo")) os.symlink("../baz", os.path.join(base, "bar/foo"))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"] assert sorted(tar_data.getnames()) == ["bar", "bar/foo", "foo"]
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No UNIX sockets on Win32") @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason="No UNIX sockets on Win32")
@ -490,7 +490,7 @@ class TarTest(unittest.TestCase):
self.addCleanup(sock.close) self.addCleanup(sock.close)
sock.bind(os.path.join(base, "test.sock")) sock.bind(os.path.join(base, "test.sock"))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert sorted(tar_data.getnames()) == ["bar", "foo"] assert sorted(tar_data.getnames()) == ["bar", "foo"]
def tar_test_negative_mtime_bug(self): def tar_test_negative_mtime_bug(self):
@ -501,7 +501,7 @@ class TarTest(unittest.TestCase):
f.write("Invisible Full Moon") f.write("Invisible Full Moon")
os.utime(filename, (12345, -3600.0)) os.utime(filename, (12345, -3600.0))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
assert tar_data.getnames() == ["th.txt"] assert tar_data.getnames() == ["th.txt"]
assert tar_data.getmember("th.txt").mtime == -3600 assert tar_data.getmember("th.txt").mtime == -3600
@ -513,7 +513,7 @@ class TarTest(unittest.TestCase):
self.addCleanup(shutil.rmtree, base) self.addCleanup(shutil.rmtree, base)
os.symlink(os.path.join(base, "b"), os.path.join(base, "a/c/b")) os.symlink(os.path.join(base, "b"), os.path.join(base, "a/c/b"))
with tar(base) as archive: with tar(base) as archive:
tar_data = tarfile.open(fileobj=archive) with tarfile.open(fileobj=archive) as tar_data:
names = tar_data.getnames() names = tar_data.getnames()
for member in dirs + files: for member in dirs + files:
assert member in names assert member in names

View File

@ -205,9 +205,8 @@ class ParseEnvFileTest(unittest.TestCase):
of 'file_content' and returns the filename. of 'file_content' and returns the filename.
Don't forget to unlink the file with os.unlink() after. Don't forget to unlink the file with os.unlink() after.
""" """
local_tempfile = tempfile.NamedTemporaryFile(delete=False) with tempfile.NamedTemporaryFile(delete=False) as local_tempfile:
local_tempfile.write(file_content.encode("UTF-8")) local_tempfile.write(file_content.encode("UTF-8"))
local_tempfile.close()
return local_tempfile.name return local_tempfile.name
def test_parse_env_file_proper(self): def test_parse_env_file_proper(self):

View File

@ -50,8 +50,7 @@ def write_irrelevant_tar(file_name):
:type file_name: str :type file_name: str
""" """
tf = tarfile.open(file_name, "w") with tarfile.open(file_name, "w") as tf:
try:
with TemporaryFile() as f: with TemporaryFile() as f:
f.write("Hello, world.".encode("utf-8")) f.write("Hello, world.".encode("utf-8"))
@ -60,6 +59,3 @@ def write_irrelevant_tar(file_name):
f.seek(0) f.seek(0)
tf.addfile(ti, f) tf.addfile(ti, f)
finally:
tf.close()