Skip to content

Commit f47be44

Browse files
committed
ipaddress: Add bytearray support
1 parent ac9d37c commit f47be44

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

Doc/library/ipaddress.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ write code that handles both IP versions correctly. Address objects are
106106
integer represents an octet (byte) in the address. Leading zeroes are
107107
not tolerated to prevent confusion with octal notation.
108108
2. An integer that fits into 32 bits.
109-
3. An integer packed into a :class:`bytes` object of length 4 (most
109+
3. An integer packed into a :class:`bytes` or :class:`bytearray` object of length 4 (most
110110
significant octet first).
111111

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

315315

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

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

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

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

734734
4. A two-tuple of an address description and a netmask, where the address

Lib/ipaddress.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def _split_addr_prefix(cls, address):
536536
(addr, prefix) tuple.
537537
"""
538538
# a packed address or integer
539-
if isinstance(address, (bytes, int)):
539+
if isinstance(address, (bytes, bytearray, int)):
540540
return address, cls.max_prefixlen
541541

542542
if not isinstance(address, tuple):
@@ -1292,7 +1292,7 @@ def __init__(self, address):
12921292
return
12931293

12941294
# Constructing from a packed address
1295-
if isinstance(address, bytes):
1295+
if isinstance(address, bytes) or isinstance(address, bytearray):
12961296
self._check_packed_address(address, 4)
12971297
self._ip = int.from_bytes(address) # big endian
12981298
return
@@ -1938,7 +1938,7 @@ def __init__(self, address):
19381938
return
19391939

19401940
# Constructing from a packed address
1941-
if isinstance(address, bytes):
1941+
if isinstance(address, bytes) or isinstance(address, bytearray):
19421942
self._check_packed_address(address, 16)
19431943
self._ip = int.from_bytes(address, 'big')
19441944
self._scope_id = None

Lib/test/test_ipaddress.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ def test_packed(self):
122122
self.assertInstancesEqual(bytes.fromhex("00000000"), "0.0.0.0")
123123
self.assertInstancesEqual(bytes.fromhex("c0a80001"), "192.168.0.1")
124124

125+
def test_packed_bytearray(self):
126+
self.assertInstancesEqual(bytearray.fromhex("00000000"), "0.0.0.0")
127+
self.assertInstancesEqual(bytearray.fromhex("c0a80001"), "192.168.0.1")
128+
125129
def test_negative_ints_rejected(self):
126130
msg = "-1 (< 0) is not permitted as an IPv4 address"
127131
with self.assertAddressError(re.escape(msg)):
@@ -161,6 +165,14 @@ def test_packed(self):
161165
addr = bytes.fromhex("c0a80001") + b'\0'*12
162166
self.assertInstancesEqual(addr, "c0a8:1::")
163167

168+
def test_packed_bytearray(self):
169+
addr = bytearray(b'\0'*12) + bytearray.fromhex("00000000")
170+
self.assertInstancesEqual(addr, "::")
171+
addr = bytearray(b'\0'*12) + bytearray.fromhex("c0a80001")
172+
self.assertInstancesEqual(addr, "::c0a8:1")
173+
addr = bytearray.fromhex("c0a80001") + bytearray(b'\0'*12)
174+
self.assertInstancesEqual(addr, "c0a8:1::")
175+
164176
def test_negative_ints_rejected(self):
165177
msg = "-1 (< 0) is not permitted as an IPv6 address"
166178
with self.assertAddressError(re.escape(msg)):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :class:`bytearray` support for ipaddress.

0 commit comments

Comments
 (0)