Fix an off by one error on negative indexs to __getitem__

http://code.google.com/p/ipaddr-py/issues/detail?id=15
This commit is contained in:
Gregory P. Smith 2009-05-08 23:16:47 +00:00
parent f7f858d141
commit 5edb1a1b0a
2 changed files with 12 additions and 0 deletions

View File

@ -209,6 +209,7 @@ class BaseIP(object):
raise IndexError raise IndexError
return self._string_from_ip_int(self.network + n) return self._string_from_ip_int(self.network + n)
else: else:
n += 1
if self.broadcast + n < self.network: if self.broadcast + n < self.network:
raise IndexError raise IndexError
return self._string_from_ip_int(self.broadcast + n) return self._string_from_ip_int(self.broadcast + n)

View File

@ -254,6 +254,17 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(self.ipv6[5], self.assertEqual(self.ipv6[5],
'2001:658:22a:cafe::5') '2001:658:22a:cafe::5')
def test_getitem(self):
# http://code.google.com/p/ipaddr-py/issues/detail?id=15
addr = ipaddr.IPv4('172.31.255.128/255.255.255.240')
self.assertEqual(28, addr.prefixlen)
addr_list = list(addr)
self.assertEqual('172.31.255.128', addr_list[0])
self.assertEqual('172.31.255.128', addr[0])
self.assertEqual('172.31.255.143', addr_list[-1])
self.assertEqual('172.31.255.143', addr[-1])
self.assertEqual(addr_list[-1], addr[-1])
def test_equals(self): def test_equals(self):
self.assertTrue(self.ipv4 == ipaddr.IPv4('1.2.3.4/24')) self.assertTrue(self.ipv4 == ipaddr.IPv4('1.2.3.4/24'))
self.assertFalse(self.ipv4 == ipaddr.IPv4('1.2.3.4/23')) self.assertFalse(self.ipv4 == ipaddr.IPv4('1.2.3.4/23'))