From fc1c9311a8e25fce011815f7d4a2dc7d41c99815 Mon Sep 17 00:00:00 2001 From: Andy McCrae Date: Mon, 23 Apr 2018 12:45:08 +0100 Subject: [PATCH] Fix py3 unicode issue unicode is nolonger a type in py3 - instead the str type is used for unicode, and the bytes type would cover the str type from py2. To avoid having to version the code, we should try to use unicode, except the NameError and continue as though it is py3 if that fails. Additionally, this patch adds a test that will fail if you revert the config_template.py back to it's original in py3. Closes-bug: 1763422 Change-Id: Ifda972caada27ade2d80f77b3df70568406226ff --- action/config_template.py | 22 +++++++++++------- tests/files/test_extend.yml.expected | 1 + tests/files/test_no_extend.yml.expected | 1 + tests/templates/test_with_comments.ini | 8 ++++++- tests/test.yml | 31 +++++++++++++------------ 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/action/config_template.py b/action/config_template.py index c8c7246..74efb9f 100644 --- a/action/config_template.py +++ b/action/config_template.py @@ -213,14 +213,20 @@ class ConfigTemplateParser(ConfigParser.RawConfigParser): if line[0].isspace() and cursect is not None and optname: value = line.strip() if value: - if isinstance(cursect[optname], (tuple, set)): - _temp_item = list(cursect[optname]) - del cursect[optname] - cursect[optname] = _temp_item - elif isinstance(cursect[optname], (str, unicode)): - _temp_item = [cursect[optname]] - del cursect[optname] - cursect[optname] = _temp_item + try: + if isinstance(cursect[optname], (tuple, set)): + _temp_item = list(cursect[optname]) + del cursect[optname] + cursect[optname] = _temp_item + elif isinstance(cursect[optname], (str, unicode)): + _temp_item = [cursect[optname]] + del cursect[optname] + cursect[optname] = _temp_item + except NameError: + if isinstance(cursect[optname], (bytes, str)): + _temp_item = [cursect[optname]] + del cursect[optname] + cursect[optname] = _temp_item cursect[optname].append(value) else: mo = self.SECTCRE.match(line) diff --git a/tests/files/test_extend.yml.expected b/tests/files/test_extend.yml.expected index c132a76..eda6a80 100644 --- a/tests/files/test_extend.yml.expected +++ b/tests/files/test_extend.yml.expected @@ -3,6 +3,7 @@ list_one: - two - three - four + - 4 list_two: - one - two diff --git a/tests/files/test_no_extend.yml.expected b/tests/files/test_no_extend.yml.expected index 4713768..7e79aa7 100644 --- a/tests/files/test_no_extend.yml.expected +++ b/tests/files/test_no_extend.yml.expected @@ -1,5 +1,6 @@ list_one: - four + - 4 list_two: - one - two diff --git a/tests/templates/test_with_comments.ini b/tests/templates/test_with_comments.ini index aa292fb..79d3644 100644 --- a/tests/templates/test_with_comments.ini +++ b/tests/templates/test_with_comments.ini @@ -2,8 +2,14 @@ # broken into multiple lines [DEFAULT] +# This tests the py3 unicode bug #1763422 +test_hosts = + +_unicode + 1 + string + [foo] #This is a comment baz = baz -[bar] \ No newline at end of file +[bar] diff --git a/tests/test.yml b/tests/test.yml index 1dfb17c..54c3269 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -222,17 +222,17 @@ foo: baz: "bar" section1: - key1: "value1" - key2: "value2" - key3: "value3" - key4: "value4" - key5: "value5" - key6: "value6" - key7: "value7" - key8: "value8" - key9: "value9" - key10: "value10" - key11: "value11" + key1: "String1" + key2: "string2" + key3: "string3" + key4: "string4" + key5: "string5" + key6: "string6" + key7: 1 + key8: 2 + key9: 3 + key10: 10 + key11: 11 section2: key1: "value1" section3: @@ -246,15 +246,16 @@ section7: key1: "value1" section8: - key1: "value1" + key1: 1 section9: - key1: "value1" + key1: 1 section10: - key1: "value1" + key1: 1 section11: - key1: "value1" + key1: 1 test_config_yml_overrides: list_one: - four + - 4 test_config_yml_hostvars_overrides: test_hostvar: "{{ ansible_default_ipv4.address }}"