mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add extra detection for hexadecimal packed bytes on Python2.
This cannot be detected with type comparison, because bytes == str and at the same time bytes != str if compatibility is not around
This commit is contained in:
parent
379ead4c65
commit
cefb16b5c2
1 changed files with 18 additions and 1 deletions
|
@ -160,7 +160,8 @@ class IPv6AddressScoped(ipaddress.IPv6Address):
|
|||
if isinstance(address, integer_types):
|
||||
self._check_int_address(address)
|
||||
self._ip = address
|
||||
elif isinstance(address, bytes):
|
||||
elif (sys.version_info.major == 3 and isinstance(address, bytes) # Python-3 compatibility messes up
|
||||
or sys.version_info.major == 2 and self._is_packed_binary(address)): # bytes and str types for Python 2.
|
||||
self._check_packed_address(address, 16)
|
||||
self._ip = ipaddress._int_from_bytes(address, 'big')
|
||||
else:
|
||||
|
@ -169,6 +170,22 @@ class IPv6AddressScoped(ipaddress.IPv6Address):
|
|||
raise ipaddress.AddressValueError("Unexpected '/' in {}".format(address))
|
||||
self._ip = self._ip_int_from_string(address)
|
||||
|
||||
def _is_packed_binary(self, data):
|
||||
'''
|
||||
Check if data is hexadecimal packed
|
||||
|
||||
:param data:
|
||||
:return:
|
||||
'''
|
||||
packed = False
|
||||
if len(data) == 16 and ':' not in data:
|
||||
try:
|
||||
packed = bool(int(str(bytearray(data)).encode('hex'), 16))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
return packed
|
||||
|
||||
@property
|
||||
def scope(self):
|
||||
'''
|
||||
|
|
Loading…
Add table
Reference in a new issue