From 8664efc5472ae8cab04ad5a3499baa9a9c0d7f89 Mon Sep 17 00:00:00 2001 From: Myron Sosyak <49795530+msosyak@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:24:34 +0200 Subject: [PATCH] Make sonic_sfp Python2 and Python3 compatible (#157) - Make `_read_eeprom_specific_bytes` Python3 and Python2 compatible - Change a stray `self.eep_dict.iteritems()` call to `self.eep_dict.items()` --- sonic_platform_base/sonic_sfp/sfputilbase.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sonic_platform_base/sonic_sfp/sfputilbase.py b/sonic_platform_base/sonic_sfp/sfputilbase.py index 74c420bb0..3dcd31416 100644 --- a/sonic_platform_base/sonic_sfp/sfputilbase.py +++ b/sonic_platform_base/sonic_sfp/sfputilbase.py @@ -338,8 +338,16 @@ def _read_eeprom_specific_bytes(self, sysfsfile_eeprom, offset, num_bytes): return None try: - for n in range(0, num_bytes): - eeprom_raw[n] = hex(ord(raw[n]))[2:].zfill(2) + # in case raw is bytes (python3 is used) raw[n] will return int, + # and in case raw is str(python2 is used) raw[n] will return str, + # so for python3 the are no need to call ord to convert str to int. + # TODO: Remove this check once we no longer support Python 2 + if type(raw) == bytes: + for n in range(0, num_bytes): + eeprom_raw[n] = hex(raw[n])[2:].zfill(2) + else: + for n in range(0, num_bytes): + eeprom_raw[n] = hex(ord(raw[n]))[2:].zfill(2) except Exception: return None @@ -736,7 +744,7 @@ def read_port_to_i2cbus_mapping(self): i2cbus_list = [] self.port_to_i2cbus_mapping = {} s = self.port_start - for sfp_sysfs_path, attrs in sorted(self.eep_dict.iteritems()): + for sfp_sysfs_path, attrs in sorted(self.eep_dict.items()): i2cbus = attrs.get("dev-id") if i2cbus is None: raise DeviceTreeError("No 'dev-id' attribute found in attr: %s" % repr(attrs))