timmerk / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

Off-by-one in index #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. addr = IPv4("172.31.255.128/255.255.255.240")
2. list(addr)[-1] != addr[-1]

What is the expected output? What do you see instead?

addr[-1] should give last element of list: '172.31.255.143', but 
gives '172.31.255.142' instead.

What version of the product are you using? On what operating system?
1.0.2 on Debian Linux (Lenny), Python 2.5

Please provide any additional information below.
Fantastic tool, thank you! This minor bug might be the cause of some other 
hard-to-track-down problems.

Original issue reported on code.google.com by CPaet...@gmail.com on 8 May 2009 at 6:39

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by mshields+personal@google.com on 9 May 2009 at 12:52