From: Brian Wellington Date: Fri, 9 Feb 2024 02:02:39 +0000 (-0800) Subject: Include the text description of an EDE. (#1042) X-Git-Tag: v2.6.0rc1~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=86dede4fba913190c6b001bf7156393f1bd474b6;p=thirdparty%2Fdnspython.git Include the text description of an EDE. (#1042) * Include the text description of an EDE. For known EDEs, add the description of the code in the output. * Update test. --- diff --git a/dns/edns.py b/dns/edns.py index b29afaa3..776e5eeb 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -345,6 +345,8 @@ class EDECode(dns.enum.IntEnum): class EDEOption(Option): # lgtm[py/missing-equals] """Extended DNS Error (EDE, RFC8914)""" + _preserve_case = {"DNSKEY", "DS", "DNSSEC", "RRSIGs", "NSEC", "NXDOMAIN"} + def __init__(self, code: Union[EDECode, str], text: Optional[str] = None): """*code*, a ``dns.edns.EDECode`` or ``str``, the info code of the extended error. @@ -362,6 +364,13 @@ class EDEOption(Option): # lgtm[py/missing-equals] def to_text(self) -> str: output = f"EDE {self.code}" + if self.code in EDECode: + desc = EDECode.to_text(self.code) + desc = " ".join( + word if word in self._preserve_case else word.title() + for word in desc.split("_") + ) + output += f" ({desc})" if self.text is not None: output += f": {self.text}" return output diff --git a/tests/test_edns.py b/tests/test_edns.py index 98e468ba..367c723c 100644 --- a/tests/test_edns.py +++ b/tests/test_edns.py @@ -141,7 +141,7 @@ class OptionTestCase(unittest.TestCase): opt.to_wire(io) data = io.getvalue() self.assertEqual(data, b"\x00\x03") - self.assertEqual(str(opt), "EDE 3") + self.assertEqual(str(opt), "EDE 3 (Stale Answer)") # with text opt = dns.edns.EDEOption(16, "test") io = BytesIO()