From 5edb1a1b0add465bae25121ee4278a9ec6009005 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Fri, 8 May 2009 23:16:47 +0000 Subject: [PATCH] Fix an off by one error on negative indexs to __getitem__ http://code.google.com/p/ipaddr-py/issues/detail?id=15 --- Lib/ipaddr.py | 1 + Lib/test/test_ipaddr.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/Lib/ipaddr.py b/Lib/ipaddr.py index b5518edb046..7289e456e43 100644 --- a/Lib/ipaddr.py +++ b/Lib/ipaddr.py @@ -209,6 +209,7 @@ class BaseIP(object): 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) diff --git a/Lib/test/test_ipaddr.py b/Lib/test/test_ipaddr.py index 98553269bf4..46b9cab6a98 100755 --- a/Lib/test/test_ipaddr.py +++ b/Lib/test/test_ipaddr.py @@ -254,6 +254,17 @@ class IpaddrUnitTest(unittest.TestCase): 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'))