diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 6978483544e621..e805cd0e2a0124 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1150,15 +1150,15 @@ def next_network(self, next_prefix=None): ((new_netmask._ip & self.network_address._ip) >> bit_shift) + 1 ) << bit_shift - try: - return self.__class__( - f"{self._string_from_ip_int(next_ip)}/{next_prefix}" - ) - except OverflowError: + if next_ip > self._ALL_ONES: raise ValueError( f"out of address space, cannot make another /{next_prefix} " "network" - ) from None + ) + + return self.__class__( + f"{self._string_from_ip_int(next_ip)}/{next_prefix}" + ) class _BaseConstants: diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 375d45172a9f4d..d6d0d8220449bd 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1596,9 +1596,15 @@ def testNextNetworkWithBadPrefix(self): def testNextNetworkOutOfAddressSpace(self): ipv4 = ipaddress.IPv4Network('255.255.255.0/24') - self.assertRaises(ValueError, ipv4.next_network) + self.assertRaisesRegex( + ValueError, + 'out of address space, cannot make another /24 network', + ipv4.next_network) ipv6 = ipaddress.IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112') - self.assertRaises(ValueError, ipv6.next_network) + self.assertRaisesRegex( + ValueError, + 'out of address space, cannot make another /112 network', + ipv6.next_network) def testFancySubnetting(self): self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)),