# HG changeset patch # User Franz Glasner # Date 1594153287 -7200 # Node ID 2a8dcab2de8c0d466ffcbde705acd9d6f7416dbb # Parent ff964825a75a7ead32097cd6280a89c21e1bd104 Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion. This is technically a breaking change. But no known client code uses the old behaviour. diff -r ff964825a75a -r 2a8dcab2de8c CHANGES.txt --- a/CHANGES.txt Tue Jul 07 09:27:24 2020 +0200 +++ b/CHANGES.txt Tue Jul 07 22:21:27 2020 +0200 @@ -13,6 +13,16 @@ -------------- .. changelog:: + :version: n/a + :released: not yet + + .. change:: + :tags: breaking, feature + + Do not implicitely convert a configuration value to text if the + value is the result of just a variable expansion. + +.. changelog:: :version: 0.7.4 :released: 2020-05-21 diff -r ff964825a75a -r 2a8dcab2de8c configmix/config.py --- a/configmix/config.py Tue Jul 07 09:27:24 2020 +0200 +++ b/configmix/config.py Tue Jul 07 22:21:27 2020 +0200 @@ -219,6 +219,12 @@ raise if varvalue is None: varvalue = u("") + # + # Dont apply and type conversions to str if the whole `s` is + # just one expansion + # + if (start == 0) and (end + 2 == len(s)): + return varvalue replaced = s[:start] + u(str(varvalue)) s = replaced + s[end+2:] # don't re-evaluate because `self.getvar_s()` expands already diff -r ff964825a75a -r 2a8dcab2de8c tests/data/conf1.ini --- a/tests/data/conf1.ini Tue Jul 07 09:27:24 2020 +0200 +++ b/tests/data/conf1.ini Tue Jul 07 22:21:27 2020 +0200 @@ -10,3 +10,11 @@ key6 = :int:0o377 __comment2 = Comment no 2 key7 = Umlaute: ÄÖÜäöüß +# Just expand to an int +key100 = {{key2}} +# Convert to a string because not alone +key101 = the {{key2}} value +# Recursion does not change the type also +key102 = {{key100}} +# Not alone -> string +key103 = {{key100}}{{key2}} diff -r ff964825a75a -r 2a8dcab2de8c tests/test.py --- a/tests/test.py Tue Jul 07 09:27:24 2020 +0200 +++ b/tests/test.py Tue Jul 07 22:21:27 2020 +0200 @@ -391,7 +391,7 @@ cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini"), os.path.join(TESTDATADIR, "conf30.conf")) self.assertRaises(KeyError, cfg.getvar_s, "key-new") - self.assertRaises(KeyError, cfg.getvar_s, "key1") + self.assertRaises(KeyError, cfg.getvar_s, "key1") def test03_only_style_corrrect_style(self): configmix.clear_assoc() @@ -431,5 +431,35 @@ self.assertRaises(KeyError, _ld) +class T05SubstituteExpand(unittest.TestCase): + + def setUp(self): + self._reset() + + def tearDown(self): + self._reset() + + def _reset(self): + configmix.clear_assoc() + for pat, fmode in configmix.DEFAULT_ASSOC: + configmix.set_assoc(pat, fmode) + + def test01_expand_int_ini(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini")) + self.assertEqual(2, cfg.getvar_s("key100")) + + def test02_expand_int_indirect_ini(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini")) + self.assertEqual(2, cfg.getvar_s("key102")) + + def test03_expand_int2str_ini(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini")) + self.assertEqual("the 2 value", cfg.getvar_s("key101")) + + def test04_expand_intint2str_ini(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini")) + self.assertEqual("22", cfg.getvar_s("key103")) + + if __name__ == "__main__": unittest.main()