diff options
| author | Pete Wicken <2273100+JamoBox@users.noreply.github.com> | 2020-03-09 22:33:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-09 15:33:45 -0700 |
| commit | 8e9c47a947954c997d4b725f4551d50a1d896722 (patch) | |
| tree | 72a0d1e8aaf5962503c008323ce8ada7f037e8eb /Lib | |
| parent | 9229eeee105f19705f72e553cf066751ac47c7b7 (diff) | |
| download | cpython-git-8e9c47a947954c997d4b725f4551d50a1d896722.tar.gz | |
bpo-28577: Special case added to IP v4 and v6 hosts for /32 and /128 networks (GH-18757)
The `.hosts()` method now returns the single address present in a /32 or /128 network.
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/ipaddress.py | 4 | ||||
| -rw-r--r-- | Lib/test/test_ipaddress.py | 17 |
2 files changed, 17 insertions, 4 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 702433953a..ac1143acba 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1509,6 +1509,8 @@ class IPv4Network(_BaseV4, _BaseNetwork): if self._prefixlen == (self._max_prefixlen - 1): self.hosts = self.__iter__ + elif self._prefixlen == (self._max_prefixlen): + self.hosts = lambda: [IPv4Address(addr)] @property @functools.lru_cache() @@ -2212,6 +2214,8 @@ class IPv6Network(_BaseV6, _BaseNetwork): if self._prefixlen == (self._max_prefixlen - 1): self.hosts = self.__iter__ + elif self._prefixlen == self._max_prefixlen: + self.hosts = lambda: [IPv6Address(addr)] def hosts(self): """Generate Iterator over usable hosts in a network. diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index f4a4afba76..bbb3fc89da 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1424,14 +1424,15 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEqual(list(ipaddress.ip_network(str_args).hosts()), list(ipaddress.ip_network(tpl_args).hosts())) - addrs = [ipaddress.IPv6Address('2001:658:22a:cafe::'), - ipaddress.IPv6Address('2001:658:22a:cafe::1')] - str_args = '2001:658:22a:cafe::/127' - tpl_args = ('2001:658:22a:cafe::', 127) + # special case where the network is a /32 + addrs = [ipaddress.IPv4Address('1.2.3.4')] + str_args = '1.2.3.4/32' + tpl_args = ('1.2.3.4', 32) self.assertEqual(addrs, list(ipaddress.ip_network(str_args).hosts())) self.assertEqual(addrs, list(ipaddress.ip_network(tpl_args).hosts())) self.assertEqual(list(ipaddress.ip_network(str_args).hosts()), list(ipaddress.ip_network(tpl_args).hosts())) + addrs = [ipaddress.IPv6Address('2001:658:22a:cafe::'), ipaddress.IPv6Address('2001:658:22a:cafe::1')] str_args = '2001:658:22a:cafe::/127' @@ -1441,6 +1442,14 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEqual(list(ipaddress.ip_network(str_args).hosts()), list(ipaddress.ip_network(tpl_args).hosts())) + addrs = [ipaddress.IPv6Address('2001:658:22a:cafe::1'), ] + str_args = '2001:658:22a:cafe::1/128' + tpl_args = ('2001:658:22a:cafe::1', 128) + self.assertEqual(addrs, list(ipaddress.ip_network(str_args).hosts())) + self.assertEqual(addrs, list(ipaddress.ip_network(tpl_args).hosts())) + self.assertEqual(list(ipaddress.ip_network(str_args).hosts()), + list(ipaddress.ip_network(tpl_args).hosts())) + def testFancySubnetting(self): self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)), sorted(self.ipv4_network.subnets(new_prefix=27))) |
