From cf44db58e051e9c18f15c5ef698cca065711dd9e Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Mon, 23 May 2016 21:17:28 +0200 Subject: [PATCH] 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. --- lib/ansible/module_utils/basic.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 0e95b5bb8f..a8ed1b3b46 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -123,6 +123,12 @@ except ImportError: Sequence = (list, tuple) Mapping = (dict,) +try: + from collections.abc import KeysView + SEQUENCETYPE = (Sequence, KeysView) +except: + SEQUENCETYPE = Sequence + try: import json # Detect the python-json library which is incompatible @@ -386,7 +392,7 @@ def return_values(obj): # (still must deal with surrogateescape on python3) yield obj.encode('utf-8') return - elif isinstance(obj, Sequence): + elif isinstance(obj, SEQUENCETYPE): for element in obj: for subelement in return_values(element): yield subelement @@ -422,7 +428,7 @@ def remove_values(value, no_log_strings): value = unicode(bytes_value, 'utf-8', errors='replace') else: value = bytes_value - elif isinstance(value, Sequence): + elif isinstance(value, SEQUENCETYPE): return [remove_values(elem, no_log_strings) for elem in value] elif isinstance(value, Mapping): return dict((k, remove_values(v, no_log_strings)) for k, v in value.items())