Commit da302452d0 for bind
commit da302452d03daf9be0b5a33cbf3d125b763d6852
Author: Michal Nowak <mnowak@isc.org>
Date: Fri Oct 9 15:47:56 2026 +0000
Merge nested with statements into parenthesized context managers
Python 3.10 allows several context managers in one parenthesized with
statement, as tests_formerr.py already does. Use it where two context
managers were nested only because one depends on the other, saving a
level of indentation.
Assisted-by: Claude:claude-opus-5-5
diff --git a/bin/tests/system/shutdown/tests_shutdown.py b/bin/tests/system/shutdown/tests_shutdown.py
index c5bff8e04e..d237b6b520 100644
--- a/bin/tests/system/shutdown/tests_shutdown.py
+++ b/bin/tests/system/shutdown/tests_shutdown.py
@@ -176,24 +176,24 @@ def test_named_shutdown(kill_method):
named_cmdline = isctest.run.get_named_cmdline(cfg_dir)
instance = isctest.instance.NamedInstance("resolver", num=3)
- with open(os.path.join(cfg_dir, "named.run"), "ab") as named_log:
- with subprocess.Popen(
- named_cmdline, cwd=cfg_dir, stderr=named_log
- ) as named_proc:
- try:
- isctest.check.named_alive(named_proc, resolver_ip)
- do_work(
- named_proc,
- resolver_ip,
- instance,
- kill_method,
- n_workers=12,
- n_queries=16,
- )
- assert wait_for_proc_termination(named_proc)
- assert named_proc.returncode == 0, "named crashed"
- finally: # Ensure named is terminated in case of an exception
- named_proc.kill()
+ with (
+ open(os.path.join(cfg_dir, "named.run"), "ab") as named_log,
+ subprocess.Popen(named_cmdline, cwd=cfg_dir, stderr=named_log) as named_proc,
+ ):
+ try:
+ isctest.check.named_alive(named_proc, resolver_ip)
+ do_work(
+ named_proc,
+ resolver_ip,
+ instance,
+ kill_method,
+ n_workers=12,
+ n_queries=16,
+ )
+ assert wait_for_proc_termination(named_proc)
+ assert named_proc.returncode == 0, "named crashed"
+ finally: # Ensure named is terminated in case of an exception
+ named_proc.kill()
@pytest.mark.parametrize("retired_view", [False, True])
@@ -219,50 +219,51 @@ def test_shutdown_pending_forward(retired_view, kill_method, templates, named_po
result += chunk
return result
- with socket.socket() as primary, socket.socket(
- socket.AF_INET, socket.SOCK_DGRAM
- ) as client:
+ with (
+ socket.socket() as primary,
+ socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as client,
+ ):
primary.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
primary.bind(("10.53.0.4", named_port))
primary.listen()
primary.settimeout(10)
client.settimeout(0.2)
- with open(f"{cfg_dir}/named.run", "ab") as log:
- with subprocess.Popen(
+ with (
+ open(f"{cfg_dir}/named.run", "ab") as log,
+ subprocess.Popen(
isctest.run.get_named_cmdline(cfg_dir), cwd=cfg_dir, stderr=log
- ) as proc:
- try:
- isctest.check.named_alive(proc, "10.53.0.3")
- update = dns.update.Update("forward.test")
- update.add("pending", 300, "A", "192.0.2.1")
- client.sendto(update.to_wire(), ("10.53.0.3", named_port))
- connection, _ = primary.accept()
- with connection:
- connection.settimeout(10)
- length = struct.unpack("!H", receive_exact(connection, 2))[0]
- forwarded = dns.message.from_wire(
- receive_exact(connection, length)
+ ) as proc,
+ ):
+ try:
+ isctest.check.named_alive(proc, "10.53.0.3")
+ update = dns.update.Update("forward.test")
+ update.add("pending", 300, "A", "192.0.2.1")
+ client.sendto(update.to_wire(), ("10.53.0.3", named_port))
+ connection, _ = primary.accept()
+ with connection:
+ connection.settimeout(10)
+ length = struct.unpack("!H", receive_exact(connection, 2))[0]
+ forwarded = dns.message.from_wire(receive_exact(connection, length))
+ assert forwarded.opcode() == dns.opcode.UPDATE
+ assert forwarded.question == update.question
+ if retired_view:
+ templates.render(f"{cfg_dir}/named.conf", {"retired": True})
+ instance.rndc("reconfig")
+ # Prove the old view is no longer in the active config.
+ result = instance.rndc(
+ "showzone forward.test IN original",
+ raise_on_exception=False,
)
- assert forwarded.opcode() == dns.opcode.UPDATE
- assert forwarded.question == update.question
- if retired_view:
- templates.render(f"{cfg_dir}/named.conf", {"retired": True})
- instance.rndc("reconfig")
- # Prove the old view is no longer in the active config.
- result = instance.rndc(
- "showzone forward.test IN original",
- raise_on_exception=False,
- )
- assert result.rc != 0
- # The operation must still be outstanding at shutdown.
- with pytest.raises(socket.timeout):
- client.recv(65535)
- if kill_method == "rndc":
- instance.rndc("stop")
- else:
- proc.terminate()
- assert proc.wait(timeout=10) == 0
- finally:
- if proc.poll() is None:
- proc.kill()
- proc.wait()
+ assert result.rc != 0
+ # The operation must still be outstanding at shutdown.
+ with pytest.raises(socket.timeout):
+ client.recv(65535)
+ if kill_method == "rndc":
+ instance.rndc("stop")
+ else:
+ proc.terminate()
+ assert proc.wait(timeout=10) == 0
+ finally:
+ if proc.poll() is None:
+ proc.kill()
+ proc.wait()
diff --git a/bin/tests/system/sig0_https/tests_sig0_https.py b/bin/tests/system/sig0_https/tests_sig0_https.py
index 5dae8f9e9f..f7c6147ecd 100644
--- a/bin/tests/system/sig0_https/tests_sig0_https.py
+++ b/bin/tests/system/sig0_https/tests_sig0_https.py
@@ -79,29 +79,31 @@ def test_sig0_doh(ns1, named_httpsport):
ctx = dns.query.make_ssl_context(verify=False, check_hostname=False)
- with socket.create_connection((ns1.ip, named_httpsport)) as sock:
- with ctx.wrap_socket(sock) as conn:
- h2 = H2Connection()
- h2.initiate_connection()
-
- stream = h2.get_next_available_stream_id()
- h2.send_headers(
- stream,
- [
- (":method", "POST"),
- (":scheme", "https"),
- (":authority", f"{ns1.ip}:{named_httpsport}"),
- (":path", "/dns-query"),
- ("content-type", "application/dns-message"),
- ("content-length", str(len(msg))),
- ],
- )
- h2.send_data(stream, msg, end_stream=True)
-
- # Send the query and immediately close the connection.
- # named should gracefully handle the fact the connection is
- # now closed after it checked the message signature.
- conn.sendall(h2.data_to_send())
+ with (
+ socket.create_connection((ns1.ip, named_httpsport)) as sock,
+ ctx.wrap_socket(sock) as conn,
+ ):
+ h2 = H2Connection()
+ h2.initiate_connection()
+
+ stream = h2.get_next_available_stream_id()
+ h2.send_headers(
+ stream,
+ [
+ (":method", "POST"),
+ (":scheme", "https"),
+ (":authority", f"{ns1.ip}:{named_httpsport}"),
+ (":path", "/dns-query"),
+ ("content-type", "application/dns-message"),
+ ("content-length", str(len(msg))),
+ ],
+ )
+ h2.send_data(stream, msg, end_stream=True)
+
+ # Send the query and immediately close the connection.
+ # named should gracefully handle the fact the connection is
+ # now closed after it checked the message signature.
+ conn.sendall(h2.data_to_send())
# If the server succesfully restarted, it means it didn't crash.
with ns1.watch_log_from_here() as watcher:
diff --git a/pyproject.toml b/pyproject.toml
index 501c2a704f..1eb61584f7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -112,6 +112,8 @@ lint.select = [
# f-strings
"UP031",
"UP032",
+ # nested with statements
+ "SIM117",
]
extend-exclude = [
"bin/tests/system/vulture_ignore_list.py",