Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix _compat for py3 #51918

Merged
merged 2 commits into from Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions salt/_compat.py
Expand Up @@ -7,8 +7,8 @@
# 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
Expand Down Expand Up @@ -173,7 +173,7 @@ def __init__(self, address):
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:
Expand All @@ -190,7 +190,7 @@ def _is_packed_binary(self, data):
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

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test__compat.py
Expand Up @@ -69,3 +69,15 @@ def test_string_io_unicode(self):
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')