Skip to content

Commit

Permalink
Make sonic_sfp Python2 and Python3 compatible (#157)
Browse files Browse the repository at this point in the history
- Make `_read_eeprom_specific_bytes` Python3 and Python2 compatible
- Change a stray `self.eep_dict.iteritems()` call to `self.eep_dict.items()`
  • Loading branch information
msosyak authored Jan 5, 2021
1 parent ea59c0f commit 8664efc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sonic_platform_base/sonic_sfp/sfputilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 8664efc

Please sign in to comment.