Skip to content

Gracefully handle ZEROFILL flag when given a IPv6 address #383

@kennell

Description

@kennell

I am in the process of upgrading netaddr from 0.10.1 to 1.2.1, my Python version is 3.12.3.

In older versions the netaddr.ZEROFILL flag was ignored when a IPv6 address was given:

# netaddr==0.10.1
>>> import netaddr
>>> ip = netaddr.IPAddress('2600:1f18:631e:2f85:93a9:f7b0:d18:89a7', flags=netaddr.ZEROFILL)
>>> ip
IPAddress('2600:1f18:631e:2f85:93a9:f7b0:d18:89a7')

In the current release it throws an ValueError:

# netaddr==1.2.1
>>> import netaddr
>>> ip = netaddr.IPAddress('2600:1f18:631e:2f85:93a9:f7b0:d18:89a7', flags=netaddr.ZEROFILL)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/lib/python3.12/site-packages/netaddr/ip/__init__.py", line 333, in __init__
    self._value = module.str_to_int(addr, flags)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/netaddr/strategy/ipv4.py", line 120, in str_to_int
    addr = '.'.join(['%d' % int(i) for i in addr.split('.')])
                            ^^^^^^
ValueError: invalid literal for int() with base 10: '2600:1f18:631e:2f85:93a9:f7b0:d18:89a7'

Suggestion: ignore the netaddr.ZEROFILL flag when a IPv6 is given.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions