From: Bob Halley Date: Mon, 6 Nov 2023 13:10:04 +0000 (-0800) Subject: Fix a type issue with dns.zone.from_file() that mypy misses but X-Git-Tag: v2.5.0rc1~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2a681e12bd5229d6f203233582a24572654f9fe;p=thirdparty%2Fdnspython.git Fix a type issue with dns.zone.from_file() that mypy misses but Cython notices. [#998] --- diff --git a/dns/zone.py b/dns/zone.py index 03d6f1a8..ef54d0a2 100644 --- a/dns/zone.py +++ b/dns/zone.py @@ -1166,6 +1166,48 @@ class Transaction(dns.transaction.Transaction): return (absolute, relativize, effective) +def _from_text( + text: Any, + origin: Optional[Union[dns.name.Name, str]] = None, + rdclass: dns.rdataclass.RdataClass = dns.rdataclass.IN, + relativize: bool = True, + zone_factory: Any = Zone, + filename: Optional[str] = None, + allow_include: bool = False, + check_origin: bool = True, + idna_codec: Optional[dns.name.IDNACodec] = None, + allow_directives: Union[bool, Iterable[str]] = True, +) -> Zone: + # See the comments for the public APIs from_text() and from_file() for + # details. + + # 'text' can also be a file, but we don't publish that fact + # since it's an implementation detail. The official file + # interface is from_file(). + + if filename is None: + filename = "" + zone = zone_factory(origin, rdclass, relativize=relativize) + with zone.writer(True) as txn: + tok = dns.tokenizer.Tokenizer(text, filename, idna_codec=idna_codec) + reader = dns.zonefile.Reader( + tok, + rdclass, + txn, + allow_include=allow_include, + allow_directives=allow_directives, + ) + try: + reader.read() + except dns.zonefile.UnknownOrigin: + # for backwards compatibility + raise dns.zone.UnknownOrigin + # Now that we're done reading, do some basic checking of the zone. + if check_origin: + zone.check_origin() + return zone + + def from_text( text: str, origin: Optional[Union[dns.name.Name, str]] = None, @@ -1226,32 +1268,18 @@ def from_text( Returns a subclass of ``dns.zone.Zone``. """ - - # 'text' can also be a file, but we don't publish that fact - # since it's an implementation detail. The official file - # interface is from_file(). - - if filename is None: - filename = "" - zone = zone_factory(origin, rdclass, relativize=relativize) - with zone.writer(True) as txn: - tok = dns.tokenizer.Tokenizer(text, filename, idna_codec=idna_codec) - reader = dns.zonefile.Reader( - tok, - rdclass, - txn, - allow_include=allow_include, - allow_directives=allow_directives, - ) - try: - reader.read() - except dns.zonefile.UnknownOrigin: - # for backwards compatibility - raise dns.zone.UnknownOrigin - # Now that we're done reading, do some basic checking of the zone. - if check_origin: - zone.check_origin() - return zone + return _from_text( + text, + origin, + rdclass, + relativize, + zone_factory, + filename, + allow_include, + check_origin, + idna_codec, + allow_directives, + ) def from_file( @@ -1322,7 +1350,7 @@ def from_file( else: cm = contextlib.nullcontext(f) with cm as f: - return from_text( + return _from_text( f, origin, rdclass,