diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 1f900583826..20f33cbdeb6 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -636,12 +636,12 @@ def __getitem__(self, n): broadcast = int(self.broadcast_address) if n >= 0: if network + n > broadcast: - raise IndexError + raise IndexError('address out of range') return self._address_class(network + n) else: n += 1 if broadcast + n < network: - raise IndexError + raise IndexError('address out of range') return self._address_class(broadcast + n) def __lt__(self, other): diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index be62fadf0d9..6c08f805da5 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1176,6 +1176,7 @@ def testNth(self): self.assertEqual(str(self.ipv6_network[5]), '2001:658:22a:cafe::5') + self.assertRaises(IndexError, self.ipv6_network.__getitem__, 1 << 64) def testGetitem(self): # http://code.google.com/p/ipaddr-py/issues/detail?id=15 diff --git a/Misc/NEWS b/Misc/NEWS index 8c85c21b185..acf1a2e760f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -38,6 +38,9 @@ Core and Builtins Library ------- +- Issue #20508: Improve exception message of IPv{4,6}Network.__getitem__. + Patch by Gareth Rees. + - Issue #21386: Implement missing IPv4Address.is_global property. It was documented since 07a5610bae9d. Initial patch by Roger Luethi.