Skip to content

Commit 61801a1

Browse files
committed
[python2] Fixed py2-ipaddress related unicode bug #97
For some strange reason this bug popped out of the blue. Apparently arguments passed to the ipaddress library must be unicode strings. No idea why this didn't come up earlier. Closes #97
1 parent 6b1dd42 commit 61801a1

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

netjsonconfig/backends/openwrt/converters/interfaces.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __intermediate_addresses(self, interface):
6868
# do not use CIDR notation when using a single ipv4
6969
# see https://github.com/openwisp/netjsonconfig/issues/54
7070
if len(static.get('ipaddr', [])) == 1:
71-
network = ip_interface(static['ipaddr'][0])
71+
network = ip_interface(six.text_type(static['ipaddr'][0]))
7272
static['ipaddr'] = str(network.ip)
7373
static['netmask'] = str(network.netmask)
7474
# do not use lists when using a single ipv6 address
@@ -269,7 +269,7 @@ def __netjson_addresses(self, interface):
269269
return interface
270270

271271
def __netjson_address(self, address, interface):
272-
ip = ip_interface(address)
272+
ip = ip_interface(six.text_type(address))
273273
family = 'ipv{0}'.format(ip.version)
274274
netjson = OrderedDict((
275275
('address', str(ip.ip)),
@@ -279,7 +279,7 @@ def __netjson_address(self, address, interface):
279279
))
280280
uci_gateway_key = 'gateway' if family == 'ipv4' else 'ip6gw'
281281
gateway = interface.get(uci_gateway_key, None)
282-
if gateway and ip_address(gateway) in ip.network:
282+
if gateway and ip_address(six.text_type(gateway)) in ip.network:
283283
netjson['gateway'] = gateway
284284
del interface[uci_gateway_key]
285285
return netjson

netjsonconfig/backends/openwrt/converters/routes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from ipaddress import ip_interface
22

3+
import six
4+
35
from ..schema import schema
46
from .base import OpenWrtConverter
57

@@ -16,7 +18,7 @@ def to_intermediate_loop(self, block, result, index=None):
1618
return result
1719

1820
def __intermediate_route(self, route, index):
19-
network = ip_interface(route.pop('destination'))
21+
network = ip_interface(six.text_type(route.pop('destination')))
2022
target = network.ip if network.version == 4 else network.network
2123
route.update({
2224
'.type': 'route{0}'.format('6' if network.version == 6 else ''),
@@ -50,7 +52,7 @@ def __netjson_route(self, route, i):
5052
network = '{0}/{1}'.format(network, route.pop('netmask'))
5153
route.update({
5254
"device": route.pop('interface'),
53-
"destination": str(ip_interface(network)),
55+
"destination": str(ip_interface(six.text_type(network))),
5456
"next": route.pop('gateway'),
5557
"cost": route.pop('metric', self._schema['properties']['cost']['default'])
5658
})

netjsonconfig/backends/openwrt/converters/rules.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from ipaddress import ip_network
22

3+
import six
4+
35
from ..schema import schema
46
from .base import OpenWrtConverter
57

@@ -21,9 +23,9 @@ def __intermediate_rule(self, rule, index):
2123
dest_net = None
2224
family = 4
2325
if 'src' in rule:
24-
src_net = ip_network(rule['src'])
26+
src_net = ip_network(six.text_type(rule['src']))
2527
if 'dest' in rule:
26-
dest_net = ip_network(rule['dest'])
28+
dest_net = ip_network(six.text_type(rule['dest']))
2729
if dest_net or src_net:
2830
family = dest_net.version if dest_net else src_net.version
2931
rule.update({

0 commit comments

Comments
 (0)