mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Clean up task_executor for python3 (#17219)
ran task_executor through python-modernize and then made changes to the code pointed out by it: * Most places where we looped through dict.keys() changed to for key in dict: Using keys() in python2 creates a list() of keys. For iterating, we can iterate over the dict itself and we'll be handed back each key. In python3, doing it this way does not create a new list and thus is more memory efficient. * In one place, use: for key in list(dict.keys()): because we're deleting elements from the dictionary inside of the loop. So we really do need to iterate over a separate list of the keys to avoid modifying the dictionary that we're iterating over. (Fixes Python3 bug) * In one place, change the order of an if-elif-else tree so that the most frequent cases are evaluated first. (Optimization)
This commit is contained in:
parent
d7f1a66b03
commit
040a38171a
1 changed files with 9 additions and 9 deletions
|
@ -125,16 +125,16 @@ class TaskExecutor:
|
|||
res['changed'] = False
|
||||
|
||||
def _clean_res(res):
|
||||
if isinstance(res, dict):
|
||||
for k in res.keys():
|
||||
if isinstance(res, UnsafeProxy):
|
||||
return res._obj
|
||||
elif isinstance(res, binary_type):
|
||||
return to_unicode(res, errors='strict')
|
||||
elif isinstance(res, dict):
|
||||
for k in res:
|
||||
res[k] = _clean_res(res[k])
|
||||
elif isinstance(res, list):
|
||||
for idx,item in enumerate(res):
|
||||
res[idx] = _clean_res(item)
|
||||
elif isinstance(res, UnsafeProxy):
|
||||
return res._obj
|
||||
elif isinstance(res, binary_type):
|
||||
return to_unicode(res, errors='strict')
|
||||
return res
|
||||
|
||||
display.debug("dumping result to json")
|
||||
|
@ -166,7 +166,7 @@ class TaskExecutor:
|
|||
self._play_context.update_vars(play_context_vars)
|
||||
|
||||
old_vars = dict()
|
||||
for k in play_context_vars.keys():
|
||||
for k in play_context_vars:
|
||||
if k in self._job_vars:
|
||||
old_vars[k] = self._job_vars[k]
|
||||
self._job_vars[k] = play_context_vars[k]
|
||||
|
@ -206,7 +206,7 @@ class TaskExecutor:
|
|||
# now we restore any old job variables that may have been modified,
|
||||
# and delete them if they were in the play context vars but not in
|
||||
# the old variables dictionary
|
||||
for k in play_context_vars.keys():
|
||||
for k in play_context_vars:
|
||||
if k in old_vars:
|
||||
self._job_vars[k] = old_vars[k]
|
||||
else:
|
||||
|
@ -616,7 +616,7 @@ class TaskExecutor:
|
|||
if self._task.delegate_to is not None:
|
||||
# since we're delegating, we don't want to use interpreter values
|
||||
# which would have been set for the original target host
|
||||
for i in variables.keys():
|
||||
for i in list(variables.keys()):
|
||||
if isinstance(i, string_types) and i.startswith('ansible_') and i.endswith('_interpreter'):
|
||||
del variables[i]
|
||||
# now replace the interpreter values with those that may have come
|
||||
|
|
Loading…
Reference in a new issue