From 2bd0bbd4df4b5588f928ab157c874d586f2132fa Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 6 Mar 2019 14:03:32 +0000 Subject: [PATCH] issue #555: ansible: workaround ancient reload(sys) hack. This is the most minimal change for what might be relatively minimal edge case. Alternative is replacing reload(), but let's not do that yet. Closes #555 --- ansible_mitogen/runner.py | 12 +++++++++- docs/changelog.rst | 4 ++++ tests/ansible/integration/runner/all.yml | 1 + .../custom_python_prehistoric_module.yml | 10 ++++++++ .../custom_python_prehistoric_module.py | 23 +++++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/ansible/integration/runner/custom_python_prehistoric_module.yml create mode 100644 tests/ansible/lib/modules/custom_python_prehistoric_module.py diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index 04c70e78..30c36be7 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -40,6 +40,7 @@ import atexit import codecs import imp import os +import re import shlex import shutil import sys @@ -806,11 +807,20 @@ class NewStyleRunner(ScriptRunner): def _setup_args(self): pass + # issue #555: in old times it was considered good form to reload sys and + # change the default encoding. This hack was removed from Ansible long ago, + # but not before permeating into many third party modules. + PREHISTORIC_HACK_RE = re.compile( + b(r'reload\s*\(\s*sys\s*\)\s*' + r'sys\s*\.\s*setdefaultencoding\([^)]+\)') + ) + def _setup_program(self): - self.source = ansible_mitogen.target.get_small_file( + source = ansible_mitogen.target.get_small_file( context=self.service_context, path=self.path, ) + self.source = self.PREHISTORIC_HACK_RE.sub(b(''), source) def _get_code(self): try: diff --git a/docs/changelog.rst b/docs/changelog.rst index 165f8a45..11d3150f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -45,6 +45,9 @@ Fixes action plug-ins that invoked :func:`_make_tmp_path` repeatedly could trigger an assertion failure. +* `#555 `_: work around an old idiom + that reloaded :mod:`sys` in order to change the interpreter's default encoding. + * `ffae0355 `_: needless information was removed from the documentation and installation procedure. @@ -78,6 +81,7 @@ Thanks! Mitogen would not be possible without the support of users. A huge thanks for bug reports, testing, features and fixes in this release contributed by `Fabian Arrotin `_, +`Giles Westwood `_, `Matt Layman `_, `Percy Grunwald `_, `Petr Enkov `_, diff --git a/tests/ansible/integration/runner/all.yml b/tests/ansible/integration/runner/all.yml index dc23901f..19586547 100644 --- a/tests/ansible/integration/runner/all.yml +++ b/tests/ansible/integration/runner/all.yml @@ -12,6 +12,7 @@ - include: custom_python_json_args_module.yml - include: custom_python_new_style_missing_interpreter.yml - include: custom_python_new_style_module.yml +- include: custom_python_prehistoric_module.yml - include: custom_python_want_json_module.yml - include: custom_script_interpreter.yml - include: environment_isolation.yml diff --git a/tests/ansible/integration/runner/custom_python_prehistoric_module.yml b/tests/ansible/integration/runner/custom_python_prehistoric_module.yml new file mode 100644 index 00000000..458f3d2b --- /dev/null +++ b/tests/ansible/integration/runner/custom_python_prehistoric_module.yml @@ -0,0 +1,10 @@ +# issue #555 + +- name: integration/runner/custom_python_prehistoric_module.yml + hosts: test-targets + any_errors_fatal: true + tasks: + - custom_python_prehistoric_module: + register: out + + - assert: that=out.ok diff --git a/tests/ansible/lib/modules/custom_python_prehistoric_module.py b/tests/ansible/lib/modules/custom_python_prehistoric_module.py new file mode 100644 index 00000000..d44f51ec --- /dev/null +++ b/tests/ansible/lib/modules/custom_python_prehistoric_module.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# issue #555: I'm a module that cutpastes an old hack. + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.basic import get_module_path +from ansible.module_utils import six + +import os +import pwd +import socket +import sys + +import sys +reload(sys) +sys.setdefaultencoding('utf8') + + +def main(): + module = AnsibleModule(argument_spec={}) + module.exit_json(ok=True) + +if __name__ == '__main__': + main()