Skip to content

gh-78646: Add bytearray support to ipaddress #135811

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 4 additions & 4 deletions Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ write code that handles both IP versions correctly. Address objects are
integer represents an octet (byte) in the address. Leading zeroes are
not tolerated to prevent confusion with octal notation.
2. An integer that fits into 32 bits.
3. An integer packed into a :class:`bytes` object of length 4 (most
3. An integer packed into a :class:`bytes` or :class:`bytearray` object of length 4 (most
significant octet first).

>>> ipaddress.IPv4Address('192.168.0.1')
Expand Down Expand Up @@ -310,7 +310,7 @@ write code that handles both IP versions correctly. Address objects are
See :RFC:`4007` for details.
For example, ``fe80::1234%1`` might identify address ``fe80::1234`` on the first link of the node.
2. An integer that fits into 128 bits.
3. An integer packed into a :class:`bytes` object of length 16, big-endian.
3. An integer packed into a :class:`bytes` or :class:`bytearray` object of length 16, big-endian.


>>> ipaddress.IPv6Address('2001:db8::1000')
Expand Down Expand Up @@ -507,7 +507,7 @@ dictionaries.
single-address network, with the network address being *address* and
the mask being ``/32``.

3. An integer packed into a :class:`bytes` object of length 4, big-endian.
3. An integer packed into a :class:`bytes` or :class:`bytearray` object of length 4, big-endian.
The interpretation is similar to an integer *address*.

4. A two-tuple of an address description and a netmask, where the address
Expand Down Expand Up @@ -728,7 +728,7 @@ dictionaries.
single-address network, with the network address being *address* and
the mask being ``/128``.

3. An integer packed into a :class:`bytes` object of length 16, big-endian.
3. An integer packed into a :class:`bytes` or :class:`bytearray` object of length 16, big-endian.
The interpretation is similar to an integer *address*.

4. A two-tuple of an address description and a netmask, where the address
Expand Down
6 changes: 3 additions & 3 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def _split_addr_prefix(cls, address):
(addr, prefix) tuple.
"""
# a packed address or integer
if isinstance(address, (bytes, int)):
if isinstance(address, (bytes, bytearray, int)):
return address, cls.max_prefixlen

if not isinstance(address, tuple):
Expand Down Expand Up @@ -1292,7 +1292,7 @@ def __init__(self, address):
return

# Constructing from a packed address
if isinstance(address, bytes):
if isinstance(address, bytes) or isinstance(address, bytearray):
self._check_packed_address(address, 4)
self._ip = int.from_bytes(address) # big endian
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a note, the int.from_bytes here will still memcpy to go from bytearray -> bytes internally (gh-132108). So this is just moving the need to copy at the moment, not eliminating entirely.

return
Expand Down Expand Up @@ -1938,7 +1938,7 @@ def __init__(self, address):
return

# Constructing from a packed address
if isinstance(address, bytes):
if isinstance(address, bytes) or isinstance(address, bytearray):
self._check_packed_address(address, 16)
self._ip = int.from_bytes(address, 'big')
self._scope_id = None
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def test_packed(self):
self.assertInstancesEqual(bytes.fromhex("00000000"), "0.0.0.0")
self.assertInstancesEqual(bytes.fromhex("c0a80001"), "192.168.0.1")

def test_packed_bytearray(self):
self.assertInstancesEqual(bytearray.fromhex("00000000"), "0.0.0.0")
self.assertInstancesEqual(bytearray.fromhex("c0a80001"), "192.168.0.1")

def test_negative_ints_rejected(self):
msg = "-1 (< 0) is not permitted as an IPv4 address"
with self.assertAddressError(re.escape(msg)):
Expand Down Expand Up @@ -161,6 +165,14 @@ def test_packed(self):
addr = bytes.fromhex("c0a80001") + b'\0'*12
self.assertInstancesEqual(addr, "c0a8:1::")

def test_packed_bytearray(self):
addr = bytearray(b'\0'*12) + bytearray.fromhex("00000000")
self.assertInstancesEqual(addr, "::")
addr = bytearray(b'\0'*12) + bytearray.fromhex("c0a80001")
self.assertInstancesEqual(addr, "::c0a8:1")
addr = bytearray.fromhex("c0a80001") + bytearray(b'\0'*12)
self.assertInstancesEqual(addr, "c0a8:1::")

def test_negative_ints_rejected(self):
msg = "-1 (< 0) is not permitted as an IPv6 address"
with self.assertAddressError(re.escape(msg)):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :class:`bytearray` support for ipaddress.
Loading