From 66f600ed3d9f22c2aa6790002507d0c821a7fd0c Mon Sep 17 00:00:00 2001 From: Paula Gombar Date: Tue, 8 Dec 2020 18:38:33 -0800 Subject: [PATCH 2/3] handle py3.9 check in future.py --- azurelinuxagent/common/future.py | 13 ++++++++++++- azurelinuxagent/common/osutil/bigip.py | 8 +++----- azurelinuxagent/common/osutil/default.py | 9 ++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/azurelinuxagent/common/future.py b/azurelinuxagent/common/future.py index 577fb12e186e..0f76aceba786 100644 --- a/azurelinuxagent/common/future.py +++ b/azurelinuxagent/common/future.py @@ -103,4 +103,15 @@ def get_openwrt_platform(): elif product_matches: if product_matches.group(1) == "OpenWrt": result[0] = "openwrt" - return result \ No newline at end of file + return result + + +def array_to_string_or_bytes(buffer): + # Python 3.9 removed the tostring() method on arrays, the new alias is tobytes() + if sys.version_info[0] == 2: + return buffer.tostring() + + if sys.version_info[0] == 3 and sys.version_info[1] <= 8: + return buffer.tostring() + + return buffer.tobytes() diff --git a/azurelinuxagent/common/osutil/bigip.py b/azurelinuxagent/common/osutil/bigip.py index ceadf8ca2066..cc1b64143c12 100644 --- a/azurelinuxagent/common/osutil/bigip.py +++ b/azurelinuxagent/common/osutil/bigip.py @@ -24,6 +24,8 @@ import socket import struct import time +from azurelinuxagent.common.future import array_to_string_or_bytes + try: # WAAgent > 2.1.3 import azurelinuxagent.common.logger as logger @@ -280,12 +282,8 @@ class BigIpOSUtil(DefaultOSUtil): if retsize == (expected * struct_size): logger.warn(('SIOCGIFCONF returned more than {0} up ' 'network interfaces.'), expected) - try: - # Python 3.9 removed the tostring() method on arrays, tobytes() is the new alias - sock = buff.tostring() - except AttributeError: - sock = buff.tobytes() + sock = array_to_string_or_bytes(buff) for i in range(0, struct_size * expected, struct_size): iface = self._format_single_interface_name(sock, i) diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index 6179061756e3..c1ca15cf64ef 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -41,7 +41,7 @@ import azurelinuxagent.common.utils.fileutil as fileutil import azurelinuxagent.common.utils.shellutil as shellutil import azurelinuxagent.common.utils.textutil as textutil from azurelinuxagent.common.exception import OSUtilError -from azurelinuxagent.common.future import ustr +from azurelinuxagent.common.future import ustr, array_to_string_or_bytes from azurelinuxagent.common.utils.cryptutil import CryptUtil from azurelinuxagent.common.utils.flexible_version import FlexibleVersion from azurelinuxagent.common.utils.networkutil import RouteEntry, NetworkInterfaceCard @@ -758,12 +758,7 @@ class DefaultOSUtil(object): logger.warn(('SIOCGIFCONF returned more than {0} up ' 'network interfaces.'), expected) - try: - # Python 3.9 removed the tostring() method on arrays, tobytes() is the new alias - ifconf_buff = buff.tostring() - except AttributeError: - ifconf_buff = buff.tobytes() - + ifconf_buff = array_to_string_or_bytes(buff) ifaces = {} for i in range(0, array_size, struct_size): iface = ifconf_buff[i:i+IFNAMSIZ].split(b'\0', 1)[0] -- 2.26.2