Skip to content

Commit

Permalink
[sonic_eeprom] Make compatible with Python 2 and 3 (sonic-net#127)
Browse files Browse the repository at this point in the history
The `string` type has changed between Python 2 and 3. Therefore, the Python 3 version of this library is broken.

This PR changes the data type of the raw EEPROM data to a `bytearray`, which is treated the same in both Python 2 and Python 3, and is technically a more appropriate data type for the raw bytes anyway.

However, this PR could cause issues if any vendors are overriding the `read_eeprom_bytes()` or `read_eeprom()` functions and returning a `string`. Any vendors doing so will need to refactor their implementation to return a `bytearray` instead.
  • Loading branch information
jleveque committed Oct 31, 2020
1 parent 012dc39 commit fc3c1a0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 113 deletions.
6 changes: 3 additions & 3 deletions sonic_platform_base/sonic_eeprom/eeprom_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ def compute_2s_complement(self, e, size):
def compute_dell_crc(self, message):
poly = 0x8005
reg = 0x0000
message += '\x00\x00'
message += bytearray(b'\x00\x00')
for byte in message:
mask = 0x80
while (mask > 0):
reg<<=1
if ord(byte) & mask:
if byte & mask:
reg += 1
mask>>=1
if reg > 0xffff:
Expand Down Expand Up @@ -265,7 +265,7 @@ def read_eeprom_bytes(self, byteCount, offset=0):
if F is not None:
F.close()

return o
return bytearray(o)

def read_eeprom_db(self):
return 0
Expand Down
Loading

0 comments on commit fc3c1a0

Please sign in to comment.