[3.13] gh-141497: Make ipaddress.IP{v4,v6}Network.hosts() always returning an iterator (GH-141547) (GH-141695)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if the ABI has changed (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run

(cherry picked from commit 6b1bdf6c7a)

Co-authored-by: Krishna Chaitanya <141550576+XChaitanyaX@users.noreply.github.com>
This commit is contained in:
Krishna Chaitanya 2025-11-18 15:13:15 +05:30 committed by GitHub
parent d4e4924a5f
commit 7b3312747f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 2 deletions

View file

@ -1559,7 +1559,7 @@ 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)]
self.hosts = lambda: iter((IPv4Address(addr),))
@property
@functools.lru_cache()
@ -2359,7 +2359,7 @@ 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)]
self.hosts = lambda: iter((IPv6Address(addr),))
def hosts(self):
"""Generate Iterator over usable hosts in a network.

View file

@ -12,6 +12,7 @@ import operator
import pickle
import ipaddress
import weakref
from collections.abc import Iterator
from test.support import LARGEST, SMALLEST
@ -1472,18 +1473,27 @@ class IpaddrUnitTest(unittest.TestCase):
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 @@ class IpaddrUnitTest(unittest.TestCase):
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 @@ class IpaddrUnitTest(unittest.TestCase):
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 @@ class IpaddrUnitTest(unittest.TestCase):
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 @@ class IpaddrUnitTest(unittest.TestCase):
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()),

View file

@ -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.