+2005-10-06 Bob Halley <halley@dnspython.org>
+
+ * dns/name.py: Added the parent() method, which returns the
+ parent of a name.
+
2005-10-01 Bob Halley <halley@dnspython.org>
* dns/resolver.py: Added zone_for_name() helper, which returns
empty name to an absolute name."""
pass
+class NoParent(dns.exception.DNSException):
+ """Raised if an attempt is made to get the parent of the root name
+ or the empty name."""
+ pass
+
_escaped = {
'"' : True,
'(' : True,
return self.derelativize(origin)
else:
return self
+
+ def parent(self):
+ """Return the parent of the name.
+ @rtype: dns.name.Name object
+ @raises NoParent: the name is either the root name or the empty name,
+ and thus has no parent.
+ """
+ if self == root or self == empty:
+ raise NoParent
+ return Name(self.labels[1:])
root = Name([''])
empty = Name([])
(n, cused) = dns.name.from_wire(w, 0)
self.failUnlessRaises(dns.name.BadLabelType, bad)
+ def testParent1(self):
+ n = dns.name.from_text('foo.bar.')
+ self.failUnless(n.parent() == dns.name.from_text('bar.'))
+ self.failUnless(n.parent().parent() == dns.name.root)
+
+ def testParent2(self):
+ n = dns.name.from_text('foo.bar', None)
+ self.failUnless(n.parent() == dns.name.from_text('bar', None))
+ self.failUnless(n.parent().parent() == dns.name.empty)
+
+ def testParent3(self):
+ def bad():
+ n = dns.name.root
+ n.parent()
+ self.failUnlessRaises(dns.name.NoParent, bad)
+
+ def testParent4(self):
+ def bad():
+ n = dns.name.empty
+ n.parent()
+ self.failUnlessRaises(dns.name.NoParent, bad)
+
if __name__ == '__main__':
unittest.main()