classification
Title: ipaddress should accept bytearray in addition to bytes
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, joernheissler, ncoghlan, pmoody, prudvinit, serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2018-08-22 19:48 by joernheissler, last changed 2018-09-11 16:35 by joernheissler.

Pull Requests
URL Status Linked Edit
PR 8908 open prudvinit, 2018-08-24 20:58
Messages (10)
msg323906 - (view) Author: Jörn Heissler (joernheissler) Date: 2018-08-22 19:48
Hi,

the ipaddress module accepts `bytes' objects in the constructors.
`bytearray' however is not supported, see paste below.

Should this be supported too?


>>> import ipaddress

>>> ipaddress.IPv4Address(bytes([127, 0, 0, 1]))
IPv4Address('127.0.0.1')

>>> ipaddress.IPv4Address(bytearray([127, 0, 0, 1]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/ipaddress.py", line 1301, in __init__
    self._ip = self._ip_int_from_string(addr_str)
  File "/usr/lib/python3.7/ipaddress.py", line 1135, in _ip_int_from_string
    raise AddressValueError("Expected 4 octets in %r" % ip_str)
ipaddress.AddressValueError: Expected 4 octets in "bytearray(b'\\x7f\\x00\\x00\\x01')"
msg323982 - (view) Author: Prudvi RajKumar Maddala (prudvinit) * Date: 2018-08-24 02:10
I think it should be supported too. How about a list of bytes? eg : [127,0,0,1].. Should that be supported as well?
msg324047 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-08-25 07:37
What is your use case?

New features can be added only in the developed version.
msg324050 - (view) Author: Jörn Heissler (joernheissler) Date: 2018-08-25 07:47
My use case is parsing binary data. For that I use a bytearray as a buffer. buf[:4] gets me another bytearray which I'd want to convert to an ipaddress.

I can't think of a usecase for list-of-int.
msg324051 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2018-08-25 08:01
Why not just bytes(buf[:4]) before passing?
msg324052 - (view) Author: Jörn Heissler (joernheissler) Date: 2018-08-25 08:08
That's what I'm doing now.
But it would be more convenient if I could pass a bytearray.
msg324437 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2018-08-31 17:02
I'm -1 on this change. I think the workaround is easy and direct.
msg324969 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-09-11 01:01
This isn't limited to just IPv4Address, the Network classes and the IPv6 classes that accept bytes should also be updated for consistency if we're going to do this.

(bytes, bytearray, memoryview) make sense to support, and explicitly test in unittests.

Until then, the workaround is to call bytes on the relevant slice of those.
msg325017 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-11 15:32
See issue27572 for moving in opposite direction. Supporting the buffer protocol rather of just bytes and bytearray complicates the code, and can be considered as a feature creep, especially if an input is a text encoded as bytes.

But in this case the bytes argument of IPv4Address() is not a text representation like b'127.0.0.1', but a packed array of bytes like bytes([127, 0, 0, 1]). Accepting bytearray and memoryview makes more sense for it. Yet I'm not sure that supporting them is worth adding an overhead. This will affect every call of the IPv4Address constructor, and I think that this is not the most common case.

Maybe add a special purposed named constructor IPv4Address.from_bytes() that will accept any objects supporting the buffer protocol?
msg325024 - (view) Author: Jörn Heissler (joernheissler) Date: 2018-09-11 16:35
> Maybe add a special purposed named constructor IPv4Address.from_bytes() that will accept any objects supporting the buffer protocol?

That would work for me.
I wonder if there should be something like ipaddress.ip_address_from_bytes too that can construct IPv4Adress or IPv6Address.
History
Date User Action Args
2018-09-11 16:35:00joernheisslersetmessages: + msg325024
2018-09-11 15:32:58serhiy.storchakasetmessages: + msg325017
2018-09-11 01:01:07gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg324969
2018-08-31 17:02:24xiang.zhangsetmessages: + msg324437
2018-08-25 08:08:23joernheisslersetmessages: + msg324052
2018-08-25 08:01:02xiang.zhangsetnosy: + xiang.zhang
messages: + msg324051
2018-08-25 07:47:44joernheisslersetmessages: + msg324050
2018-08-25 07:37:24serhiy.storchakasetnosy: + pmoody, serhiy.storchaka, ncoghlan

messages: + msg324047
versions: - Python 3.4, Python 3.5, Python 3.6, Python 3.7
2018-08-24 20:58:45prudvinitsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8379
2018-08-24 02:10:31prudvinitsetnosy: + prudvinit
messages: + msg323982
2018-08-22 19:48:00joernheisslercreate