Skip to content

gh-78646: Add bytearray support to ipaddress#135811

Open
matan1008 wants to merge 1 commit intopython:mainfrom
matan1008:support-ipaddress-from-bytearray
Open

gh-78646: Add bytearray support to ipaddress#135811
matan1008 wants to merge 1 commit intopython:mainfrom
matan1008:support-ipaddress-from-bytearray

Conversation

@matan1008
Copy link
Copy Markdown
Contributor

@matan1008 matan1008 commented Jun 22, 2025

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Jun 22, 2025

The original issue did not reach a consensus IMO and there was a suggestion:

Maybe add a special purposed named constructor IPv4Address.from_bytes() that will accept any objects supporting the buffer protocol?

cc @serhiy-storchaka

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Jun 22, 2025
Comment thread Lib/ipaddress.py
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
Copy Markdown
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.

@serhiy-storchaka
Copy link
Copy Markdown
Member

I think that instead of overloading and slowing down main constructors, we should add more specific alternative constructors -- from bytes (accepts also anything that implements the buffer protocol), from integer, from string.

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review pending The issue will be closed if no feedback is provided stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants