From e7831a801fadb93714fdb7e3b3a39928bb1f1188 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 20 Apr 2018 20:47:07 +0100 Subject: [PATCH] issue #195: handle non-ASCII scripts in runner.py. --- ansible_mitogen/runner.py | 13 +++++++++++-- tests/ansible/integration/runner/all.yml | 1 + .../runner/custom_script_interpreter.yml | 18 ++++++++++++++++++ .../modules/custom_bash_old_style_module.sh | 4 ++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/ansible/integration/runner/custom_script_interpreter.yml diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index 16a7d777..d5c2fd2b 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -61,6 +61,15 @@ ansible.module_utils.basic._ANSIBLE_ARGS = '{}' LOG = logging.getLogger(__name__) +def utf8(s): + """ + Coerce an object to bytes if it is Unicode. + """ + if isinstance(s, unicode): + s = s.encode('utf-8') + return s + + def reopen_readonly(fp): """ Replace the file descriptor belonging to the file object `fp` with one @@ -329,9 +338,9 @@ class ScriptRunner(ProgramRunner): if not self.interpreter: return s - shebang = '#!' + self.interpreter + shebang = '#!' + utf8(self.interpreter) if self.interpreter_arg: - shebang += ' ' + self.interpreter_arg + shebang += ' ' + utf8(self.interpreter_arg) new = [shebang] if os.path.basename(self.interpreter).startswith('python'): diff --git a/tests/ansible/integration/runner/all.yml b/tests/ansible/integration/runner/all.yml index b65b1a53..c3d95005 100644 --- a/tests/ansible/integration/runner/all.yml +++ b/tests/ansible/integration/runner/all.yml @@ -9,5 +9,6 @@ - import_playbook: custom_python_json_args_module.yml - import_playbook: custom_python_new_style_module.yml - import_playbook: custom_python_want_json_module.yml +- import_playbook: custom_script_interpreter.yml - import_playbook: forking_behaviour.yml - import_playbook: remote_tmp.yml diff --git a/tests/ansible/integration/runner/custom_script_interpreter.yml b/tests/ansible/integration/runner/custom_script_interpreter.yml new file mode 100644 index 00000000..9ad350bb --- /dev/null +++ b/tests/ansible/integration/runner/custom_script_interpreter.yml @@ -0,0 +1,18 @@ +- name: integration/runner/custom_script_interpreter.yml + hosts: all + any_errors_fatal: true + tasks: + + - custom_bash_old_style_module: + foo: true + with_sequence: start=1 end={{end|default(1)}} + register: out + vars: + ansible_bash_interpreter: /bin/bash + + - assert: + that: | + (not out.changed) and + (not out.results[0].changed) and + out.results[0].msg == 'Here is my input' + diff --git a/tests/ansible/lib/modules/custom_bash_old_style_module.sh b/tests/ansible/lib/modules/custom_bash_old_style_module.sh index 144789cb..9f80dc28 100755 --- a/tests/ansible/lib/modules/custom_bash_old_style_module.sh +++ b/tests/ansible/lib/modules/custom_bash_old_style_module.sh @@ -1,6 +1,10 @@ #!/bin/bash # I am an Ansible old-style module. +# This line is to encourage a UnicodeDecodeError in +# integration/runner/custom_script_interpreter.yml +# see https://github.com/dw/mitogen/issues/195 +# £££  INPUT=$1 [ ! -r "$INPUT" ] && {