From 9935fca570a0c99be326049a9d671a2c218ae8e5 Mon Sep 17 00:00:00 2001 From: Andriy Kokhan <43479230+akokhan@users.noreply.github.com> Date: Tue, 15 Dec 2020 19:41:07 +0200 Subject: [PATCH] Fixed eeprom_tlvinfo.py to make it Python2/Python3 compatible (#155) This PR fixes eeprom_tlvinfo.py to make it compatible with both Python2 & Python3. The current implementation causes runtime issue as follows: ``` Dec 14 12:04:34.908841 sonic WARNING pmon#syseepromd[51]: Failed to load platform-specific eeprom from sonic_platform package due to UnicodeDecodeError('ascii', '*\x02\x00\x8c', 3, 4, 'ordinal not in range(128)') Dec 14 12:04:34.910093 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet56 admin status to up Dec 14 12:04:34.923442 sonic ERR pmon#syseepromd[51]: Failed to load platform-specific eeprom implementation: UnicodeDecodeError('ascii', '*\x02\x00\x8c', 3, 4, 'ordinal not in range(128)') Dec 14 12:04:34.925628 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet60 MTU to 9100 Dec 14 12:04:34.939277 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet60 fec to rs Dec 14 12:04:34.943819 sonic NOTICE swss#orchagent: :- doPortTask: Set port Ethernet60 admin status to up ``` This cause `syseepromd` crash: ``` Dec 14 12:04:43.194489 sonic INFO pmon#supervisord 2020-12-14 12:04:34,651 INFO spawned: 'syseepromd' with pid 51 Dec 14 12:04:43.194489 sonic INFO pmon#supervisord 2020-12-14 12:04:34,955 INFO exited: syseepromd (exit status 5; not expected) ``` Signed-off-by: Andriy Kokhan --- .../sonic_eeprom/eeprom_tlvinfo.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py b/sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py index 4ace83e8bd..2a6e18dfe5 100644 --- a/sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py +++ b/sonic_platform_base/sonic_eeprom/eeprom_tlvinfo.py @@ -202,16 +202,16 @@ def set_eeprom(self, e, cmd_args): if self._TLV_HDR_ENABLED: new_tlvs_len = len(new_tlvs) + 6 - new_e = self._TLV_INFO_ID_STRING + bytes([self._TLV_INFO_VERSION]) + \ - bytes([(new_tlvs_len >> 8) & 0xFF]) + \ - bytes([new_tlvs_len & 0xFF]) + new_tlvs + new_e = self._TLV_INFO_ID_STRING + bytearray([self._TLV_INFO_VERSION]) + \ + bytearray([(new_tlvs_len >> 8) & 0xFF]) + \ + bytearray([new_tlvs_len & 0xFF]) + new_tlvs else: new_e = new_tlvs if self._TLV_CODE_CRC_32 != self._TLV_CODE_UNDEFINED: - new_e = new_e + bytes([self._TLV_CODE_CRC_32]) + bytes([4]) + new_e = new_e + bytearray([self._TLV_CODE_CRC_32]) + bytearray([4]) elif self._TLV_CODE_QUANTA_CRC != self._TLV_CODE_UNDEFINED: - new_e = new_e + bytes([self._TLV_CODE_QUANTA_CRC]) + bytes([2]) + new_e = new_e + bytearray([self._TLV_CODE_QUANTA_CRC]) + bytearray([2]) else: print("\nFailed to formulate new eeprom\n") exit @@ -624,19 +624,19 @@ def encoder(self, I, v): errstr = "A string less than 256 characters" if len(v) > 255: raise - value = v + value = v.encode("ascii", "replace") elif I[0] == self._TLV_CODE_DEVICE_VERSION: errstr = "A number between 0 and 255" num = int(v, 0) if num < 0 or num > 255: raise - value = chr(num) + value = bytearray([num]) elif I[0] == self._TLV_CODE_MAC_SIZE: errstr = "A number between 0 and 65535" num = int(v, 0) if num < 0 or num > 65535: raise - value = chr((num >> 8) & 0xFF) + chr(num & 0xFF) + value = bytearray([(num >> 8) & 0xFF, num & 0xFF]) elif I[0] == self._TLV_CODE_MANUF_DATE: errstr = 'MM/DD/YYYY HH:MM:SS' date, time = v.split() @@ -646,22 +646,22 @@ def encoder(self, I, v): mo < 1 or mo > 12 or da < 1 or da > 31 or yr < 0 or yr > 9999 or \ hr < 0 or hr > 23 or mn < 0 or mn > 59 or sc < 0 or sc > 59: raise - value = v + value = v.encode("ascii", "replace") elif I[0] == self._TLV_CODE_MAC_BASE: errstr = 'XX:XX:XX:XX:XX:XX' mac_digits = v.split(':') if len(mac_digits) != 6: raise - value = "" + value = bytearray() for c in mac_digits: - value = value + chr(int(c, 16)) + value += bytearray([int(c, 16)]) elif I[0] == self._TLV_CODE_MANUF_COUNTRY: errstr = 'CC, a two character ISO 3166-1 alpha-2 country code' if len(v) < 2: raise - value = v[0:2] + value = v.encode("ascii", "replace") elif I[0] == self._TLV_CODE_CRC_32: - value = '' + value = bytearray() # Disallow setting any Quanta specific codes elif I[0] == self._TLV_CODE_QUANTA_MAGIC or \ I[0] == self._TLV_CODE_QUANTA_CARD_TYPE or \ @@ -672,9 +672,9 @@ def encoder(self, I, v): raise Exception('quanta-read-only') else: errstr = '0xXX ... A list of space-separated hexidecimal numbers' - value = "" + value = bytearray() for c in v.split(): - value += chr(int(c, 0)) + value += bytearray([int(c, 0)]) except Exception as inst: if (len(inst.args) > 0) and (inst.args[0] == 'quanta-read-only'): sys.stderr.write("Error: '" + "0x%02X" % (I[0],) + "' -- Unable to set the read-only Quanta codes.\n") @@ -682,7 +682,7 @@ def encoder(self, I, v): sys.stderr.write("Error: '" + "0x%02X" % (I[0],) + "' correct format is " + errstr + "\n") exit(0) - return (chr(I[0]) + chr(len(value)) + value).encode() + return bytearray([I[0]]) + bytearray([len(value)]) + value def is_checksum_field(self, I):