From c7be004c197ca8545635245ed955759d319a447b Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Wed, 30 Sep 2015 09:09:25 +0300 Subject: [PATCH] Python 3 has no dict.iteritems() (Reminder: cannot use six here, module_utils get shipped to remote machines that may not have six installed -- besides six doens't support Python 2.4.) --- lib/ansible/module_utils/basic.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 0995fdd7582..e5c53345049 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -88,6 +88,15 @@ try: except NameError: bytes = str # Python 2 +try: + dict.iteritems +except AttributeError: + def iteritems(d): # Python 3 + return d.items() +else: + def iteritems(d): # Python 2 + return d.iteritems() + try: import json @@ -299,7 +308,7 @@ def json_dict_unicode_to_bytes(d, encoding='utf-8'): if isinstance(d, unicode): return d.encode(encoding) elif isinstance(d, dict): - return dict(imap(json_dict_unicode_to_bytes, d.iteritems(), repeat(encoding))) + return dict(imap(json_dict_unicode_to_bytes, iteritems(d), repeat(encoding))) elif isinstance(d, list): return list(imap(json_dict_unicode_to_bytes, d, repeat(encoding))) elif isinstance(d, tuple): @@ -317,7 +326,7 @@ def json_dict_bytes_to_unicode(d, encoding='utf-8'): if isinstance(d, str): return unicode(d, encoding) elif isinstance(d, dict): - return dict(imap(json_dict_bytes_to_unicode, d.iteritems(), repeat(encoding))) + return dict(imap(json_dict_bytes_to_unicode, iteritems(d), repeat(encoding))) elif isinstance(d, list): return list(imap(json_dict_bytes_to_unicode, d, repeat(encoding))) elif isinstance(d, tuple): @@ -414,7 +423,7 @@ class AnsibleModule(object): self.aliases = {} if add_file_common_args: - for k, v in FILE_COMMON_ARGUMENTS.iteritems(): + for k, v in FILE_COMMON_ARGUMENTS.items(): if k not in self.argument_spec: self.argument_spec[k] = v @@ -950,7 +959,7 @@ class AnsibleModule(object): def _handle_aliases(self): aliases_results = {} #alias:canon - for (k,v) in self.argument_spec.iteritems(): + for (k,v) in self.argument_spec.items(): self._legal_inputs.append(k) aliases = v.get('aliases', None) default = v.get('default', None) @@ -971,7 +980,7 @@ class AnsibleModule(object): return aliases_results def _check_arguments(self, check_invalid_arguments): - for (k,v) in self.params.iteritems(): + for (k,v) in self.params.items(): if k == '_ansible_check_mode' and v: if not self.supports_check_mode: @@ -1023,7 +1032,7 @@ class AnsibleModule(object): def _check_required_arguments(self): ''' ensure all required arguments are present ''' missing = [] - for (k,v) in self.argument_spec.iteritems(): + for (k,v) in self.argument_spec.items(): required = v.get('required', False) if required and k not in self.params: missing.append(k) @@ -1046,7 +1055,7 @@ class AnsibleModule(object): def _check_argument_values(self): ''' ensure all arguments have the requested values, and there are no stray arguments ''' - for (k,v) in self.argument_spec.iteritems(): + for (k,v) in self.argument_spec.items(): choices = v.get('choices',None) if choices is None: continue @@ -1189,7 +1198,7 @@ class AnsibleModule(object): def _check_argument_types(self): ''' ensure all arguments have the requested type ''' - for (k, v) in self.argument_spec.iteritems(): + for (k, v) in self.argument_spec.items(): wanted = v.get('type', None) if wanted is None: continue @@ -1208,7 +1217,7 @@ class AnsibleModule(object): self.fail_json(msg="argument %s is of type %s and we were unable to convert to %s" % (k, type(value), wanted)) def _set_defaults(self, pre=True): - for (k,v) in self.argument_spec.iteritems(): + for (k,v) in self.argument_spec.items(): default = v.get('default', None) if pre == True: # this prevents setting defaults on required items