diff options
Diffstat (limited to 'dns')
| -rw-r--r-- | dns/exception.py | 14 | ||||
| -rw-r--r-- | dns/rdata.py | 51 |
2 files changed, 40 insertions, 25 deletions
diff --git a/dns/exception.py b/dns/exception.py index 8f1d488..9486f45 100644 --- a/dns/exception.py +++ b/dns/exception.py @@ -126,3 +126,17 @@ class Timeout(DNSException): """The DNS operation timed out.""" supp_kwargs = {'timeout'} fmt = "The DNS operation timed out after {timeout} seconds" + + +class ExceptionWrapper: + def __init__(self, exception_class): + self.exception_class = exception_class + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type is not None and not isinstance(exc_val, + self.exception_class): + raise self.exception_class() from exc_val + return False diff --git a/dns/rdata.py b/dns/rdata.py index 2d08dcc..0daa08d 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -459,35 +459,35 @@ def from_text(rdclass, rdtype, tok, origin=None, relativize=True, Returns an instance of the chosen Rdata subclass. """ - if isinstance(tok, str): tok = dns.tokenizer.Tokenizer(tok, idna_codec=idna_codec) rdclass = dns.rdataclass.RdataClass.make(rdclass) rdtype = dns.rdatatype.RdataType.make(rdtype) cls = get_rdata_class(rdclass, rdtype) - rdata = None - if cls != GenericRdata: - # peek at first token - token = tok.get() - tok.unget(token) - if token.is_identifier() and \ - token.value == r'\#': - # - # Known type using the generic syntax. Extract the - # wire form from the generic syntax, and then run - # from_wire on it. - # - grdata = GenericRdata.from_text(rdclass, rdtype, tok, origin, - relativize, relativize_to) - rdata = from_wire(rdclass, rdtype, grdata.data, 0, len(grdata.data), - origin) - if rdata is None: - rdata = cls.from_text(rdclass, rdtype, tok, origin, relativize, - relativize_to) - token = tok.get_eol_as_token() - if token.comment is not None: - object.__setattr__(rdata, 'rdcomment', token.comment) - return rdata + with dns.exception.ExceptionWrapper(dns.exception.SyntaxError): + rdata = None + if cls != GenericRdata: + # peek at first token + token = tok.get() + tok.unget(token) + if token.is_identifier() and \ + token.value == r'\#': + # + # Known type using the generic syntax. Extract the + # wire form from the generic syntax, and then run + # from_wire on it. + # + grdata = GenericRdata.from_text(rdclass, rdtype, tok, origin, + relativize, relativize_to) + rdata = from_wire(rdclass, rdtype, grdata.data, 0, + len(grdata.data), origin) + if rdata is None: + rdata = cls.from_text(rdclass, rdtype, tok, origin, relativize, + relativize_to) + token = tok.get_eol_as_token() + if token.comment is not None: + object.__setattr__(rdata, 'rdcomment', token.comment) + return rdata def from_wire_parser(rdclass, rdtype, parser, origin=None): @@ -517,7 +517,8 @@ def from_wire_parser(rdclass, rdtype, parser, origin=None): rdclass = dns.rdataclass.RdataClass.make(rdclass) rdtype = dns.rdatatype.RdataType.make(rdtype) cls = get_rdata_class(rdclass, rdtype) - return cls.from_wire_parser(rdclass, rdtype, parser, origin) + with dns.exception.ExceptionWrapper(dns.exception.FormError): + return cls.from_wire_parser(rdclass, rdtype, parser, origin) def from_wire(rdclass, rdtype, wire, current, rdlen, origin=None): |
