Fix compatibility issue when using netifaces

Bug #1743673 reported by Jianghua Wang on 2018-01-17
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
os-xenapi
Undecided
Jianghua Wang

Bug Description

ipaddress.IPv4Interface only accept unicoded string as input parameter. But some old python's netifaces module returns a str. If we use the IPs returned from netifaces, ipaddress will report the following error:

>>> import ipaddress
>>> ipaddress.IPv4Interface('10.10.10.10/24')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/ipaddress.py", line 1483, in _init_
IPv4Address._init_(self, address)
File "/usr/lib/python2.7/site-packages/ipaddress.py", line 1391, in _init_
self._check_packed_address(address, 4)
File "/usr/lib/python2.7/site-packages/ipaddress.py", line 554, in _check_packed_address
expected_len, self._version))
ipaddress.AddressValueError: '10.10.10.10/24' (len 14 != 4) is not permitted as an IPv4 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?
>>> ipaddress.IPv4Interface(u'10.10.10.10/24')
IPv4Interface(u'10.10.10.10/24')

Jianghua Wang (wjh-fresh) wrote :

Python 2.7.5 produces non-unicoded str

[root@localhost os-xenapi]# python
{color:red}Python 2.7.5 {color}(default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import netifaces
>>> netifaces.ifaddresses('eth0').get(netifaces.AF_INET, [])
{color:red}[{'broadcast': '10.10.10.255', 'netmask': '255.255.255.0', 'addr': '10.10.10.10'}]{color}
python 2.7.12 produces unicoded string

stack@DevStackOSDomU:~/os-xenapi$ python
{color:red}Python 2.7.12{color} (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> eth='eth2'
>>> netifaces.ifaddresses(eth).get(netifaces.AF_INET, [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'netifaces' is not defined
>>> import netifaces
>>> netifaces.ifaddresses(eth).get(netifaces.AF_INET, [])
[]
>>> netifaces.ifaddresses('eth0').get(netifaces.AF_INET, [])
[{'broadcast': u'10.71.199.255', 'netmask': u'255.255.248.0', 'addr': {color:red}u'{color}10.7
>>>

Fix proposed to branch: master
Review: https://review.openstack.org/534583

Changed in os-xenapi:
assignee: nobody → Jianghua Wang (wjh-fresh)
status: New → In Progress

Reviewed: https://review.openstack.org/534583
Committed: https://git.openstack.org/cgit/openstack/os-xenapi/commit/?id=fd35c7dc3ea95c30eb12bee61af9edc2f208b8aa
Submitter: Zuul
Branch: master

commit fd35c7dc3ea95c30eb12bee61af9edc2f208b8aa
Author: Jianghua Wang <email address hidden>
Date: Wed Jan 17 01:34:40 2018 +0000

    Fix compatibility issue when using netifaces

    Force using unicode before pass the string to ipaddress.IPv4Interface().

    Change-Id: Idebb198ad2751a77e7399a3809a3951f6c899dee
    Closes-Bug: #1743673

Changed in os-xenapi:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers