mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 19:01:26 -07:00
Add SEQUENCETYPE to handle the dict_keys type (#15953)
On python 3, there is a specific type for dict keys instead of list, so previous tests based on Sequence didn't not work anymore.
This commit is contained in:
parent
c8f0cdbdfd
commit
cf44db58e0
1 changed files with 8 additions and 2 deletions
|
@ -123,6 +123,12 @@ except ImportError:
|
||||||
Sequence = (list, tuple)
|
Sequence = (list, tuple)
|
||||||
Mapping = (dict,)
|
Mapping = (dict,)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from collections.abc import KeysView
|
||||||
|
SEQUENCETYPE = (Sequence, KeysView)
|
||||||
|
except:
|
||||||
|
SEQUENCETYPE = Sequence
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
# Detect the python-json library which is incompatible
|
# Detect the python-json library which is incompatible
|
||||||
|
@ -386,7 +392,7 @@ def return_values(obj):
|
||||||
# (still must deal with surrogateescape on python3)
|
# (still must deal with surrogateescape on python3)
|
||||||
yield obj.encode('utf-8')
|
yield obj.encode('utf-8')
|
||||||
return
|
return
|
||||||
elif isinstance(obj, Sequence):
|
elif isinstance(obj, SEQUENCETYPE):
|
||||||
for element in obj:
|
for element in obj:
|
||||||
for subelement in return_values(element):
|
for subelement in return_values(element):
|
||||||
yield subelement
|
yield subelement
|
||||||
|
@ -422,7 +428,7 @@ def remove_values(value, no_log_strings):
|
||||||
value = unicode(bytes_value, 'utf-8', errors='replace')
|
value = unicode(bytes_value, 'utf-8', errors='replace')
|
||||||
else:
|
else:
|
||||||
value = bytes_value
|
value = bytes_value
|
||||||
elif isinstance(value, Sequence):
|
elif isinstance(value, SEQUENCETYPE):
|
||||||
return [remove_values(elem, no_log_strings) for elem in value]
|
return [remove_values(elem, no_log_strings) for elem in value]
|
||||||
elif isinstance(value, Mapping):
|
elif isinstance(value, Mapping):
|
||||||
return dict((k, remove_values(v, no_log_strings)) for k, v in value.items())
|
return dict((k, remove_values(v, no_log_strings)) for k, v in value.items())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue