10000 bpo-28577: Special case added to IP v4 and v6 hosts for /32 and /128 networks by JamoBox · Pull Request #18757 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-28577: Special case added to IP v4 and v6 hosts for /32 and /128 networks #18757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Special case added to IPv6Network hosts for /128 networks
Signed-off-by: Pete Wicken <petewicken@gmail.com>
  • Loading branch information
JamoBox committed Mar 3, 2020
commit b7c888a6a55b9157c0c95f391040fccd08b39b26
4 changes: 3 additions & 1 deletion Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ dictionaries.
network address itself and the network broadcast address. For networks
with a mask length of 31, the network address and network broadcast
address are also included in the result. Networks with a mask of 32
will return a list containing the single host address,
will return a list containing the single host address.

>>> list(ip_network('192.0.2.0/2 10000 9').hosts()) #doctest: +NORMALIZE_WHITESPACE
[IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
Expand Down Expand Up @@ -681,6 +681,8 @@ dictionaries.
hosts are all the IP addresses that belong to the network, except the
Subnet-Router anycast address. For networks with a mask length of 127,
the Subnet-Router anycast address is also included in the result.
Networks with a mask of 128 will return a list containing the
single host address.

.. method:: overlaps(other)
.. method:: address_exclude(network)
Expand Down
2 changes: 2 additions & 0 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,8 @@ 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)]

def hosts(self):
"""Generate Iterator over usable hosts in a network.
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,10 +1441,10 @@ def testHosts(self):
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'
tpl_args = ('2001:658:22a:cafe::', 127)

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()),
Expand Down
0