[Python3] Python3 migrate fix key error when parsing conn_graph_facts#7196
[Python3] Python3 migrate fix key error when parsing conn_graph_facts#7196lerry-lee wants to merge 10 commits intosonic-net:masterfrom
Conversation
1. In Python2, dict.keys() returns list object, but in Python3 returns an iterable but not indexable object. So that need to convert to list. 2. In Python2, the key in dev_conn.items() is unicode object, but in Python3 is AnsileUnsafeText. So that need to convert to str. Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
1. The Python "NameError name 'unicode' is not defined" occurs when using the unicode object in Python 3. To solve the error, replace all calls to unicode() with str() because unicode was renamed to str in Python 3. 2. In Python2, dict.keys() returns list object, but in Python3 returns an iterable but not indexable object. So that need to convert to list. Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
…g the unicode object in Python 3. To solve the error, replace all calls to unicode() with str() because unicode was renamed to str in Python 3. Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
…n iterable but not indexable object. So that need to convert to list. Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
tests/conftest.py
Outdated
| duthost = duthosts[dut_host] | ||
| # In Python2, the key in dev_conn.items() is unicode object, but in Python3 is AnsileUnsafeText. | ||
| # So that convert to str explicitly. | ||
| duthost = duthosts[str(dut_host)] |
There was a problem hiding this comment.
Other test scripts using this fixture also may run into such issue. Suggest to fix this issue from ansible/library/conn_graph_facts.py.
There was a problem hiding this comment.
Thanks for the good suggestion! But I tried to modify the ansible/library/conn_graph_facts.py like rename the key device_conn, but it didn't work:
results = {
'device_info': lab_graph.devices,
'device_conn': lab_graph.links,
'device_port_vlans': lab_graph.vlanport,
}
Currently, I found conn_graph_facts got from here in tests/common/fixtures/conn_graph_facts.py:
conn_graph_facts = localhost.conn_graph_facts(
**kargs)["ansible_facts"]
So, I added a method to make some converts after this conn_graph_fact was got if using Python3. This can also fix this issue.
conn_graph_facts = localhost.conn_graph_facts(
**kargs)["ansible_facts"]
return key_convert2str(conn_graph_facts)
def key_convert2str(conn_graph_facts):
"""
In Python2, some key type are unicode, but In Python3, are AnsibleUnsafeText. Convert them to str.
Currently, convert conn_graph_facts['device_conn'].
"""
# If Python2, do not change
if sys.version_info[0] < 3:
return conn_graph_facts
# Else, convert
result = copy.deepcopy(conn_graph_facts)
for key, value in conn_graph_facts['device_conn'].items():
result['device_conn'][str(key)] = result['device_conn'].pop(str(key))
return result
Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
|
|
||
|
|
||
| import sys | ||
| import copy |
There was a problem hiding this comment.
It would be better to import at the top of file.
| conn_graph_facts = localhost.conn_graph_facts( | ||
| **kargs)["ansible_facts"] | ||
| return conn_graph_facts | ||
| return key_convert2str(conn_graph_facts) |
There was a problem hiding this comment.
Fixing from pytest fixture side only resolves the issue we encountered in pytest.
The conn_graph_facts ansible module may be called in ansible or other places. It would be better to resolve the encoding issue directly from the conn_graph_facts ansible module located at sonic-mgmt/ansible/library/conn_graph_facts.py
|
New PR #7399 replace this. |
Description of PR
When running pc/test_lag_2.py, and some cases under directory
pfcwd/in Python3 environmet, there are some key errors like the below:Actually, it is caused by these codes (When parsing conn_graph_facts):
In Python2 env, the
dut_host(the key in dev_conn.items()) isunicodeobject, but in Python3 isAnsileUnsafeTextobject, so that adding convert to str explicitly can solve this issue.In Python2, dict.keys()/dict.values() returns list object, but in Python3 returns an iterable but not indexable object. So that need to convert to list.
The Python "NameError name 'unicode' is not defined" occurs when using the unicode object in Python 3. To solve the error, replace all calls to unicode() with str() because unicode was renamed to str in Python 3.
Type of change
Back port request
Approach
What is the motivation for this PR?
Python3 migrate fix key error.
How did you do it?
Add explicit convert.
How did you verify/test it?
Run TC and no key error.
Any platform specific information?
Supported testbed topology if it's a new test case?
Documentation