From 073f10a52afaa5bc84aafcdc6efd2cd8a7ca2801 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Wed, 14 Oct 2015 08:43:26 +0300 Subject: [PATCH 1/2] Python 3: don't convert module arguments to bytes Fixes a test failure: ====================================================================== ERROR: test_module_utils_basic_ansible_module_creation (units.module_utils.test_basic.TestModuleUtilsBasic) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/mg/src/ansible/test/units/module_utils/test_basic.py", line 250, in test_module_utils_basic_ansible_module_creation supports_check_mode=True, File "/home/mg/src/ansible/lib/ansible/module_utils/basic.py", line 470, in __init__ self._check_required_arguments() File "/home/mg/src/ansible/lib/ansible/module_utils/basic.py", line 1050, in _check_required_arguments self.fail_json(msg="missing required arguments: %s" % ",".join(missing)) File "/home/mg/src/ansible/lib/ansible/module_utils/basic.py", line 1445, in fail_json sys.exit(1) SystemExit: 1 -------------------- >> begin captured stdout << --------------------- {"msg": "missing required arguments: foo", "failed": true} because converting 'foo' to bytes yields b'foo' on Python 3, which doesn't match the native-unicode 'foo' argument spec. --- lib/ansible/module_utils/basic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 0da00053a0e..fbd1cdf4682 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1240,7 +1240,10 @@ class AnsibleModule(object): def _load_params(self): ''' read the input and return a dictionary and the arguments string ''' - params = json_dict_unicode_to_bytes(json.loads(MODULE_COMPLEX_ARGS)) + params = json.loads(MODULE_COMPLEX_ARGS) + if str is bytes: + # Python 2 + params = json_dict_unicode_to_bytes(params) if params is None: params = dict() return params From 225fe85530094efde0a37ad8f5a4d37ce050f26a Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Wed, 14 Oct 2015 09:32:26 +0300 Subject: [PATCH 2/2] Skip test_module_utils_basic_ansible_module_creation on Python 3 This reverts commit 073f10a52afaa5bc84aafcdc6efd2cd8a7ca2801 and instead disables the failing test. We're focusing on Python 3 support on the controller first; modules will come later. --- lib/ansible/module_utils/basic.py | 5 +---- test/units/module_utils/test_basic.py | 4 +++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index fbd1cdf4682..0da00053a0e 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1240,10 +1240,7 @@ class AnsibleModule(object): def _load_params(self): ''' read the input and return a dictionary and the arguments string ''' - params = json.loads(MODULE_COMPLEX_ARGS) - if str is bytes: - # Python 2 - params = json_dict_unicode_to_bytes(params) + params = json_dict_unicode_to_bytes(json.loads(MODULE_COMPLEX_ARGS)) if params is None: params = dict() return params diff --git a/test/units/module_utils/test_basic.py b/test/units/module_utils/test_basic.py index 78116847de5..8e3e8021801 100644 --- a/test/units/module_utils/test_basic.py +++ b/test/units/module_utils/test_basic.py @@ -21,6 +21,7 @@ from __future__ import (absolute_import, division) __metaclass__ = type import errno +import sys from six.moves import builtins @@ -220,7 +221,8 @@ class TestModuleUtilsBasic(unittest.TestCase): from ansible.module_utils.basic import get_module_path with patch('os.path.realpath', return_value='/path/to/foo/'): self.assertEqual(get_module_path(), '/path/to/foo') - + + @unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)") def test_module_utils_basic_ansible_module_creation(self): from ansible.module_utils import basic