Skip to content

Commit f9eb739

Browse files
authored
Remove unnecessary calls to str.encode() now that the package is Python 3; Fix deprecation warning (sonic-net#1260)
In Python 3, all strings are unicode by default. There is no need to encode strings to bytes. Also fix Python 3 deprecation warning in decode-syseeprom, as the `imp` module is deprecated in favor of `importlib`
1 parent b445364 commit f9eb739

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

acl_loader/main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,14 @@ def convert_ip(self, table_name, rule_idx, rule):
429429
rule_props["IP_PROTOCOL"] = rule.ip.config.protocol
430430

431431
if rule.ip.config.source_ip_address:
432-
source_ip_address = rule.ip.config.source_ip_address.encode("ascii")
432+
source_ip_address = rule.ip.config.source_ip_address
433433
if ipaddress.ip_network(source_ip_address).version == 4:
434434
rule_props["SRC_IP"] = source_ip_address
435435
else:
436436
rule_props["SRC_IPV6"] = source_ip_address
437437

438438
if rule.ip.config.destination_ip_address:
439-
destination_ip_address = rule.ip.config.destination_ip_address.encode("ascii")
439+
destination_ip_address = rule.ip.config.destination_ip_address
440440
if ipaddress.ip_network(destination_ip_address).version == 4:
441441
rule_props["DST_IP"] = destination_ip_address
442442
else:
@@ -550,7 +550,7 @@ def convert_rules(self):
550550
:return:
551551
"""
552552
for acl_set_name in self.yang_acl.acl.acl_sets.acl_set:
553-
table_name = acl_set_name.replace(" ", "_").replace("-", "_").upper().encode('ascii')
553+
table_name = acl_set_name.replace(" ", "_").replace("-", "_").upper()
554554
acl_set = self.yang_acl.acl.acl_sets.acl_set[acl_set_name]
555555

556556
if not self.is_table_valid(table_name):

fwutil/lib.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -417,24 +417,6 @@ def __parse_module_section(self, module):
417417
self.__module_component_map[key] = OrderedDict()
418418
self.__parse_component_section(key, value[self.COMPONENT_KEY], True)
419419

420-
# TODO: This function should not be necessary once we no longer support Python 2
421-
def __deunicodify_hook(self, pairs):
422-
new_pairs = [ ]
423-
424-
for key, value in pairs:
425-
try:
426-
key = key.encode(self.UTF8_ENCODING)
427-
except Exception:
428-
pass
429-
430-
try:
431-
value = value.encode(self.UTF8_ENCODING)
432-
except Exception:
433-
pass
434-
435-
new_pairs.append((key, value))
436-
437-
return OrderedDict(new_pairs)
438420

439421
def get_chassis_component_map(self):
440422
return self.__chassis_component_map
@@ -451,7 +433,7 @@ def parse_platform_components(self, root_path=None):
451433
platform_components_path = self.__get_platform_components_path(root_path)
452434

453435
with open(platform_components_path) as platform_components:
454-
data = json.load(platform_components, object_pairs_hook=self.__deunicodify_hook)
436+
data = json.load(platform_components)
455437

456438
if not self.__is_dict(data):
457439
self.__parser_platform_fail("dictionary is expected: key=root")

scripts/decode-syseeprom

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#
77
try:
88
import glob
9-
import imp
109
import optparse
1110
import os
1211
import sys
1312
import warnings
13+
from importlib.machinery import SourceFileLoader
1414

1515
from sonic_py_common import device_info
1616
except ImportError as e:
@@ -39,7 +39,7 @@ def main():
3939
# load the target class file and instantiate the object
4040
#
4141
try:
42-
m = imp.load_source('eeprom','/'.join([platform_path, 'plugins', 'eeprom.py']))
42+
m = SourceFileLoader('eeprom','/'.join([platform_path, 'plugins', 'eeprom.py'])).load_module()
4343
except IOError:
4444
raise IOError("cannot load module: " + '/'.join([platform_path, 'plugins', 'eeprom.py']))
4545

0 commit comments

Comments
 (0)