Skip to content

Commit 8664efc

Browse files
authored
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()`
1 parent ea59c0f commit 8664efc

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

sonic_platform_base/sonic_sfp/sfputilbase.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,16 @@ def _read_eeprom_specific_bytes(self, sysfsfile_eeprom, offset, num_bytes):
338338
return None
339339

340340
try:
341-
for n in range(0, num_bytes):
342-
eeprom_raw[n] = hex(ord(raw[n]))[2:].zfill(2)
341+
# in case raw is bytes (python3 is used) raw[n] will return int,
342+
# and in case raw is str(python2 is used) raw[n] will return str,
343+
# so for python3 the are no need to call ord to convert str to int.
344+
# TODO: Remove this check once we no longer support Python 2
345+
if type(raw) == bytes:
346+
for n in range(0, num_bytes):
347+
eeprom_raw[n] = hex(raw[n])[2:].zfill(2)
348+
else:
349+
for n in range(0, num_bytes):
350+
eeprom_raw[n] = hex(ord(raw[n]))[2:].zfill(2)
343351
except Exception:
344352
return None
345353

@@ -736,7 +744,7 @@ def read_port_to_i2cbus_mapping(self):
736744
i2cbus_list = []
737745
self.port_to_i2cbus_mapping = {}
738746
s = self.port_start
739-
for sfp_sysfs_path, attrs in sorted(self.eep_dict.iteritems()):
747+
for sfp_sysfs_path, attrs in sorted(self.eep_dict.items()):
740748
i2cbus = attrs.get("dev-id")
741749
if i2cbus is None:
742750
raise DeviceTreeError("No 'dev-id' attribute found in attr: %s" % repr(attrs))

0 commit comments

Comments
 (0)