On Jul 18, 2012, at 3:17 PM, Anand Buddhdev <[email protected]> wrote:

> On 07/07/2012 21:36, Anand Buddhdev wrote:
>> Hello dnspython users and devs,
>> 
>> I'm trying to use the dns.namedict module. If I create a NameDict
>> object, and store zones z1 and z2 in it, then calls to
>> get_deepest_match() for sub.z1 and sub.z2 succeed as I expect.
>> 
>> However, if I call the method for z3, then I expect to get back either
>> None, or dns.name.empty. Instead, I'm getting an exception:
>> 
>>>>> z.get_deepest_match(dns.name.from_text('z3'))
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>>  File "/Library/Python/2.7/site-packages/dns/namedict.py", line 58, in
>> get_deepest_match
>>    v = self[dns.name.empty]
>> KeyError: <DNS name @>
>> 
>> Is this a bug?

It may be a bit surprising but it's not a bug per se.  The namedict doesn't 
come with an entry for dns.name.root or dns.name.empty.  So, if you want 
get_deepest_match() to match something and not raise KeyError, you must have an 
entry for the root of the tree (either dns.name.root if you're working with 
absolute names, or dns.name.empty if you're working with relativized names).

E.g.

import dns.name
import dns.namedict

d = dns.namedict.NameDict()
d[dns.name.from_text('dnspython.org.')] = 'match_dnspython'
d[dns.name.from_text('.')] = 'match_root'

print d.get_deepest_match(dns.name.from_text('a.dnspython.org.'))
print d.get_deepest_match(dns.name.from_text('dnspython.org.'))
print d.get_deepest_match(dns.name.from_text('a.org.'))
print d.get_deepest_match(dns.name.from_text('org.'))
print d.get_deepest_match(dns.name.from_text('.'))

In retrospect I suppose it was a bad choice to call it "get_deepest_match" 
since the "get" suggests that it would return None on no match instead of 
raising KeyError.

/Bob

_______________________________________________
dnspython-users mailing list
[email protected]
http://howl.play-bow.org/mailman/listinfo/dnspython-users

Reply via email to