e267d0
From daf723176da7bfee5baae928ff1af310a2400289 Mon Sep 17 00:00:00 2001
e267d0
From: Bob Halley <halley@dnspython.org>
e267d0
Date: Sun, 21 Feb 2016 15:22:21 -0800
e267d0
Subject: [PATCH] Set response_time to zero when exceptions occur early.
e267d0
e267d0
---
e267d0
 dns/query.py | 12 ++++++++++--
e267d0
 1 file changed, 10 insertions(+), 2 deletions(-)
e267d0
e267d0
diff --git a/dns/query.py b/dns/query.py
e267d0
index ad6dffd..4e8089f 100644
e267d0
--- a/dns/query.py
e267d0
+++ b/dns/query.py
e267d0
@@ -204,6 +204,7 @@ def udp(q, where, timeout=None, port=53, af=None, source=None, source_port=0,
e267d0
     (af, destination, source) = _destination_and_source(af, where, port, source,
e267d0
                                                         source_port)
e267d0
     s = socket.socket(af, socket.SOCK_DGRAM, 0)
e267d0
+    begin_time = None
e267d0
     try:
e267d0
         expiration = _compute_expiration(timeout)
e267d0
         s.setblocking(0)
e267d0
@@ -224,7 +225,10 @@ def udp(q, where, timeout=None, port=53, af=None, source=None, source_port=0,
e267d0
                                        '%s instead of %s' % (from_address,
e267d0
                                                              destination))
e267d0
     finally:
e267d0
-        response_time = time.time() - begin_time
e267d0
+        if begin_time is None:
e267d0
+            response_time = 0
e267d0
+        else:
e267d0
+            response_time = time.time() - begin_time
e267d0
         s.close()
e267d0
     r = dns.message.from_wire(wire, keyring=q.keyring, request_mac=q.mac,
e267d0
                               one_rr_per_rrset=one_rr_per_rrset)
e267d0
@@ -301,6 +305,7 @@ def tcp(q, where, timeout=None, port=53, af=None, source=None, source_port=0,
e267d0
     (af, destination, source) = _destination_and_source(af, where, port, source,
e267d0
                                                         source_port)
e267d0
     s = socket.socket(af, socket.SOCK_STREAM, 0)
e267d0
+    begin_time = None
e267d0
     try:
e267d0
         expiration = _compute_expiration(timeout)
e267d0
         s.setblocking(0)
e267d0
@@ -320,7 +325,10 @@ def tcp(q, where, timeout=None, port=53, af=None, source=None, source_port=0,
e267d0
         (l,) = struct.unpack("!H", ldata)
e267d0
         wire = _net_read(s, l, expiration)
e267d0
     finally:
e267d0
-        response_time = time.time() - begin_time
e267d0
+        if begin_time is None:
e267d0
+            response_time = 0
e267d0
+        else:
e267d0
+            response_time = time.time() - begin_time
e267d0
         s.close()
e267d0
     r = dns.message.from_wire(wire, keyring=q.keyring, request_mac=q.mac,
e267d0
                               one_rr_per_rrset=one_rr_per_rrset)