Merge pull request #51918 from twangboy/fix_compat

Fix _compat for py3
This commit is contained in:
Daniel Wozniak 2019-03-07 11:33:27 -07:00 committed by GitHub
commit 48d298c568
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View file

@ -7,8 +7,8 @@ Salt compatibility code
# Import python libs
from __future__ import absolute_import, unicode_literals, print_function
import sys
import types
import logging
import binascii
# Import 3rd-party libs
from salt.exceptions import SaltException
@ -173,7 +173,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address):
self._ip = address
elif self._is_packed_binary(address):
self._check_packed_address(address, 16)
self._ip = ipaddress._int_from_bytes(address, 'big')
self._ip = int(binascii.hexlify(address), 16)
else:
address = str(address)
if '/' in address:
@ -190,7 +190,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address):
packed = False
if isinstance(data, bytes) and len(data) == 16 and b':' not in data:
try:
packed = bool(int(str(bytearray(data)).encode('hex'), 16))
packed = bool(int(binascii.hexlify(data), 16))
except ValueError:
pass

View file

@ -69,3 +69,15 @@ class CompatTestCase(TestCase):
else:
expected = 'StringIO.StringIO instance'
self.assertTrue(expected in repr(ret))
def test_ipv6_class__is_packed_binary(self):
ipv6 = compat.IPv6AddressScoped('2001:db8::')
self.assertEqual(str(ipv6), '2001:db8::')
def test_ipv6_class__is_packed_binary_integer(self):
ipv6 = compat.IPv6AddressScoped(42540766411282592856903984951653826560)
self.assertEqual(str(ipv6), '2001:db8::')
def test_ipv6_class__is_packed_binary__issue_51831(self):
ipv6 = compat.IPv6AddressScoped(b'sixteen.digit.bn')
self.assertEqual(str(ipv6), '7369:7874:6565:6e2e:6469:6769:742e:626e')