Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ipaddress
import weakref
from test.support import LARGEST, SMALLEST
from typing import Iterator


class BaseTestCase(unittest.TestCase):
Expand Down Expand Up @@ -1472,12 +1473,16 @@ def testGetSupernet4(self):
self.ipv6_scoped_network.supernet(new_prefix=62))

def testHosts(self):
hosts = self.ipv4_network.hosts()
self.assertIsInstance(hosts, Iterator)
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)
hosts = list(ipv6_network.hosts())
self.assertEqual(255, len(hosts))
self.assertEqual(ipaddress.IPv6Address('2001:658:22a:cafe::1'), hosts[0])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In :mod:`ipaddress`, ensure that the methods ``IPv6Network.hosts()`` and
``IPv4Network.hosts()`` always returns an iterator.
Loading