Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.

Ipaddress Unicode Error #55

Merged
merged 3 commits into from
Sep 23, 2020
Merged

Ipaddress Unicode Error #55

merged 3 commits into from
Sep 23, 2020

Conversation

Promaethius
Copy link
Contributor

On some systems, the following error occurs resulting in cloud-init never resolving:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: '172.16.4.144' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?

@akutz
Copy link
Contributor

akutz commented Sep 22, 2020

Hi @Promaethius,

On some systems

Which systems? The function you used unicode appears to be a Python 2 built-in, but I do not see it listed for Python 3. Will this change work for Python 2 and not 3? We are trying to maintain compatibility between both versions for now. Can you please ensure any patch works for both versions of Python? Thank you.

cross compatible encoding
@Promaethius
Copy link
Contributor Author

@akutz Any Red Hat system with FIPS enabled. I have no idea why, maybe it sets a system default that python2 picks up on but this change does work. Did a comparison between RHEL7 and RHEL7 with FIPS boot option.

Using Str.encode('utf-8') method instead which should be cross compatible.

@akutz
Copy link
Contributor

akutz commented Sep 22, 2020

Hi @Promaethius,

Thank you!

Using Str.encode('utf-8') method instead which should be cross compatible.

Hmm, while it works in Python 2:

$ python2 -c 'print("hi".encode("utf-8"))'
hi

it appears the function in Python 3 returns a bytes representation of the unicode string:

$ python3 -c 'print("hi".encode("utf-8"))'
b'hi'

See if you can get it to work with both. Meanwhile I think I will conduct a poll to find out how many people are still using this with CentOS 6 and 7. It is past time to EOL support for Python 2 if we can do so with zero to marginal impact.

specific exception handling over python version checking
@Promaethius
Copy link
Contributor Author

@akutz ok I implemented a nested exception structure that looks better than a python version check. It'll at least retry on python2 fails with a unicode string. Python3 should pass right through.

@akutz akutz merged commit 62dae9e into vmware-archive:master Sep 23, 2020
@marcusportmann
Copy link
Contributor

Hello,

This fix isn't working on CentOS 7 with Python 2.7.5. I fixed it by replacing the encode call with a unicode() call in the nested exception structure.

def is_valid_ip_addr(val):
    """
    Returns false if the address is loopback, link local or unspecified;
    otherwise true is returned.
    """
    addr = None
    try:
        try:
            addr = ipaddress.ip_address(val)
        except ipaddress.AddressValueError:
            addr = ipaddress.ip_address(unicode(val))
    except:
        return False
    if addr.is_link_local or addr.is_loopback or addr.is_unspecified:
        return False
    return True

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants