meantime commit: code review, simplification, pythonization, etc. (test cases passed)

pull/1414/head
sebres 9 years ago
parent 9ede535a61
commit 6985531e91

@ -1102,11 +1102,10 @@ class IPAddr(object):
for family in [socket.AF_INET, socket.AF_INET6]: for family in [socket.AF_INET, socket.AF_INET6]:
try: try:
binary = socket.inet_pton(family, ipstring) binary = socket.inet_pton(family, ipstring)
except socket.error:
continue
else:
self.valid = True self.valid = True
break break
except socket.error:
continue
if self.valid and family == socket.AF_INET: if self.valid and family == socket.AF_INET:
# convert host to network byte order # convert host to network byte order
@ -1178,9 +1177,13 @@ class IPAddr(object):
return self.family < other.family or self.addr < other.addr return self.family < other.family or self.addr < other.addr
def __add__(self, other): def __add__(self, other):
if not isinstance(other, IPAddr):
other = IPAddr(other)
return "%s%s" % (self, other) return "%s%s" % (self, other)
def __radd__(self, other): def __radd__(self, other):
if not isinstance(other, IPAddr):
other = IPAddr(other)
return "%s%s" % (other, self) return "%s%s" % (other, self)
def __hash__(self): def __hash__(self):

@ -1383,20 +1383,16 @@ class DNSUtilsNetworkTests(unittest.TestCase):
self.assertEqual(res, None) self.assertEqual(res, None)
def testAddr2bin(self): def testAddr2bin(self):
res = DNSUtils.addr2bin('10.0.0.0') res = IPAddr('10.0.0.0')
self.assertEqual(res, 167772160L) self.assertEqual(res.addr, 167772160L)
res = DNSUtils.addr2bin('10.0.0.0', cidr=None) res = IPAddr('10.0.0.0', cidr=None)
self.assertEqual(res, 167772160L) self.assertEqual(res.addr, 167772160L)
res = DNSUtils.addr2bin('10.0.0.0', cidr=32L) res = IPAddr('10.0.0.0', cidr=32L)
self.assertEqual(res, 167772160L) self.assertEqual(res.addr, 167772160L)
res = DNSUtils.addr2bin('10.0.0.1', cidr=32L) res = IPAddr('10.0.0.1', cidr=32L)
self.assertEqual(res, 167772161L) self.assertEqual(res.addr, 167772161L)
res = DNSUtils.addr2bin('10.0.0.1', cidr=31L) res = IPAddr('10.0.0.1', cidr=31L)
self.assertEqual(res, 167772160L) self.assertEqual(res.addr, 167772160L)
def testBin2addr(self):
res = DNSUtils.bin2addr(167772160L)
self.assertEqual(res, '10.0.0.0')
def testIPAddr_Equal6(self): def testIPAddr_Equal6(self):
self.assertEqual( self.assertEqual(

Loading…
Cancel
Save