Skip to content

Commit

Permalink
Fix Python3 str decode issues (sonic-net#14332)
Browse files Browse the repository at this point in the history
What is the motivation for this PR?
This fixes an issue due to Python 3 migration, where decode() is no longer a method of str.

How did you do it?
For cases where we actually need the unicode-escape conversion, I convert to bytes first, then re-decode to str to decode the escape sequences. In other cases where we are just taking the raw output of a command, I just removed the decode().

How did you verify/test it?
I ran the relevant sonic-mgmt tests and verified that the tests now pass and no longer raise exceptions due to decode() not being a method of str or AnsibleUnsafeText.
  • Loading branch information
patrickmacarthur authored Sep 26, 2024
1 parent 6fe4aec commit 3e8708d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/common/helpers/sonic_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_keys(self, table):
raise SonicDbKeyNotFound("No keys for %s found in sonic-db cmd: %s" % (table, cmd))
else:
if six.PY2:
return result['stdout'].decode('unicode-escape')
return result['stdout'].decode('unicode-escape').splitlines()
else:
return result['stdout'].splitlines()

Expand Down
10 changes: 6 additions & 4 deletions tests/voq/test_voq_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ def test_voq_local_port_create(duthosts, enum_frontend_dut_hostname, enum_asic_i

show_intf = asic.show_interface(command="status", include_internal_intfs=True)['ansible_facts']
for portkey in keylist:
portkey = portkey.decode('unicode-escape') # need to handle the hyphen in the inband port name
port_name = hostif_table[portkey]['value']["SAI_HOSTIF_ATTR_NAME"].decode('unicode-escape')
# need to handle the hyphen in the inband port name
portkey = bytes(portkey, encoding='ascii').decode('unicode-escape')
port_name = bytes(hostif_table[portkey]['value']["SAI_HOSTIF_ATTR_NAME"],
encoding='ascii').decode('unicode-escape')
port_state = hostif_table[portkey]['value']["SAI_HOSTIF_ATTR_OPER_STATUS"]
port_type = hostif_table[portkey]['value']["SAI_HOSTIF_ATTR_TYPE"]

Expand Down Expand Up @@ -206,8 +208,8 @@ def check_voq_interfaces(duthosts, per_host, asic, cfg_facts):
if porttype == 'hostif':
# find the hostif entry to get the physical port the router interface is on.
hostifkey = asicdb.find_hostif_by_portid(portid)
hostif = asicdb.get_hostif_table(refresh=False)[hostifkey][
'value']['SAI_HOSTIF_ATTR_NAME'].decode('unicode-escape')
hostif = bytes(asicdb.get_hostif_table(refresh=False)[hostifkey][
'value']['SAI_HOSTIF_ATTR_NAME'], encoding='ascii').decode('unicode-escape')
logger.info("RIF: %s is on local port: %s", rif, hostif)
rif_ports_in_asicdb.append(hostif)
if hostif not in dev_intfs and hostif not in voq_intfs:
Expand Down

0 comments on commit 3e8708d

Please sign in to comment.