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_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
)
) as p:
cmd_output, err = p.communicate()
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_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
)
) as p:
cmd_output, err = p.communicate()
return new_docker_cmd, to_native(cmd_output), err, p.returncode
@ -223,12 +223,11 @@ class Connection(ConnectionBase):
container = self.get_option("remote_addr")
if container in self._container_user_cache:
return self._container_user_cache[container]
p = subprocess.Popen(
with subprocess.Popen(
[self.docker_cmd, "inspect", "--format", "{{.Config.User}}", container],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
) as p:
out, err = p.communicate()
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]
p = subprocess.Popen(
with subprocess.Popen(
local_cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
) as p:
display.debug("done running command with Popen()")
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]
p = subprocess.Popen(
with subprocess.Popen(
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
) as p:
p.communicate()
if getattr(self._shell, "_IS_WINDOWS", False):
@ -582,7 +581,7 @@ class Connection(ConnectionBase):
to_bytes(actual_out_path, errors="surrogate_or_strict"), "wb"
) as out_file:
try:
p = subprocess.Popen(
pp = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=out_file,
@ -592,9 +591,9 @@ class Connection(ConnectionBase):
raise AnsibleError(
"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(
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:
display.debug(f"Unable to open pty: {e}")
p = subprocess.Popen(
with subprocess.Popen(
cmd,
shell=isinstance(cmd, (str, bytes)),
executable=executable if isinstance(cmd, (str, bytes)) else None,
@ -142,8 +142,7 @@ class Connection(ConnectionBase):
stdin=stdin,
stdout=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 master is not None:
os.close(stdin)

View File

@ -72,7 +72,7 @@ class SSHSocket(socket.socket):
env.pop("LD_LIBRARY_PATH", None)
env.pop("SSL_CERT_FILE", None)
self.proc = subprocess.Popen(
self.proc = subprocess.Popen( # pylint: disable=consider-using-with
args,
env=env,
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):
f = tempfile.NamedTemporaryFile()
t = tarfile.open(mode="w", fileobj=f)
f = tempfile.NamedTemporaryFile() # pylint: disable=consider-using-with
try:
with tarfile.open(mode="w", fileobj=f) as t:
if isinstance(dockerfile, io.StringIO):
raise TypeError("Please use io.BytesIO to create in-memory Dockerfiles")
elif isinstance(dockerfile, io.BytesIO):
@ -136,8 +137,10 @@ def mkbuildcontext(dockerfile):
else:
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname="Dockerfile")
t.addfile(dfinfo, dockerfile)
t.close()
f.seek(0)
except Exception: # noqa: E722
f.close()
raise
return f

View File

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

View File

@ -205,9 +205,8 @@ class ParseEnvFileTest(unittest.TestCase):
of 'file_content' and returns the filename.
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.close()
return local_tempfile.name
def test_parse_env_file_proper(self):

View File

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