Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sonic_sfp Python2 and Python3 compatible #157

Merged
merged 2 commits into from
Jan 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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