Skip to content

Update ipaddress from 3.13.5 #5878

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 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
193 changes: 167 additions & 26 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def collapse_addresses(addresses):
[IPv4Network('192.0.2.0/24')]

Args:
addresses: An iterator of IPv4Network or IPv6Network objects.
addresses: An iterable of IPv4Network or IPv6Network objects.

Returns:
An iterator of the collapsed IPv(4|6)Network objects.
Expand Down Expand Up @@ -734,7 +734,7 @@ def __eq__(self, other):
return NotImplemented

def __hash__(self):
return hash(int(self.network_address) ^ int(self.netmask))
return hash((int(self.network_address), int(self.netmask)))

def __contains__(self, other):
# always false if one is v4 and the other is v6.
Expand Down Expand Up @@ -1086,7 +1086,11 @@ def is_private(self):
"""
return any(self.network_address in priv_network and
self.broadcast_address in priv_network
for priv_network in self._constants._private_networks)
for priv_network in self._constants._private_networks) and all(
self.network_address not in network and
self.broadcast_address not in network
for network in self._constants._private_networks_exceptions
)

@property
def is_global(self):
Expand Down Expand Up @@ -1333,18 +1337,41 @@ def is_reserved(self):
@property
@functools.lru_cache()
def is_private(self):
"""Test if this address is allocated for private networks.
"""``True`` if the address is defined as not globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exceptions:

Returns:
A boolean, True if the address is reserved per
iana-ipv4-special-registry.
* ``is_private`` is ``False`` for ``100.64.0.0/10``
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_private == address.ipv4_mapped.is_private

``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
"""
return any(self in net for net in self._constants._private_networks)
return (
any(self in net for net in self._constants._private_networks)
and all(self not in net for net in self._constants._private_networks_exceptions)
)

@property
@functools.lru_cache()
def is_global(self):
"""``True`` if the address is defined as globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exception:

For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_global == address.ipv4_mapped.is_global

``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
"""
return self not in self._constants._public_network and not self.is_private

@property
Expand Down Expand Up @@ -1389,6 +1416,16 @@ def is_link_local(self):
"""
return self in self._constants._linklocal_network

@property
def ipv6_mapped(self):
"""Return the IPv4-mapped IPv6 address.

Returns:
The IPv4-mapped IPv6 address per RFC 4291.

"""
return IPv6Address(f'::ffff:{self}')


class IPv4Interface(IPv4Address):

Expand Down Expand Up @@ -1548,13 +1585,15 @@ class _IPv4Constants:

_public_network = IPv4Network('100.64.0.0/10')

# Not globally reachable address blocks listed on
# https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
_private_networks = [
IPv4Network('0.0.0.0/8'),
IPv4Network('10.0.0.0/8'),
IPv4Network('127.0.0.0/8'),
IPv4Network('169.254.0.0/16'),
IPv4Network('172.16.0.0/12'),
IPv4Network('192.0.0.0/29'),
IPv4Network('192.0.0.0/24'),
IPv4Network('192.0.0.170/31'),
IPv4Network('192.0.2.0/24'),
IPv4Network('192.168.0.0/16'),
Expand All @@ -1565,6 +1604,11 @@ class _IPv4Constants:
IPv4Network('255.255.255.255/32'),
]

_private_networks_exceptions = [
IPv4Network('192.0.0.9/32'),
IPv4Network('192.0.0.10/32'),
]

_reserved_network = IPv4Network('240.0.0.0/4')

_unspecified_address = IPv4Address('0.0.0.0')
Expand Down Expand Up @@ -1630,8 +1674,18 @@ def _ip_int_from_string(cls, ip_str):
"""
if not ip_str:
raise AddressValueError('Address cannot be empty')

parts = ip_str.split(':')
if len(ip_str) > 45:
shorten = ip_str
if len(shorten) > 100:
shorten = f'{ip_str[:45]}({len(ip_str)-90} chars elided){ip_str[-45:]}'
raise AddressValueError(f"At most 45 characters expected in "
f"{shorten!r}")

# We want to allow more parts than the max to be 'split'
# to preserve the correct error message when there are
# too many parts combined with '::'
_max_parts = cls._HEXTET_COUNT + 1
parts = ip_str.split(':', maxsplit=_max_parts)

# An IPv6 address needs at least 2 colons (3 parts).
_min_parts = 3
Expand All @@ -1651,7 +1705,6 @@ def _ip_int_from_string(cls, ip_str):
# An IPv6 address can't have more than 8 colons (9 parts).
# The extra colon comes from using the "::" notation for a single
# leading or trailing zero part.
_max_parts = cls._HEXTET_COUNT + 1
if len(parts) > _max_parts:
msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str)
raise AddressValueError(msg)
Expand Down Expand Up @@ -1923,8 +1976,49 @@ def __init__(self, address):

self._ip = self._ip_int_from_string(addr_str)

def _explode_shorthand_ip_string(self):
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
return super()._explode_shorthand_ip_string()
prefix_len = 30
raw_exploded_str = super()._explode_shorthand_ip_string()
return f"{raw_exploded_str[:prefix_len]}{ipv4_mapped!s}"

def _reverse_pointer(self):
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
return super()._reverse_pointer()
prefix_len = 30
raw_exploded_str = super()._explode_shorthand_ip_string()[:prefix_len]
# ipv4 encoded using hexadecimal nibbles instead of decimals
ipv4_int = ipv4_mapped._ip
reverse_chars = f"{raw_exploded_str}{ipv4_int:008x}"[::-1].replace(':', '')
return '.'.join(reverse_chars) + '.ip6.arpa'

def _ipv4_mapped_ipv6_to_str(self):
"""Return convenient text representation of IPv4-mapped IPv6 address

See RFC 4291 2.5.5.2, 2.2 p.3 for details.

Returns:
A string, 'x:x:x:x:x:x:d.d.d.d', where the 'x's are the hexadecimal values of
the six high-order 16-bit pieces of the address, and the 'd's are
the decimal values of the four low-order 8-bit pieces of the
address (standard IPv4 representation) as defined in RFC 4291 2.2 p.3.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
raise AddressValueError("Can not apply to non-IPv4-mapped IPv6 address %s" % str(self))
high_order_bits = self._ip >> 32
return "%s:%s" % (self._string_from_ip_int(high_order_bits), str(ipv4_mapped))

def __str__(self):
ip_str = super().__str__()
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
ip_str = super().__str__()
else:
ip_str = self._ipv4_mapped_ipv6_to_str()
return ip_str + '%' + self._scope_id if self._scope_id else ip_str

def __hash__(self):
Expand Down Expand Up @@ -1967,6 +2061,9 @@ def is_multicast(self):
See RFC 2373 2.7 for details.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_multicast
return self in self._constants._multicast_network

@property
Expand All @@ -1978,6 +2075,9 @@ def is_reserved(self):
reserved IPv6 Network ranges.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_reserved
return any(self in x for x in self._constants._reserved_networks)

@property
Expand All @@ -1988,6 +2088,9 @@ def is_link_local(self):
A boolean, True if the address is reserved per RFC 4291.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_link_local
return self in self._constants._linklocal_network

@property
Expand All @@ -2007,28 +2110,46 @@ def is_site_local(self):
@property
@functools.lru_cache()
def is_private(self):
"""Test if this address is allocated for private networks.
"""``True`` if the address is defined as not globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exceptions:

Returns:
A boolean, True if the address is reserved per
iana-ipv6-special-registry, or is ipv4_mapped and is
reserved in the iana-ipv4-special-registry.
* ``is_private`` is ``False`` for ``100.64.0.0/10``
* For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_private == address.ipv4_mapped.is_private

``is_private`` has value opposite to :attr:`is_global`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_private
return any(self in net for net in self._constants._private_networks)
return (
any(self in net for net in self._constants._private_networks)
and all(self not in net for net in self._constants._private_networks_exceptions)
)

@property
def is_global(self):
"""Test if this address is allocated for public networks.
"""``True`` if the address is defined as globally reachable by
iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
(for IPv6) with the following exception:

Returns:
A boolean, true if the address is not reserved per
iana-ipv6-special-registry.
For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the
semantics of the underlying IPv4 addresses and the following condition holds
(see :attr:`IPv6Address.ipv4_mapped`)::

address.is_global == address.ipv4_mapped.is_global

``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
IPv4 range where they are both ``False``.
"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_global
return not self.is_private

@property
Expand All @@ -2040,6 +2161,9 @@ def is_unspecified(self):
RFC 2373 2.5.2.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_unspecified
return self._ip == 0

@property
Expand All @@ -2051,6 +2175,9 @@ def is_loopback(self):
RFC 2373 2.5.3.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_loopback
return self._ip == 1

@property
Expand Down Expand Up @@ -2167,7 +2294,7 @@ def is_unspecified(self):

@property
def is_loopback(self):
return self._ip == 1 and self.network.is_loopback
return super().is_loopback and self.network.is_loopback


class IPv6Network(_BaseV6, _BaseNetwork):
Expand Down Expand Up @@ -2268,19 +2395,33 @@ class _IPv6Constants:

_multicast_network = IPv6Network('ff00::/8')

# Not globally reachable address blocks listed on
# https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
_private_networks = [
IPv6Network('::1/128'),
IPv6Network('::/128'),
IPv6Network('::ffff:0:0/96'),
IPv6Network('64:ff9b:1::/48'),
IPv6Network('100::/64'),
IPv6Network('2001::/23'),
IPv6Network('2001:2::/48'),
IPv6Network('2001:db8::/32'),
IPv6Network('2001:10::/28'),
# IANA says N/A, let's consider it not globally reachable to be safe
IPv6Network('2002::/16'),
# RFC 9637: https://www.rfc-editor.org/rfc/rfc9637.html#section-6-2.2
IPv6Network('3fff::/20'),
IPv6Network('fc00::/7'),
IPv6Network('fe80::/10'),
]

_private_networks_exceptions = [
IPv6Network('2001:1::1/128'),
IPv6Network('2001:1::2/128'),
IPv6Network('2001:3::/32'),
IPv6Network('2001:4:112::/48'),
IPv6Network('2001:20::/28'),
IPv6Network('2001:30::/28'),
]

_reserved_networks = [
IPv6Network('::/8'), IPv6Network('100::/8'),
IPv6Network('200::/7'), IPv6Network('400::/6'),
Expand Down
Loading
Loading