From dce03b9300381625b1d979f53dc706c109ba632a Mon Sep 17 00:00:00 2001 From: Petr Spacek Date: Thu, 19 Mar 2015 18:02:08 +0100 Subject: [PATCH] Return timeout duration as part of str(Timeout). --- dns/exception.py | 2 ++ dns/resolver.py | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dns/exception.py b/dns/exception.py index 4f7df346..cbcdb57c 100644 --- a/dns/exception.py +++ b/dns/exception.py @@ -110,3 +110,5 @@ class TooBig(DNSException): class Timeout(DNSException): """The DNS operation timed out.""" + supp_kwargs = set(['timeout']) + fmt = "%s after {timeout} seconds" % __doc__[:-1] diff --git a/dns/resolver.py b/dns/resolver.py index 0e400229..6da630e4 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -744,18 +744,18 @@ class Resolver(object): def _compute_timeout(self, start): now = time.time() - if now < start: - if start - now > 1: + duration = now - start + if duration < 0: + if duration < -1: # Time going backwards is bad. Just give up. - raise Timeout + raise Timeout(timeout=duration) else: # Time went backwards, but only a little. This can # happen, e.g. under vmware with older linux kernels. # Pretend it didn't happen. now = start - duration = now - start if duration >= self.lifetime: - raise Timeout + raise Timeout(timeout=duration) return min(self.lifetime - duration, self.timeout) def query(self, qname, rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN, -- 2.47.3