Closed GoogleCodeExporter closed 9 years ago
Indeed, thats a bug. addr[-1] should return .143. Here is the fix with a
unittest:
(diff is against Python trunk directory structure rather than ipaddr-py)
Index: Lib/ipaddr.py
===================================================================
--- Lib/ipaddr.py (revision 72488)
+++ Lib/ipaddr.py (working copy)
@@ -209,6 +209,7 @@
raise IndexError
return self._string_from_ip_int(self.network + n)
else:
+ n += 1
if self.broadcast + n < self.network:
raise IndexError
return self._string_from_ip_int(self.broadcast + n)
Index: Lib/test/test_ipaddr.py
===================================================================
--- Lib/test/test_ipaddr.py (revision 72488)
+++ Lib/test/test_ipaddr.py (working copy)
@@ -254,6 +254,17 @@
self.assertEqual(self.ipv6[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):
self.assertTrue(self.ipv4 == ipaddr.IPv4('1.2.3.4/24'))
self.assertFalse(self.ipv4 == ipaddr.IPv4('1.2.3.4/23'))
Original comment by gpsm...@gmail.com
on 8 May 2009 at 11:15
fyi - fixed in python svn trunk and py3k branch r72489 and r72490.
Original comment by gpsm...@gmail.com
on 8 May 2009 at 11:20
Original comment by mshields+personal@google.com
on 9 May 2009 at 12:52
Original issue reported on code.google.com by
CPaet...@gmail.com
on 8 May 2009 at 6:39