fix interpolation of None value

interpolation use string.Template that always return str.
In most case, the caller convert the value to the option type.
But that doesn't work if the value is None.

This change make it works for None by replacing it by an empty string.

Closes-bug: #1654621
Change-Id: I9e318050d26277df99c9352dca70dc4ef4fbe13b
This commit is contained in:
Mehdi Abaakouk 2017-01-06 21:20:36 +01:00
parent 38246b6f4f
commit 71075cec20
2 changed files with 13 additions and 0 deletions

View File

@ -3210,6 +3210,8 @@ class ConfigOpts(collections.Mapping):
if isinstance(value, self.conf.GroupAttr):
raise TemplateSubstitutionError(
'substituting group %s not supported' % key)
if value is None:
return ''
return value

View File

@ -3503,6 +3503,17 @@ class SadPathTestCase(BaseTestCase):
def test_conf_file_bad_float(self):
self._do_test_conf_file_bad_value(cfg.FloatOpt)
def test_str_sub_none_value(self):
self.conf.register_cli_opt(cfg.StrOpt('oo'))
self.conf.register_cli_opt(cfg.StrOpt('bar', default='$oo'))
self.conf.register_cli_opt(cfg.StrOpt('barbar', default='foo $oo foo'))
self.conf([])
self.assertTrue(hasattr(self.conf, 'bar'))
self.assertEqual('', self.conf.bar)
self.assertEqual("foo foo", self.conf.barbar)
def test_str_sub_from_group(self):
self.conf.register_group(cfg.OptGroup('f'))
self.conf.register_cli_opt(cfg.StrOpt('oo', default='blaa'), group='f')