mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-141497: Make ipaddress.IP{v4,v6}Network.hosts() always returning an iterator (GH-141547)
This commit is contained in:
parent
274a26cca8
commit
6b1bdf6c7a
3 changed files with 40 additions and 2 deletions
|
|
@ -1546,7 +1546,7 @@ def __init__(self, address, strict=True):
|
|||
if self._prefixlen == (self.max_prefixlen - 1):
|
||||
self.hosts = self.__iter__
|
||||
elif self._prefixlen == (self.max_prefixlen):
|
||||
self.hosts = lambda: [IPv4Address(addr)]
|
||||
self.hosts = lambda: iter((IPv4Address(addr),))
|
||||
|
||||
@property
|
||||
@functools.lru_cache()
|
||||
|
|
@ -2337,7 +2337,7 @@ def __init__(self, address, strict=True):
|
|||
if self._prefixlen == (self.max_prefixlen - 1):
|
||||
self.hosts = self.__iter__
|
||||
elif self._prefixlen == self.max_prefixlen:
|
||||
self.hosts = lambda: [IPv6Address(addr)]
|
||||
self.hosts = lambda: iter((IPv6Address(addr),))
|
||||
|
||||
def hosts(self):
|
||||
"""Generate Iterator over usable hosts in a network.
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
import pickle
|
||||
import ipaddress
|
||||
import weakref
|
||||
from collections.abc import Iterator
|
||||
from test.support import LARGEST, SMALLEST
|
||||
|
||||
|
||||
|
|
@ -1472,18 +1473,27 @@ def testGetSupernet4(self):
|
|||
self.ipv6_scoped_network.supernet(new_prefix=62))
|
||||
|
||||
def testHosts(self):
|
||||
hosts = self.ipv4_network.hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), next(hosts))
|
||||
hosts = list(self.ipv4_network.hosts())
|
||||
self.assertEqual(254, len(hosts))
|
||||
self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), hosts[0])
|
||||
self.assertEqual(ipaddress.IPv4Address('1.2.3.254'), hosts[-1])
|
||||
|
||||
ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/120')
|
||||
hosts = ipv6_network.hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(ipaddress.IPv6Address('2001:658:22a:cafe::1'), next(hosts))
|
||||
hosts = list(ipv6_network.hosts())
|
||||
self.assertEqual(255, len(hosts))
|
||||
self.assertEqual(ipaddress.IPv6Address('2001:658:22a:cafe::1'), hosts[0])
|
||||
self.assertEqual(ipaddress.IPv6Address('2001:658:22a:cafe::ff'), hosts[-1])
|
||||
|
||||
ipv6_scoped_network = ipaddress.IPv6Network('2001:658:22a:cafe::%scope/120')
|
||||
hosts = ipv6_scoped_network.hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual((ipaddress.IPv6Address('2001:658:22a:cafe::1')), next(hosts))
|
||||
hosts = list(ipv6_scoped_network.hosts())
|
||||
self.assertEqual(255, len(hosts))
|
||||
self.assertEqual(ipaddress.IPv6Address('2001:658:22a:cafe::1'), hosts[0])
|
||||
|
|
@ -1494,6 +1504,12 @@ def testHosts(self):
|
|||
ipaddress.IPv4Address('2.0.0.1')]
|
||||
str_args = '2.0.0.0/31'
|
||||
tpl_args = ('2.0.0.0', 31)
|
||||
hosts = ipaddress.ip_network(str_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
hosts = ipaddress.ip_network(tpl_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
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()),
|
||||
|
|
@ -1503,6 +1519,12 @@ def testHosts(self):
|
|||
addrs = [ipaddress.IPv4Address('1.2.3.4')]
|
||||
str_args = '1.2.3.4/32'
|
||||
tpl_args = ('1.2.3.4', 32)
|
||||
hosts = ipaddress.ip_network(str_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
hosts = ipaddress.ip_network(tpl_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
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()),
|
||||
|
|
@ -1512,6 +1534,12 @@ def testHosts(self):
|
|||
ipaddress.IPv6Address('2001:658:22a:cafe::1')]
|
||||
str_args = '2001:658:22a:cafe::/127'
|
||||
tpl_args = ('2001:658:22a:cafe::', 127)
|
||||
hosts = ipaddress.ip_network(str_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
hosts = ipaddress.ip_network(tpl_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
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()),
|
||||
|
|
@ -1520,6 +1548,12 @@ def testHosts(self):
|
|||
addrs = [ipaddress.IPv6Address('2001:658:22a:cafe::1'), ]
|
||||
str_args = '2001:658:22a:cafe::1/128'
|
||||
tpl_args = ('2001:658:22a:cafe::1', 128)
|
||||
hosts = ipaddress.ip_network(str_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
hosts = ipaddress.ip_network(tpl_args).hosts()
|
||||
self.assertIsInstance(hosts, Iterator)
|
||||
self.assertEqual(next(hosts), addrs[0])
|
||||
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()),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
:mod:`ipaddress`: ensure that the methods
|
||||
:meth:`IPv4Network.hosts() <ipaddress.IPv4Network.hosts>` and
|
||||
:meth:`IPv6Network.hosts() <ipaddress.IPv6Network.hosts>` always return an
|
||||
iterator.
|
||||
Loading…
Add table
Add a link
Reference in a new issue