Skip to content

gh-118710: IPv*Address class properties #120698

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 8 commits into from
Sep 4, 2024
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
8 changes: 8 additions & 0 deletions Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ write code that handles both IP versions correctly. Address objects are

The appropriate version number: ``4`` for IPv4, ``6`` for IPv6.

.. versionchanged:: 3.14

Made available on the class.

.. attribute:: max_prefixlen

The total number of bits in the address representation for this
Expand All @@ -140,6 +144,10 @@ write code that handles both IP versions correctly. Address objects are
are compared to determine whether or not an address is part of a
network.

.. versionchanged:: 3.14

Made available on the class.

.. attribute:: compressed
.. attribute:: exploded

Expand Down
105 changes: 41 additions & 64 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def summarize_address_range(first, last):
else:
raise ValueError('unknown IP version')

ip_bits = first._max_prefixlen
ip_bits = first.max_prefixlen
first_int = first._ip
last_int = last._ip
while first_int <= last_int:
Expand Down Expand Up @@ -326,20 +326,20 @@ def collapse_addresses(addresses):
# split IP addresses and networks
for ip in addresses:
if isinstance(ip, _BaseAddress):
if ips and ips[-1]._version != ip._version:
if ips and ips[-1].version != ip.version:
raise TypeError("%s and %s are not of the same version" % (
ip, ips[-1]))
ips.append(ip)
elif ip._prefixlen == ip._max_prefixlen:
if ips and ips[-1]._version != ip._version:
elif ip._prefixlen == ip.max_prefixlen:
if ips and ips[-1].version != ip.version:
raise TypeError("%s and %s are not of the same version" % (
ip, ips[-1]))
try:
ips.append(ip.ip)
except AttributeError:
ips.append(ip.network_address)
else:
if nets and nets[-1]._version != ip._version:
if nets and nets[-1].version != ip.version:
raise TypeError("%s and %s are not of the same version" % (
ip, nets[-1]))
nets.append(ip)
Expand Down Expand Up @@ -407,26 +407,21 @@ def reverse_pointer(self):
"""
return self._reverse_pointer()

@property
def version(self):
msg = '%200s has no version specified' % (type(self),)
raise NotImplementedError(msg)

def _check_int_address(self, address):
if address < 0:
msg = "%d (< 0) is not permitted as an IPv%d address"
raise AddressValueError(msg % (address, self._version))
raise AddressValueError(msg % (address, self.version))
if address > self._ALL_ONES:
msg = "%d (>= 2**%d) is not permitted as an IPv%d address"
raise AddressValueError(msg % (address, self._max_prefixlen,
self._version))
raise AddressValueError(msg % (address, self.max_prefixlen,
self.version))

def _check_packed_address(self, address, expected_len):
address_len = len(address)
if address_len != expected_len:
msg = "%r (len %d != %d) is not permitted as an IPv%d address"
raise AddressValueError(msg % (address, address_len,
expected_len, self._version))
expected_len, self.version))

@classmethod
def _ip_int_from_prefix(cls, prefixlen):
Expand Down Expand Up @@ -455,12 +450,12 @@ def _prefix_from_ip_int(cls, ip_int):
ValueError: If the input intermingles zeroes & ones
"""
trailing_zeroes = _count_righthand_zero_bits(ip_int,
cls._max_prefixlen)
prefixlen = cls._max_prefixlen - trailing_zeroes
cls.max_prefixlen)
prefixlen = cls.max_prefixlen - trailing_zeroes
leading_ones = ip_int >> trailing_zeroes
all_ones = (1 << prefixlen) - 1
if leading_ones != all_ones:
byteslen = cls._max_prefixlen // 8
byteslen = cls.max_prefixlen // 8
details = ip_int.to_bytes(byteslen, 'big')
msg = 'Netmask pattern %r mixes zeroes & ones'
raise ValueError(msg % details)
Expand Down Expand Up @@ -492,7 +487,7 @@ def _prefix_from_prefix_string(cls, prefixlen_str):
prefixlen = int(prefixlen_str)
except ValueError:
cls._report_invalid_netmask(prefixlen_str)
if not (0 <= prefixlen <= cls._max_prefixlen):
if not (0 <= prefixlen <= cls.max_prefixlen):
cls._report_invalid_netmask(prefixlen_str)
return prefixlen

Expand Down Expand Up @@ -542,7 +537,7 @@ def _split_addr_prefix(cls, address):
"""
# a packed address or integer
if isinstance(address, (bytes, int)):
return address, cls._max_prefixlen
return address, cls.max_prefixlen

if not isinstance(address, tuple):
# Assume input argument to be string or any object representation
Expand All @@ -552,7 +547,7 @@ def _split_addr_prefix(cls, address):
# Constructing from a tuple (addr, [mask])
if len(address) > 1:
return address
return address[0], cls._max_prefixlen
return address[0], cls.max_prefixlen

def __reduce__(self):
return self.__class__, (str(self),)
Expand All @@ -577,14 +572,14 @@ def __int__(self):
def __eq__(self, other):
try:
return (self._ip == other._ip
and self._version == other._version)
and self.version == other.version)
except AttributeError:
return NotImplemented

def __lt__(self, other):
if not isinstance(other, _BaseAddress):
return NotImplemented
if self._version != other._version:
if self.version != other.version:
raise TypeError('%s and %s are not of the same version' % (
self, other))
if self._ip != other._ip:
Expand Down Expand Up @@ -613,7 +608,7 @@ def __hash__(self):
return hash(hex(int(self._ip)))

def _get_address_key(self):
return (self._version, self)
return (self.version, self)

def __reduce__(self):
return self.__class__, (self._ip,)
Expand Down Expand Up @@ -649,15 +644,15 @@ def __format__(self, fmt):

# Set some defaults
if fmt_base == 'n':
if self._version == 4:
if self.version == 4:
fmt_base = 'b' # Binary is default for ipv4
else:
fmt_base = 'x' # Hex is default for ipv6

if fmt_base == 'b':
padlen = self._max_prefixlen
padlen = self.max_prefixlen
else:
padlen = self._max_prefixlen // 4
padlen = self.max_prefixlen // 4

if grouping:
padlen += padlen // 4 - 1
Expand Down Expand Up @@ -716,7 +711,7 @@ def __getitem__(self, n):
def __lt__(self, other):
if not isinstance(other, _BaseNetwork):
return NotImplemented
if self._version != other._version:
if self.version != other.version:
raise TypeError('%s and %s are not of the same version' % (
self, other))
if self.network_address != other.network_address:
Expand All @@ -727,7 +722,7 @@ def __lt__(self, other):

def __eq__(self, other):
try:
return (self._version == other._version and
return (self.version == other.version and
self.network_address == other.network_address and
int(self.netmask) == int(other.netmask))
except AttributeError:
Expand All @@ -738,7 +733,7 @@ def __hash__(self):

def __contains__(self, other):
# always false if one is v4 and the other is v6.
if self._version != other._version:
if self.version != other.version:
return False
# dealing with another network.
if isinstance(other, _BaseNetwork):
Expand Down Expand Up @@ -829,7 +824,7 @@ def address_exclude(self, other):
ValueError: If other is not completely contained by self.

"""
if not self._version == other._version:
if not self.version == other.version:
raise TypeError("%s and %s are not of the same version" % (
self, other))

Expand Down Expand Up @@ -901,10 +896,10 @@ def compare_networks(self, other):

"""
# does this need to raise a ValueError?
if self._version != other._version:
if self.version != other.version:
raise TypeError('%s and %s are not of the same type' % (
self, other))
# self._version == other._version below here:
# self.version == other.version below here:
if self.network_address < other.network_address:
return -1
if self.network_address > other.network_address:
Expand All @@ -924,7 +919,7 @@ def _get_networks_key(self):
and list.sort().

"""
return (self._version, self.network_address, self.netmask)
return (self.version, self.network_address, self.netmask)

def subnets(self, prefixlen_diff=1, new_prefix=None):
"""The subnets which join to make the current subnet.
Expand Down Expand Up @@ -952,7 +947,7 @@ def subnets(self, prefixlen_diff=1, new_prefix=None):
number means a larger network)

"""
if self._prefixlen == self._max_prefixlen:
if self._prefixlen == self.max_prefixlen:
yield self
return

Expand All @@ -967,7 +962,7 @@ def subnets(self, prefixlen_diff=1, new_prefix=None):
raise ValueError('prefix length diff must be > 0')
new_prefixlen = self._prefixlen + prefixlen_diff

if new_prefixlen > self._max_prefixlen:
if new_prefixlen > self.max_prefixlen:
raise ValueError(
'prefix length diff %d is invalid for netblock %s' % (
new_prefixlen, self))
Expand Down Expand Up @@ -1036,7 +1031,7 @@ def is_multicast(self):
def _is_subnet_of(a, b):
try:
# Always false if one is v4 and the other is v6.
if a._version != b._version:
if a.version != b.version:
raise TypeError(f"{a} and {b} are not of the same version")
return (b.network_address <= a.network_address and
b.broadcast_address >= a.broadcast_address)
Expand Down Expand Up @@ -1146,11 +1141,11 @@ class _BaseV4:
"""

__slots__ = ()
_version = 4
version = 4
# Equivalent to 255.255.255.255 or 32 bits of 1's.
_ALL_ONES = (2**IPV4LENGTH) - 1

_max_prefixlen = IPV4LENGTH
max_prefixlen = IPV4LENGTH
# There are only a handful of valid v4 netmasks, so we cache them all
# when constructed (see _make_netmask()).
_netmask_cache = {}
Expand All @@ -1170,7 +1165,7 @@ def _make_netmask(cls, arg):
if arg not in cls._netmask_cache:
if isinstance(arg, int):
prefixlen = arg
if not (0 <= prefixlen <= cls._max_prefixlen):
if not (0 <= prefixlen <= cls.max_prefixlen):
cls._report_invalid_netmask(prefixlen)
else:
try:
Expand Down Expand Up @@ -1268,15 +1263,6 @@ def _reverse_pointer(self):
reverse_octets = str(self).split('.')[::-1]
return '.'.join(reverse_octets) + '.in-addr.arpa'

@property
def max_prefixlen(self):
return self._max_prefixlen

@property
def version(self):
return self._version


class IPv4Address(_BaseV4, _BaseAddress):

"""Represent and manipulate single IPv4 Addresses."""
Expand Down Expand Up @@ -1556,9 +1542,9 @@ def __init__(self, address, strict=True):
self.network_address = IPv4Address(packed &
int(self.netmask))

if self._prefixlen == (self._max_prefixlen - 1):
if self._prefixlen == (self.max_prefixlen - 1):
self.hosts = self.__iter__
elif self._prefixlen == (self._max_prefixlen):
elif self._prefixlen == (self.max_prefixlen):
self.hosts = lambda: [IPv4Address(addr)]

@property
Expand Down Expand Up @@ -1628,11 +1614,11 @@ class _BaseV6:
"""

__slots__ = ()
_version = 6
version = 6
_ALL_ONES = (2**IPV6LENGTH) - 1
_HEXTET_COUNT = 8
_HEX_DIGITS = frozenset('0123456789ABCDEFabcdef')
_max_prefixlen = IPV6LENGTH
max_prefixlen = IPV6LENGTH

# There are only a bunch of valid v6 netmasks, so we cache them all
# when constructed (see _make_netmask()).
Expand All @@ -1650,7 +1636,7 @@ def _make_netmask(cls, arg):
if arg not in cls._netmask_cache:
if isinstance(arg, int):
prefixlen = arg
if not (0 <= prefixlen <= cls._max_prefixlen):
if not (0 <= prefixlen <= cls.max_prefixlen):
cls._report_invalid_netmask(prefixlen)
else:
prefixlen = cls._prefix_from_prefix_string(arg)
Expand Down Expand Up @@ -1912,15 +1898,6 @@ def _split_scope_id(ip_str):
raise AddressValueError('Invalid IPv6 address: "%r"' % ip_str)
return addr, scope_id

@property
def max_prefixlen(self):
return self._max_prefixlen

@property
def version(self):
return self._version


class IPv6Address(_BaseV6, _BaseAddress):

"""Represent and manipulate single IPv6 Addresses."""
Expand Down Expand Up @@ -2323,9 +2300,9 @@ def __init__(self, address, strict=True):
self.network_address = IPv6Address(packed &
int(self.netmask))

if self._prefixlen == (self._max_prefixlen - 1):
if self._prefixlen == (self.max_prefixlen - 1):
self.hosts = self.__iter__
elif self._prefixlen == self._max_prefixlen:
elif self._prefixlen == self.max_prefixlen:
self.hosts = lambda: [IPv6Address(addr)]

def hosts(self):
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,11 +2189,17 @@ def testIPv6AddressTooLarge(self):
ipaddress.ip_address('FFFF::c000:201%scope'))

def testIPVersion(self):
self.assertEqual(ipaddress.IPv4Address.version, 4)
self.assertEqual(ipaddress.IPv6Address.version, 6)

self.assertEqual(self.ipv4_address.version, 4)
self.assertEqual(self.ipv6_address.version, 6)
self.assertEqual(self.ipv6_scoped_address.version, 6)

def testMaxPrefixLength(self):
self.assertEqual(ipaddress.IPv4Address.max_prefixlen, 32)
self.assertEqual(ipaddress.IPv6Address.max_prefixlen, 128)

self.assertEqual(self.ipv4_interface.max_prefixlen, 32)
self.assertEqual(self.ipv6_interface.max_prefixlen, 128)
self.assertEqual(self.ipv6_scoped_interface.max_prefixlen, 128)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`ipaddress.IPv4Address` and :class:`ipaddress.IPv6Address` attributes ``version`` and ``max_prefixlen`` are now available on the class.
Loading