]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Fix resolve() NoAnswer problems from [Issue #488]
authorBob Halley <halley@dnspython.org>
Thu, 28 May 2020 13:03:54 +0000 (06:03 -0700)
committerBob Halley <halley@dnspython.org>
Thu, 28 May 2020 13:04:04 +0000 (06:04 -0700)
dns/resolver.py
dns/trio/resolver.py

index 20356685f0cd62e2f4dcb1e99b4ebaae37632a74..f9dbd641ca317415bcf218f9136a2849cb7e9cee 100644 (file)
@@ -1056,7 +1056,11 @@ class Resolver(object):
         start = time.time()
         while True:
             (request, answer) = resolution.next_request()
-            if answer:
+            # Note we need to say "if answer is not None" and not just
+            # "if answer" because answer implements __len__, and python
+            # will call that.  We want to return if we have an answer
+            # object, including in cases where its length is 0.
+            if answer is not None:
                 # cache hit!
                 return answer
             done = False
@@ -1088,11 +1092,15 @@ class Resolver(object):
                                                        timeout=timeout)
                         elif protocol:
                             continue
-                    (answer, done) = resolution.query_result(response, None)
-                    if answer:
-                        return answer
                 except Exception as ex:
                     (_, done) = resolution.query_result(None, ex)
+                (answer, done) = resolution.query_result(response, None)
+                # Note we need to say "if answer is not None" and not just
+                # "if answer" because answer implements __len__, and python
+                # will call that.  We want to return if we have an answer
+                # object, including in cases where its length is 0.
+                if answer is not None:
+                    return answer
 
     def query(self, qname, rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN,
               tcp=False, source=None, raise_on_no_answer=True, source_port=0,
index f9cdfeacc0176f3264a37ff6ef5443a482183735..4551857c12d7231c31684f880a4dffd12d268f4c 100644 (file)
@@ -89,7 +89,11 @@ class Resolver(dns.resolver.Resolver):
                                               raise_on_no_answer, search)
         while True:
             (request, answer) = resolution.next_request()
-            if answer:
+            # Note we need to say "if answer is not None" and not just
+            # "if answer" because answer implements __len__, and python
+            # will call that.  We want to return if we have an answer
+            # object, including in cases where its length is 0.
+            if answer is not None:
                 # cache hit!
                 return answer
             loops = 1
@@ -121,11 +125,15 @@ class Resolver(dns.resolver.Resolver):
                         else:
                             # We don't do DoH yet.
                             raise NotImplementedError
-                    (answer, done) = resolution.query_result(response, None)
-                    if answer:
-                        return answer
                 except Exception as ex:
                     (_, done) = resolution.query_result(None, ex)
+                (answer, done) = resolution.query_result(response, None)
+                # Note we need to say "if answer is not None" and not just
+                # "if answer" because answer implements __len__, and python
+                # will call that.  We want to return if we have an answer
+                # object, including in cases where its length is 0.
+                if answer is not None:
+                    return answer
 
     async def query(self, *args, **kwargs):
         # We have to define something here as we don't want to inherit the