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
This commit is contained in:
Andy McCrae 2018-04-23 12:45:08 +01:00
parent 6b6a9c6f76
commit fc1c9311a8
5 changed files with 39 additions and 24 deletions

View File

@ -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)

View File

@ -3,6 +3,7 @@ list_one:
- two
- three
- four
- 4
list_two:
- one
- two

View File

@ -1,5 +1,6 @@
list_one:
- four
- 4
list_two:
- one
- two

View File

@ -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]
[bar]

View File

@ -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 }}"