Don't iterate through addresses in netaddr.IPNetwork
Currently, to_primitive tries to iterate through all addresses in the network, because the type doesn't have a special handling that would short curcuit it, but also has __iter__. This may be detrimental to performance, up to the point of node crash due to memory exhaustion if the passed network range is too large (think of 0.0.0.0/0 or even 2001::/64). This behavior also makes it impossible to restore the original data format (CIDR). This patch short curcuits the iteration by handling the IPNetwork type as a special case, same as we do for IPAddress. Change-Id: I6aecd2d057d282a655ff9e4918c164253142b188 Closes-Bug: #1698355
This commit is contained in:
parent
98642b2d69
commit
38ac21b523
|
@ -112,7 +112,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||||
if isinstance(value, uuid.UUID):
|
if isinstance(value, uuid.UUID):
|
||||||
return six.text_type(value)
|
return six.text_type(value)
|
||||||
|
|
||||||
if netaddr and isinstance(value, netaddr.IPAddress):
|
if netaddr and isinstance(value, (netaddr.IPAddress, netaddr.IPNetwork)):
|
||||||
return six.text_type(value)
|
return six.text_type(value)
|
||||||
|
|
||||||
if ipaddress and isinstance(value,
|
if ipaddress and isinstance(value,
|
||||||
|
|
|
@ -320,6 +320,11 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
||||||
ret = jsonutils.to_primitive(thing)
|
ret = jsonutils.to_primitive(thing)
|
||||||
self.assertEqual({'ip_addr': '2001:db8::'}, ret)
|
self.assertEqual({'ip_addr': '2001:db8::'}, ret)
|
||||||
|
|
||||||
|
def test_ipnet_using_netaddr(self):
|
||||||
|
thing = {'ip_net': netaddr.IPNetwork('1.2.3.0/24')}
|
||||||
|
ret = jsonutils.to_primitive(thing)
|
||||||
|
self.assertEqual({'ip_net': '1.2.3.0/24'}, ret)
|
||||||
|
|
||||||
def test_message_with_param(self):
|
def test_message_with_param(self):
|
||||||
msg = self.trans_fixture.lazy('A message with param: %s')
|
msg = self.trans_fixture.lazy('A message with param: %s')
|
||||||
msg = msg % 'test_domain'
|
msg = msg % 'test_domain'
|
||||||
|
|
Loading…
Reference in New Issue