From 79f9d545f2b96744ca881d30739c717408899450 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:14:18 +0200 Subject: [PATCH 01/37] Add missing docstrings --- salt/_compat.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index 9b10646ace..ce41b4c7f0 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -78,11 +78,23 @@ def bytes_(s, encoding='latin-1', errors='strict'): if PY3: def ascii_native_(s): + ''' + Python 3 handler. + + :param s: + :return: + ''' if isinstance(s, text_type): s = s.encode('ascii') return str(s, 'ascii', 'strict') else: def ascii_native_(s): + ''' + Python 2 handler. + + :param s: + :return: + ''' if isinstance(s, text_type): s = s.encode('ascii') return str(s) From 574810512406df83efc09246169cc4d971ad5f7c Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:14:28 +0200 Subject: [PATCH 02/37] Lintfix: add missing new line --- salt/_compat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/_compat.py b/salt/_compat.py index ce41b4c7f0..8e1c5ce3f3 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -145,6 +145,7 @@ def string_io(data=None): # cStringIO can't handle unicode except (UnicodeEncodeError, TypeError): return StringIO(data) + if PY3: import ipaddress else: From 8c79ad29068d129867a7c08d183a7e70c5fa57d3 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:25:46 +0200 Subject: [PATCH 03/37] Fix ipaddress import and remove extra variable --- tests/unit/modules/test_network.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/tests/unit/modules/test_network.py b/tests/unit/modules/test_network.py index d616d8b2ba..0321c5fcb4 100644 --- a/tests/unit/modules/test_network.py +++ b/tests/unit/modules/test_network.py @@ -25,15 +25,7 @@ import salt.utils.network import salt.utils.path import salt.modules.network as network from salt.exceptions import CommandExecutionError -if six.PY2: - import salt.ext.ipaddress as ipaddress - HAS_IPADDRESS = True -else: - try: - import ipaddress - HAS_IPADDRESS = True - except ImportError: - HAS_IPADDRESS = False +from salt._compat import ipaddress @skipIf(NO_MOCK, NO_MOCK_REASON) @@ -276,7 +268,7 @@ class NetworkTestCase(TestCase, LoaderModuleMockMixin): self.assertDictEqual(network.connect('host', 'port'), {'comment': ret, 'result': True}) - @skipIf(HAS_IPADDRESS is False, 'unable to import \'ipaddress\'') + @skipIf(bool(ipaddress) is False, 'unable to import \'ipaddress\'') def test_is_private(self): ''' Test for Check if the given IP address is a private address @@ -288,7 +280,7 @@ class NetworkTestCase(TestCase, LoaderModuleMockMixin): return_value=True): self.assertTrue(network.is_private('::1')) - @skipIf(HAS_IPADDRESS is False, 'unable to import \'ipaddress\'') + @skipIf(bool(ipaddress) is False, 'unable to import \'ipaddress\'') def test_is_loopback(self): ''' Test for Check if the given IP address is a loopback address From 824e0e598369f0a4ecae108b2e328caa24ae9819 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:26:34 +0200 Subject: [PATCH 04/37] Fix ipaddress imports --- salt/cloud/clouds/saltify.py | 6 +----- salt/ext/win_inet_pton.py | 2 +- salt/minion.py | 5 +---- salt/modules/ipset.py | 5 +---- salt/modules/network.py | 6 +----- salt/modules/vagrant.py | 6 +----- salt/modules/win_network.py | 5 +---- salt/pillar/netbox.py | 2 +- salt/utils/minions.py | 5 +---- tests/unit/grains/test_core.py | 5 +---- 10 files changed, 10 insertions(+), 37 deletions(-) diff --git a/salt/cloud/clouds/saltify.py b/salt/cloud/clouds/saltify.py index c9cc281b42..10ead0c7ad 100644 --- a/salt/cloud/clouds/saltify.py +++ b/salt/cloud/clouds/saltify.py @@ -27,11 +27,7 @@ import salt.utils.cloud import salt.config as config import salt.client import salt.ext.six as six -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress - +from salt._compat import ipaddress from salt.exceptions import SaltCloudException, SaltCloudSystemExit # Get logging started diff --git a/salt/ext/win_inet_pton.py b/salt/ext/win_inet_pton.py index 1204bede10..89aba14ce9 100644 --- a/salt/ext/win_inet_pton.py +++ b/salt/ext/win_inet_pton.py @@ -9,7 +9,7 @@ from __future__ import absolute_import import socket import ctypes import os -import ipaddress +from salt._compat import ipaddress import salt.ext.six as six diff --git a/salt/minion.py b/salt/minion.py index 437a95bccc..7eeeea0664 100644 --- a/salt/minion.py +++ b/salt/minion.py @@ -27,10 +27,7 @@ from binascii import crc32 # Import Salt Libs # pylint: disable=import-error,no-name-in-module,redefined-builtin from salt.ext import six -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress from salt.ext.six.moves import range from salt.utils.zeromq import zmq, ZMQDefaultLoop, install_zmq, ZMQ_VERSION_INFO import salt.defaults.exitcodes diff --git a/salt/modules/ipset.py b/salt/modules/ipset.py index 7047e84c29..1a0fa0044d 100644 --- a/salt/modules/ipset.py +++ b/salt/modules/ipset.py @@ -13,10 +13,7 @@ from salt.ext.six.moves import map, range import salt.utils.path # Import third-party libs -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress # Set up logging log = logging.getLogger(__name__) diff --git a/salt/modules/network.py b/salt/modules/network.py index bdf73dfc89..433523681f 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -26,11 +26,7 @@ from salt.exceptions import CommandExecutionError # Import 3rd-party libs from salt.ext import six from salt.ext.six.moves import range # pylint: disable=import-error,no-name-in-module,redefined-builtin -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress - +from salt._compat import ipaddress log = logging.getLogger(__name__) diff --git a/salt/modules/vagrant.py b/salt/modules/vagrant.py index 0592dede55..0f518c2602 100644 --- a/salt/modules/vagrant.py +++ b/salt/modules/vagrant.py @@ -39,11 +39,7 @@ import salt.utils.path import salt.utils.stringutils from salt.exceptions import CommandExecutionError, SaltInvocationError import salt.ext.six as six - -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress log = logging.getLogger(__name__) diff --git a/salt/modules/win_network.py b/salt/modules/win_network.py index 5e98d17cde..51ed1778bd 100644 --- a/salt/modules/win_network.py +++ b/salt/modules/win_network.py @@ -32,10 +32,7 @@ try: import wmi # pylint: disable=W0611 except ImportError: HAS_DEPENDENCIES = False -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress # Define the module's virtual name __virtualname__ = 'network' diff --git a/salt/pillar/netbox.py b/salt/pillar/netbox.py index 71d99b3bba..21cc3670eb 100644 --- a/salt/pillar/netbox.py +++ b/salt/pillar/netbox.py @@ -53,7 +53,7 @@ import logging # Import Salt libs import salt.utils.http -import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress log = logging.getLogger(__name__) diff --git a/salt/utils/minions.py b/salt/utils/minions.py index 289ee03552..afe3f1a2d0 100644 --- a/salt/utils/minions.py +++ b/salt/utils/minions.py @@ -26,10 +26,7 @@ import salt.cache from salt.ext import six # Import 3rd-party libs -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress HAS_RANGE = False try: import seco.range # pylint: disable=import-error diff --git a/tests/unit/grains/test_core.py b/tests/unit/grains/test_core.py index a731b7c296..d477f3b692 100644 --- a/tests/unit/grains/test_core.py +++ b/tests/unit/grains/test_core.py @@ -36,10 +36,7 @@ import salt.grains.core as core # Import 3rd-party libs from salt.ext import six -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +from salt._compat import ipaddress log = logging.getLogger(__name__) From fa27867cc325cb546a12f911c0dccc2107aa9cca Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:27:04 +0200 Subject: [PATCH 05/37] Make ipaddress importing safe for unit test environments --- salt/_compat.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 8e1c5ce3f3..22fe19b6cb 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -146,7 +146,10 @@ def string_io(data=None): # cStringIO can't handle unicode return StringIO(data) -if PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress +try: + if PY3: + import ipaddress + else: + import salt.ext.ipaddress as ipaddress +except ImportError: + ipaddress = None From 3e0009a46c0cc0e3dbac540d9a0cd82efc93c03e Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:27:20 +0200 Subject: [PATCH 06/37] Remove unused import --- salt/cloud/clouds/vagrant.py | 1 - 1 file changed, 1 deletion(-) diff --git a/salt/cloud/clouds/vagrant.py b/salt/cloud/clouds/vagrant.py index a24170c78a..0159d02623 100644 --- a/salt/cloud/clouds/vagrant.py +++ b/salt/cloud/clouds/vagrant.py @@ -25,7 +25,6 @@ import tempfile import salt.utils import salt.config as config import salt.client -import salt.ext.six as six if six.PY3: import ipaddress else: From 3266d31038f9d792920938384491f186dcf9cf99 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:27:31 +0200 Subject: [PATCH 07/37] Fix ipaddress import --- salt/cloud/clouds/vagrant.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/salt/cloud/clouds/vagrant.py b/salt/cloud/clouds/vagrant.py index 0159d02623..0fe410eb91 100644 --- a/salt/cloud/clouds/vagrant.py +++ b/salt/cloud/clouds/vagrant.py @@ -25,12 +25,8 @@ import tempfile import salt.utils import salt.config as config import salt.client -if six.PY3: - import ipaddress -else: - import salt.ext.ipaddress as ipaddress -from salt.exceptions import SaltCloudException, SaltCloudSystemExit, \ - SaltInvocationError +from salt._compat import ipaddress +from salt.exceptions import SaltCloudException, SaltCloudSystemExit, SaltInvocationError # Get logging started log = logging.getLogger(__name__) From 84afc6669d8bc50a0106a22982d9537c4653fcbb Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:51:45 +0200 Subject: [PATCH 08/37] Fix unicode imports in compat --- salt/_compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/_compat.py b/salt/_compat.py index 22fe19b6cb..a0fa5e7b82 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -5,7 +5,7 @@ Salt compatibility code # pylint: disable=import-error,unused-import,invalid-name # Import python libs -from __future__ import absolute_import +from __future__ import absolute_import, unicode_literals, print_function import sys import types From f532410df97f6aa36dc1f5f13d3e37ea132a0c72 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 17:52:32 +0200 Subject: [PATCH 09/37] Override standard IPv6Address class --- salt/_compat.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index a0fa5e7b82..1a09a9835c 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -10,6 +10,7 @@ import sys import types # Import 3rd-party libs +from salt.exceptions import SaltException from salt.ext.six import binary_type, string_types, text_type from salt.ext.six.moves import cStringIO, StringIO @@ -153,3 +154,64 @@ try: import salt.ext.ipaddress as ipaddress except ImportError: ipaddress = None + + +class IPv6AddressScoped(ipaddress.IPv6Address): + ''' + Represent and manipulate single IPv6 Addresses. + Scope-aware version + ''' + def __init__(self, address): + ''' + Instantiate a new IPv6 address object. Scope is moved to an attribute 'scope'. + + Args: + address: A string or integer representing the IP + + Additionally, an integer can be passed, so + IPv6Address('2001:db8::') == IPv6Address(42540766411282592856903984951653826560) + or, more generally + IPv6Address(int(IPv6Address('2001:db8::'))) == IPv6Address('2001:db8::') + + Raises: + AddressValueError: If address isn't a valid IPv6 address. + + :param address: + ''' + if '%' in address: + buff = address.split('%') + if len(buff) != 2: + raise SaltException('Invalid IPv6 address: "{}"'.format(address)) + address, self.__scope = buff + else: + self.__scope = None + + ipaddress._BaseAddress.__init__(self, address) + ipaddress._BaseV6.__init__(self, address) + + # Efficient constructor from integer. + if isinstance(address, int): + self._check_int_address(address) + self._ip = address + return + + if isinstance(address, bytes): + self._check_packed_address(address, 16) + self._ip = ipaddress._int_from_bytes(address, 'big') + return + + addr_str = str(address) + self._ip = self._ip_int_from_string(addr_str) + + @property + def scope(self): + ''' + Return scope of IPv6 address. + + :return: + ''' + return self.__scope + + +if ipaddress: + ipaddress.IPv6Address = IPv6AddressScoped From 75da90ad917980ffa9e75aaa41ba77adf4d62178 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:27:56 +0200 Subject: [PATCH 10/37] Check version via object --- salt/_compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/_compat.py b/salt/_compat.py index 1a09a9835c..fa08e19419 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -36,7 +36,7 @@ except Exception: # True if we are running on Python 3. -PY3 = sys.version_info[0] == 3 +PY3 = sys.version_info.major == 3 if PY3: From 0baa44beebade79f9421177b16edffeab881d281 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:28:25 +0200 Subject: [PATCH 11/37] Isolate Py2 and Py3 mode --- salt/_compat.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index fa08e19419..ac20b42268 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -11,7 +11,7 @@ import types # Import 3rd-party libs from salt.exceptions import SaltException -from salt.ext.six import binary_type, string_types, text_type +from salt.ext.six import binary_type, string_types, text_type, integer_types from salt.ext.six.moves import cStringIO, StringIO HAS_XML = True @@ -178,7 +178,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :param address: ''' - if '%' in address: + if isinstance(address, string_types) and '%' in address: buff = address.split('%') if len(buff) != 2: raise SaltException('Invalid IPv6 address: "{}"'.format(address)) @@ -186,11 +186,12 @@ class IPv6AddressScoped(ipaddress.IPv6Address): else: self.__scope = None - ipaddress._BaseAddress.__init__(self, address) - ipaddress._BaseV6.__init__(self, address) + if sys.version_info.major == 2: + ipaddress._BaseAddress.__init__(self, address) + ipaddress._BaseV6.__init__(self, address) # Efficient constructor from integer. - if isinstance(address, int): + if isinstance(address, integer_types): self._check_int_address(address) self._ip = address return From c48ec26244f883fc113af09d4b57c9ec11909b8c Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:39:49 +0200 Subject: [PATCH 12/37] Add logging --- salt/_compat.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index ac20b42268..d81499f2a1 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -8,12 +8,15 @@ Salt compatibility code from __future__ import absolute_import, unicode_literals, print_function import sys import types +import logging # Import 3rd-party libs from salt.exceptions import SaltException from salt.ext.six import binary_type, string_types, text_type, integer_types from salt.ext.six.moves import cStringIO, StringIO +log = logging.getLogger(__name__) + HAS_XML = True try: # Python >2.5 From 46b6623f707ec3c926c198e88ac614dde0a2688e Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:40:27 +0200 Subject: [PATCH 13/37] Add debugging to the ip_address method (py2 and py3) --- salt/_compat.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index d81499f2a1..c8c619d6d7 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -217,5 +217,40 @@ class IPv6AddressScoped(ipaddress.IPv6Address): return self.__scope +def ip_address(address): + """Take an IP string/int and return an object of the correct type. + + Args: + address: A string or integer, the IP address. Either IPv4 or + IPv6 addresses may be supplied; integers less than 2**32 will + be considered to be IPv4 by default. + + Returns: + An IPv4Address or IPv6Address object. + + Raises: + ValueError: if the *address* passed isn't either a v4 or a v6 + address + + """ + try: + return ipaddress.IPv4Address(address) + except (ipaddress.AddressValueError, ipaddress.NetmaskValueError): + log.debug('Error while parsing IPv4 address: %s', address, exc_info=True) + + try: + return IPv6AddressScoped(address) + except (ipaddress.AddressValueError, ipaddress.NetmaskValueError): + log.debug('Error while parsing IPv6 address: %s', address, exc_info=True) + + if isinstance(address, bytes): + raise ipaddress.AddressValueError('{} does not appear to be an IPv4 or IPv6 address. ' + 'Did you pass in a bytes (str in Python 2) instead ' + 'of a unicode object?'.format(repr(address))) + + raise ValueError('{} does not appear to be an IPv4 or IPv6 address'.format(repr(address))) + + if ipaddress: ipaddress.IPv6Address = IPv6AddressScoped + ipaddress.ip_address = ip_address From 344d045c587b007aae089ef2af39fa8e829c381c Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:41:01 +0200 Subject: [PATCH 14/37] Remove multiple returns and add check for address syntax --- salt/_compat.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index c8c619d6d7..87f76ebb05 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -197,15 +197,14 @@ class IPv6AddressScoped(ipaddress.IPv6Address): if isinstance(address, integer_types): self._check_int_address(address) self._ip = address - return - - if isinstance(address, bytes): + elif isinstance(address, bytes): self._check_packed_address(address, 16) self._ip = ipaddress._int_from_bytes(address, 'big') - return - - addr_str = str(address) - self._ip = self._ip_int_from_string(addr_str) + else: + address = str(address) + if '/' in address: + raise ipaddress.AddressValueError("Unexpected '/' in {}".format(address)) + self._ip = self._ip_int_from_string(address) @property def scope(self): From f5106b5bbea1c1329ba95f300519f23bbcdbd53e Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:45:30 +0200 Subject: [PATCH 15/37] Remove unnecessary variable for import detection --- salt/_compat.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 87f76ebb05..651a885f02 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -17,7 +17,6 @@ from salt.ext.six.moves import cStringIO, StringIO log = logging.getLogger(__name__) -HAS_XML = True try: # Python >2.5 import xml.etree.cElementTree as ElementTree @@ -35,7 +34,6 @@ except Exception: import elementtree.ElementTree as ElementTree except Exception: ElementTree = None - HAS_XML = False # True if we are running on Python 3. @@ -49,7 +47,7 @@ else: import exceptions -if HAS_XML: +if ElementTree is not None: if not hasattr(ElementTree, 'ParseError'): class ParseError(Exception): ''' From 015aa2d092ab61aad1cf8526daf916d598665e39 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:45:48 +0200 Subject: [PATCH 16/37] Remove duplicated code --- salt/_compat.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 651a885f02..ecc55acf12 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -78,28 +78,21 @@ def bytes_(s, encoding='latin-1', errors='strict'): return s -if PY3: - def ascii_native_(s): - ''' - Python 3 handler. +def ascii_native_(s): + ''' + Python 2/3 handler. - :param s: - :return: - ''' - if isinstance(s, text_type): - s = s.encode('ascii') - return str(s, 'ascii', 'strict') -else: - def ascii_native_(s): - ''' - Python 2 handler. + :param s: + :return: + ''' + if isinstance(s, text_type): + s = s.encode('ascii') + if PY3: + s = str(s, 'ascii', 'strict') + else: + s = str(s) + return s - :param s: - :return: - ''' - if isinstance(s, text_type): - s = s.encode('ascii') - return str(s) ascii_native_.__doc__ = ''' Python 3: If ``s`` is an instance of ``text_type``, return From ef1556880376e63118c515702e71ca8fee5e57fc Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:55:42 +0200 Subject: [PATCH 17/37] Remove unnecessary operator --- salt/_compat.py | 1 - 1 file changed, 1 deletion(-) diff --git a/salt/_compat.py b/salt/_compat.py index ecc55acf12..d328b35f9e 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -53,7 +53,6 @@ if ElementTree is not None: ''' older versions of ElementTree do not have ParseError ''' - pass ElementTree.ParseError = ParseError From c3c4c8c25ee930c27cba4aae4d7362a2fcb73413 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:56:04 +0200 Subject: [PATCH 18/37] Remove multiple returns --- salt/_compat.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index d328b35f9e..9f9d7fd434 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -62,9 +62,7 @@ def text_(s, encoding='latin-1', errors='strict'): If ``s`` is an instance of ``binary_type``, return ``s.decode(encoding, errors)``, otherwise return ``s`` ''' - if isinstance(s, binary_type): - return s.decode(encoding, errors) - return s + return s.decode(encoding, errors) if isinstance(s, binary_type) else s def bytes_(s, encoding='latin-1', errors='strict'): @@ -72,9 +70,7 @@ def bytes_(s, encoding='latin-1', errors='strict'): If ``s`` is an instance of ``text_type``, return ``s.encode(encoding, errors)``, otherwise return ``s`` ''' - if isinstance(s, text_type): - return s.encode(encoding, errors) - return s + return s.encode(encoding, errors) if isinstance(s, text_type) else s def ascii_native_(s): From 85d765d04e41bebaa7e143010c6fad2380e2476c Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:56:33 +0200 Subject: [PATCH 19/37] Use ternary operator instead --- salt/_compat.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 9f9d7fd434..fee9e92c90 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -82,11 +82,8 @@ def ascii_native_(s): ''' if isinstance(s, text_type): s = s.encode('ascii') - if PY3: - s = str(s, 'ascii', 'strict') - else: - s = str(s) - return s + + return str(s, 'ascii', 'strict') if PY3 else s ascii_native_.__doc__ = ''' From 07eb315884a00f57cc04ce39c3e8d05f447204b2 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:56:51 +0200 Subject: [PATCH 20/37] Remove duplicated code --- salt/_compat.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index fee9e92c90..5b208e63f3 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -95,24 +95,17 @@ Python 2: If ``s`` is an instance of ``text_type``, return ''' -if PY3: - def native_(s, encoding='latin-1', errors='strict'): - ''' - If ``s`` is an instance of ``text_type``, return - ``s``, otherwise return ``str(s, encoding, errors)`` - ''' - if isinstance(s, text_type): - return s - return str(s, encoding, errors) -else: - def native_(s, encoding='latin-1', errors='strict'): - ''' - If ``s`` is an instance of ``text_type``, return - ``s.encode(encoding, errors)``, otherwise return ``str(s)`` - ''' - if isinstance(s, text_type): - return s.encode(encoding, errors) - return str(s) +def native_(s, encoding='latin-1', errors='strict'): + ''' + If ``s`` is an instance of ``text_type``, return + ``s``, otherwise return ``str(s, encoding, errors)`` + ''' + if PY3: + out = s if isinstance(s, text_type) else str(s, encoding, errors) + else: + out = s.encode(encoding, errors) if isinstance(s, text_type) else str(s) + + return out native_.__doc__ = ''' Python 3: If ``s`` is an instance of ``text_type``, return ``s``, otherwise From 384d8ada3191c4367ba65904b61867419feec6fd Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 18 Sep 2018 23:58:24 +0200 Subject: [PATCH 21/37] Move docstrings to their native places --- salt/_compat.py | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 5b208e63f3..5fc593aec1 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -75,10 +75,11 @@ def bytes_(s, encoding='latin-1', errors='strict'): def ascii_native_(s): ''' - Python 2/3 handler. + Python 3: If ``s`` is an instance of ``text_type``, return + ``s.encode('ascii')``, otherwise return ``str(s, 'ascii', 'strict')`` - :param s: - :return: + Python 2: If ``s`` is an instance of ``text_type``, return + ``s.encode('ascii')``, otherwise return ``str(s)`` ''' if isinstance(s, text_type): s = s.encode('ascii') @@ -86,19 +87,13 @@ def ascii_native_(s): return str(s, 'ascii', 'strict') if PY3 else s -ascii_native_.__doc__ = ''' -Python 3: If ``s`` is an instance of ``text_type``, return -``s.encode('ascii')``, otherwise return ``str(s, 'ascii', 'strict')`` - -Python 2: If ``s`` is an instance of ``text_type``, return -``s.encode('ascii')``, otherwise return ``str(s)`` -''' - - def native_(s, encoding='latin-1', errors='strict'): ''' - If ``s`` is an instance of ``text_type``, return - ``s``, otherwise return ``str(s, encoding, errors)`` + Python 3: If ``s`` is an instance of ``text_type``, return ``s``, otherwise + return ``str(s, encoding, errors)`` + + Python 2: If ``s`` is an instance of ``text_type``, return + ``s.encode(encoding, errors)``, otherwise return ``str(s)`` ''' if PY3: out = s if isinstance(s, text_type) else str(s, encoding, errors) @@ -107,14 +102,6 @@ def native_(s, encoding='latin-1', errors='strict'): return out -native_.__doc__ = ''' -Python 3: If ``s`` is an instance of ``text_type``, return ``s``, otherwise -return ``str(s, encoding, errors)`` - -Python 2: If ``s`` is an instance of ``text_type``, return -``s.encode(encoding, errors)``, otherwise return ``str(s)`` -''' - def string_io(data=None): # cStringIO can't handle unicode ''' From 3e898b6dc3854032af43cb6ab95e507c7ffa7c4e Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 00:04:03 +0200 Subject: [PATCH 22/37] Add real exception message --- salt/_compat.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 5fc593aec1..e283219c11 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -197,13 +197,15 @@ def ip_address(address): """ try: return ipaddress.IPv4Address(address) - except (ipaddress.AddressValueError, ipaddress.NetmaskValueError): - log.debug('Error while parsing IPv4 address: %s', address, exc_info=True) + except (ipaddress.AddressValueError, ipaddress.NetmaskValueError) as err: + log.debug('Error while parsing IPv4 address: %s', address) + log.debug(err) try: return IPv6AddressScoped(address) - except (ipaddress.AddressValueError, ipaddress.NetmaskValueError): - log.debug('Error while parsing IPv6 address: %s', address, exc_info=True) + except (ipaddress.AddressValueError, ipaddress.NetmaskValueError) as err: + log.debug('Error while parsing IPv6 address: %s', address) + log.debug(err) if isinstance(address, bytes): raise ipaddress.AddressValueError('{} does not appear to be an IPv4 or IPv6 address. ' From 5337860de23e1ec99d019776591ec623a3ae8b77 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 00:04:22 +0200 Subject: [PATCH 23/37] Add logging to the ip_interface --- salt/_compat.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index e283219c11..dff576b473 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -215,6 +215,43 @@ def ip_address(address): raise ValueError('{} does not appear to be an IPv4 or IPv6 address'.format(repr(address))) +def ip_interface(address): + """Take an IP string/int and return an object of the correct type. + + Args: + address: A string or integer, the IP address. Either IPv4 or + IPv6 addresses may be supplied; integers less than 2**32 will + be considered to be IPv4 by default. + + Returns: + An IPv4Interface or IPv6Interface object. + + Raises: + ValueError: if the string passed isn't either a v4 or a v6 + address. + + Notes: + The IPv?Interface classes describe an Address on a particular + Network, so they're basically a combination of both the Address + and Network classes. + + """ + try: + return ipaddress.IPv4Interface(address) + except (ipaddress.AddressValueError, ipaddress.NetmaskValueError) as err: + log.debug('Error while getting IPv4 interface for address %s', address) + log.debug(err) + + try: + return ipaddress.IPv6Interface(address) + except (ipaddress.AddressValueError, ipaddress.NetmaskValueError) as err: + log.debug('Error while getting IPv6 interface for address %s', address) + log.debug(err) + + raise ValueError('{} does not appear to be an IPv4 or IPv6 interface'.format(address)) + + if ipaddress: ipaddress.IPv6Address = IPv6AddressScoped ipaddress.ip_address = ip_address + ipaddress.ip_interface = ip_interface From 2fe8f8aec18d4548b3483a2912a49d211958c151 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 00:32:22 +0200 Subject: [PATCH 24/37] Remove unnecessary manipulation with IPv6 scope outside of the IPv6Address object instance --- salt/utils/dns.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/salt/utils/dns.py b/salt/utils/dns.py index 3687491545..687b9ac4c1 100644 --- a/salt/utils/dns.py +++ b/salt/utils/dns.py @@ -1139,18 +1139,13 @@ def parse_resolv(src='/etc/resolv.conf'): try: (directive, arg) = (line[0].lower(), line[1:]) # Drop everything after # or ; (comments) - arg = list(itertools.takewhile( - lambda x: x[0] not in ('#', ';'), arg)) - + arg = list(itertools.takewhile(lambda x: x[0] not in ('#', ';'), arg)) if directive == 'nameserver': - # Split the scope (interface) if it is present - addr, scope = arg[0].split('%', 1) if '%' in arg[0] else (arg[0], '') + addr = arg[0] try: ip_addr = ipaddress.ip_address(addr) version = ip_addr.version - # Rejoin scope after address validation - if scope: - ip_addr = '%'.join((str(ip_addr), scope)) + ip_addr = str(ip_addr) if ip_addr not in nameservers: nameservers.append(ip_addr) if version == 4 and ip_addr not in ip4_nameservers: From 1d74142d9e3e7faf122ed47819d33be6ca5767fd Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 00:32:40 +0200 Subject: [PATCH 25/37] Add scope on str --- salt/_compat.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index dff576b473..c483b67ae4 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -178,6 +178,10 @@ class IPv6AddressScoped(ipaddress.IPv6Address): ''' return self.__scope + def __str__(self): + return text_type(self._string_from_ip_int(self._ip) + + ('%' + self.scope if self.scope is not None else '')) + def ip_address(address): """Take an IP string/int and return an object of the correct type. From a303822ef880258ff74cee10587bd383d35f2f34 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 00:43:28 +0200 Subject: [PATCH 26/37] Lintfix: W0611 --- tests/unit/modules/test_network.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/modules/test_network.py b/tests/unit/modules/test_network.py index 0321c5fcb4..9ed307f1dc 100644 --- a/tests/unit/modules/test_network.py +++ b/tests/unit/modules/test_network.py @@ -20,7 +20,6 @@ from tests.support.mock import ( ) # Import Salt Libs -from salt.ext import six import salt.utils.network import salt.utils.path import salt.modules.network as network From 284c3ba388b5fe0824743192b71d7d48fe73152d Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 00:45:15 +0200 Subject: [PATCH 27/37] Lintfix: mute not called constructors --- salt/_compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/_compat.py b/salt/_compat.py index c483b67ae4..1027796596 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -2,7 +2,7 @@ ''' Salt compatibility code ''' -# pylint: disable=import-error,unused-import,invalid-name +# pylint: disable=import-error,unused-import,invalid-name,W0231,W0233 # Import python libs from __future__ import absolute_import, unicode_literals, print_function From be2eea50f0a471acae49d9059601f8a3c08780c6 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 15:05:13 +0200 Subject: [PATCH 28/37] Add additional check --- tests/unit/utils/test_jinja.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/utils/test_jinja.py b/tests/unit/utils/test_jinja.py index fea6a2c0b1..1748197936 100644 --- a/tests/unit/utils/test_jinja.py +++ b/tests/unit/utils/test_jinja.py @@ -996,6 +996,10 @@ class TestCustomExtensions(TestCase): ''' Test the `ipaddr` Jinja filter. ''' + rendered = render_jinja_tmpl("{{ '::' | ipaddr }}", + dict(opts=self.local_opts, saltenv='test', salt=self.local_salt)) + self.assertEqual(rendered, '::') + rendered = render_jinja_tmpl("{{ '192.168.0.1' | ipaddr }}", dict(opts=self.local_opts, saltenv='test', salt=self.local_salt)) self.assertEqual(rendered, '192.168.0.1') From 6f2e6cd974207c78eb404c0f8f362de544d69855 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 15:15:42 +0200 Subject: [PATCH 29/37] 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 --- salt/_compat.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/salt/_compat.py b/salt/_compat.py index 1027796596..cad7f92514 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -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): ''' From da1d4a9839e5511b0472068ca2cb99af78de4ea6 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 15:16:41 +0200 Subject: [PATCH 30/37] Fix py2 case where the same class cannot initialise itself on Python2 via super. --- salt/_compat.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index cad7f92514..06eaf4f8b8 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -200,6 +200,25 @@ class IPv6AddressScoped(ipaddress.IPv6Address): ('%' + self.scope if self.scope is not None else '')) +class IPv6InterfaceScoped(ipaddress.IPv6Interface): + ''' + Update + ''' + def __init__(self, address): + if isinstance(address, (bytes, int)): + IPv6AddressScoped.__init__(self, address) + self.network = ipaddress.IPv6Network(self._ip) + self._prefixlen = self._max_prefixlen + return + + addr = ipaddress._split_optional_netmask(address) + IPv6AddressScoped.__init__(self, addr[0]) + self.network = ipaddress.IPv6Network(address, strict=False) + self.netmask = self.network.netmask + self._prefixlen = self.network._prefixlen + self.hostmask = self.network.hostmask + + def ip_address(address): """Take an IP string/int and return an object of the correct type. @@ -274,5 +293,7 @@ def ip_interface(address): if ipaddress: ipaddress.IPv6Address = IPv6AddressScoped + if sys.version_info.major == 2: + ipaddress.IPv6Interface = IPv6AddressScoped ipaddress.ip_address = ip_address ipaddress.ip_interface = ip_interface From cf49ea0a6e5fdbdbdc9b686100cadca0b99249c8 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 15:26:16 +0200 Subject: [PATCH 31/37] Simplify checking clause --- salt/_compat.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 06eaf4f8b8..b8e762e430 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -144,6 +144,10 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :param address: ''' + if not hasattr(self, '_is_packed_binary'): + # This method (below) won't be around for Python 3 and we need check this differently anyway + self._is_packed_binary = lambda p: isinstance(p, bytes) + if isinstance(address, string_types) and '%' in address: buff = address.split('%') if len(buff) != 2: @@ -160,8 +164,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address): if isinstance(address, integer_types): self._check_int_address(address) self._ip = address - 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. + elif self._is_packed_binary(address): self._check_packed_address(address, 16) self._ip = ipaddress._int_from_bytes(address, 'big') else: From 858b50bb7ada6ce2ddbce3b82082774a5a1fcb96 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 15:30:20 +0200 Subject: [PATCH 32/37] Do not use introspection for method swap --- salt/_compat.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index b8e762e430..e6f3721b7f 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -144,10 +144,6 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :param address: ''' - if not hasattr(self, '_is_packed_binary'): - # This method (below) won't be around for Python 3 and we need check this differently anyway - self._is_packed_binary = lambda p: isinstance(p, bytes) - if isinstance(address, string_types) and '%' in address: buff = address.split('%') if len(buff) != 2: @@ -159,6 +155,10 @@ class IPv6AddressScoped(ipaddress.IPv6Address): if sys.version_info.major == 2: ipaddress._BaseAddress.__init__(self, address) ipaddress._BaseV6.__init__(self, address) + else: + # This method (below) won't be around for Python 3 + # and this type is needed to be checked differently + self._is_packed_binary = lambda p: isinstance(p, bytes) # Efficient constructor from integer. if isinstance(address, integer_types): From 10a24d787c8fb0eaa257a4d52bcf5dc3234b027c Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 15:43:18 +0200 Subject: [PATCH 33/37] Fix wrong type swap --- salt/_compat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index e6f3721b7f..fc87202a63 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -203,7 +203,7 @@ class IPv6AddressScoped(ipaddress.IPv6Address): ('%' + self.scope if self.scope is not None else '')) -class IPv6InterfaceScoped(ipaddress.IPv6Interface): +class IPv6InterfaceScoped(ipaddress.IPv6Interface, IPv6AddressScoped): ''' Update ''' @@ -297,6 +297,6 @@ def ip_interface(address): if ipaddress: ipaddress.IPv6Address = IPv6AddressScoped if sys.version_info.major == 2: - ipaddress.IPv6Interface = IPv6AddressScoped + ipaddress.IPv6Interface = IPv6InterfaceScoped ipaddress.ip_address = ip_address ipaddress.ip_interface = ip_interface From e0e5346cae4b491b9086d7ab610e4087838abd56 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Wed, 19 Sep 2018 16:54:47 +0200 Subject: [PATCH 34/37] Add Py3.4 old implementation's fix --- salt/_compat.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/salt/_compat.py b/salt/_compat.py index fc87202a63..c15cb84d80 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -160,6 +160,11 @@ class IPv6AddressScoped(ipaddress.IPv6Address): # and this type is needed to be checked differently self._is_packed_binary = lambda p: isinstance(p, bytes) + # Python 3.4 fix. Versions higher are simply not affected + # https://github.com/python/cpython/blob/3.4/Lib/ipaddress.py#L543-L544 + self._version = 6 + self._max_prefixlen = ipaddress.IPV6LENGTH + # Efficient constructor from integer. if isinstance(address, integer_types): self._check_int_address(address) From 550b8c6cddf5d7f860ee2414a5dd1d3c73bdcd1b Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Fri, 21 Sep 2018 11:30:46 +0200 Subject: [PATCH 35/37] Lintfix --- salt/_compat.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index c15cb84d80..65b4f3dcf3 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -156,10 +156,6 @@ class IPv6AddressScoped(ipaddress.IPv6Address): ipaddress._BaseAddress.__init__(self, address) ipaddress._BaseV6.__init__(self, address) else: - # This method (below) won't be around for Python 3 - # and this type is needed to be checked differently - self._is_packed_binary = lambda p: isinstance(p, bytes) - # Python 3.4 fix. Versions higher are simply not affected # https://github.com/python/cpython/blob/3.4/Lib/ipaddress.py#L543-L544 self._version = 6 @@ -185,6 +181,9 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :param data: :return: ''' + if sys.version_info.major > 2: + return isinstance(data, bytes) + packed = False if len(data) == 16 and ':' not in data: try: From e18e7bdc87690920d635b28710038c3c339f1f81 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Fri, 21 Sep 2018 11:34:41 +0200 Subject: [PATCH 36/37] Lintfix refactor: remove duplicate returns as not needed --- salt/_compat.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 65b4f3dcf3..38806fd6e2 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -181,15 +181,15 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :param data: :return: ''' - if sys.version_info.major > 2: - return isinstance(data, bytes) - packed = False - if len(data) == 16 and ':' not in data: - try: - packed = bool(int(str(bytearray(data)).encode('hex'), 16)) - except ValueError: - pass + if sys.version_info.major > 2: + packed = isinstance(data, bytes) + else: + if len(data) == 16 and ':' not in data: + try: + packed = bool(int(str(bytearray(data)).encode('hex'), 16)) + except ValueError: + pass return packed From 736246df86b63724de1df24d11117a94e5630a37 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Mon, 24 Sep 2018 11:46:38 +0200 Subject: [PATCH 37/37] Revert method remapping with pylint updates --- salt/_compat.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/salt/_compat.py b/salt/_compat.py index 38806fd6e2..0576210afc 100644 --- a/salt/_compat.py +++ b/salt/_compat.py @@ -144,6 +144,13 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :param address: ''' + # pylint: disable-all + if not hasattr(self, '_is_packed_binary'): + # This method (below) won't be around for some Python 3 versions + # and we need check this differently anyway + self._is_packed_binary = lambda p: isinstance(p, bytes) + # pylint: enable-all + if isinstance(address, string_types) and '%' in address: buff = address.split('%') if len(buff) != 2: @@ -182,14 +189,11 @@ class IPv6AddressScoped(ipaddress.IPv6Address): :return: ''' packed = False - if sys.version_info.major > 2: - packed = isinstance(data, bytes) - else: - if len(data) == 16 and ':' not in data: - try: - packed = bool(int(str(bytearray(data)).encode('hex'), 16)) - except ValueError: - pass + if len(data) == 16 and ':' not in data: + try: + packed = bool(int(str(bytearray(data)).encode('hex'), 16)) + except ValueError: + pass return packed