1
0
Fork 0
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:
Toshio Kuratomi 2016-08-24 07:52:36 -07:00 committed by GitHub
parent d7f1a66b03
commit 040a38171a

View file

@ -125,16 +125,16 @@ class TaskExecutor:
res['changed'] = False res['changed'] = False
def _clean_res(res): def _clean_res(res):
if isinstance(res, dict): if isinstance(res, UnsafeProxy):
for k in res.keys(): 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]) res[k] = _clean_res(res[k])
elif isinstance(res, list): elif isinstance(res, list):
for idx,item in enumerate(res): for idx,item in enumerate(res):
res[idx] = _clean_res(item) 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 return res
display.debug("dumping result to json") display.debug("dumping result to json")
@ -166,7 +166,7 @@ class TaskExecutor:
self._play_context.update_vars(play_context_vars) self._play_context.update_vars(play_context_vars)
old_vars = dict() old_vars = dict()
for k in play_context_vars.keys(): for k in play_context_vars:
if k in self._job_vars: if k in self._job_vars:
old_vars[k] = self._job_vars[k] old_vars[k] = self._job_vars[k]
self._job_vars[k] = play_context_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, # 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 # and delete them if they were in the play context vars but not in
# the old variables dictionary # the old variables dictionary
for k in play_context_vars.keys(): for k in play_context_vars:
if k in old_vars: if k in old_vars:
self._job_vars[k] = old_vars[k] self._job_vars[k] = old_vars[k]
else: else:
@ -616,7 +616,7 @@ class TaskExecutor:
if self._task.delegate_to is not None: if self._task.delegate_to is not None:
# since we're delegating, we don't want to use interpreter values # since we're delegating, we don't want to use interpreter values
# which would have been set for the original target host # 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'): if isinstance(i, string_types) and i.startswith('ansible_') and i.endswith('_interpreter'):
del variables[i] del variables[i]
# now replace the interpreter values with those that may have come # now replace the interpreter values with those that may have come