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 6 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
31 changes: 4 additions & 27 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,6 @@ 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"
Expand Down Expand Up @@ -1146,11 +1141,11 @@ class _BaseV4:
"""

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

_max_prefixlen = IPV4LENGTH
max_prefixlen = _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 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 @@ -1628,11 +1614,11 @@ class _BaseV6:
"""

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

# There are only a bunch of valid v6 netmasks, so we cache them all
# when constructed (see _make_netmask()).
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
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.