]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Various small improvements (#1248)
authordarkdragon-001 <darkdragon-001@users.noreply.github.com>
Thu, 1 Jan 2026 21:16:41 +0000 (22:16 +0100)
committerGitHub <noreply@github.com>
Thu, 1 Jan 2026 21:16:41 +0000 (13:16 -0800)
* Use name parameter

This increases clarity and is less error-prone.

* Improve optional error ignore

* Prefix private classes with underscore to avoid confusion

The public interface is the base class in dns._asyncbackend.

* Inherit from asyncio.DatagramProtocol

dns/_asyncio_backend.py
dns/asyncquery.py

index cc683985b5a238e67fb1db1951a5a80ab06873cf..e0c367ed307a355ad0fc998426bb8fdd65c9af0a 100644 (file)
@@ -21,7 +21,7 @@ def _get_running_loop():
         return asyncio.get_event_loop()
 
 
-class _DatagramProtocol:
+class _DatagramProtocol(asyncio.DatagramProtocol):
     def __init__(self):
         self.transport = None
         self.recvfrom = None
@@ -63,7 +63,7 @@ async def _maybe_wait_for(awaitable, timeout):
         return await awaitable
 
 
-class DatagramSocket(dns._asyncbackend.DatagramSocket):
+class _DatagramSocket(dns._asyncbackend.DatagramSocket):
     def __init__(self, family, transport, protocol):
         super().__init__(family, socket.SOCK_DGRAM)
         self.transport = transport
@@ -98,7 +98,7 @@ class DatagramSocket(dns._asyncbackend.DatagramSocket):
         raise NotImplementedError
 
 
-class StreamSocket(dns._asyncbackend.StreamSocket):
+class _StreamSocket(dns._asyncbackend.StreamSocket):
     def __init__(self, af, reader, writer):
         super().__init__(af, socket.SOCK_STREAM)
         self.reader = reader
@@ -235,12 +235,12 @@ class Backend(dns._asyncbackend.Backend):
                 source = (dns.inet.any_for_af(af), 0)
             transport, protocol = await loop.create_datagram_endpoint(
                 _DatagramProtocol,  # type: ignore
-                source,
+                local_addr=source,
                 family=af,
                 proto=proto,
                 remote_addr=destination,
             )
-            return DatagramSocket(af, transport, protocol)
+            return _DatagramSocket(af, transport, protocol)
         elif socktype == socket.SOCK_STREAM:
             if destination is None:
                 # This shouldn't happen, but we check to make code analysis software
@@ -258,7 +258,7 @@ class Backend(dns._asyncbackend.Backend):
                 ),
                 timeout,
             )
-            return StreamSocket(af, r, w)
+            return _StreamSocket(af, r, w)
         raise NotImplementedError(
             "unsupported socket " + f"type {socktype}"
         )  # pragma: no cover
index 7ec7ed8203bdab71c3c8ed3d9617d6c9cc6bb78e..426381a80b6ea843b4b7fb40476150675063e0cd 100644 (file)
@@ -158,6 +158,7 @@ async def receive_udp(
                 one_rr_per_rrset=one_rr_per_rrset,
                 ignore_trailing=ignore_trailing,
                 raise_on_truncation=raise_on_truncation,
+                continue_on_error=ignore_errors,
             )
         except dns.message.Truncated as e:
             # See the comment in query.py for details.
@@ -359,6 +360,7 @@ async def receive_tcp(
     keyring: dict[dns.name.Name, dns.tsig.Key] | None = None,
     request_mac: bytes | None = b"",
     ignore_trailing: bool = False,
+    ignore_errors: bool = False,
 ) -> tuple[dns.message.Message, float]:
     """Read a DNS message from a TCP socket.
 
@@ -378,6 +380,7 @@ async def receive_tcp(
         request_mac=request_mac,
         one_rr_per_rrset=one_rr_per_rrset,
         ignore_trailing=ignore_trailing,
+        continue_on_error=ignore_errors,
     )
     return (r, received_time)