]> granicus.if.org Git - python/commitdiff
Closes #11324: ConfigParser(interpolation=None) doesn't work.
authorŁukasz Langa <lukasz@langa.pl>
Thu, 28 Apr 2011 15:03:45 +0000 (17:03 +0200)
committerŁukasz Langa <lukasz@langa.pl>
Thu, 28 Apr 2011 15:03:45 +0000 (17:03 +0200)
Initial patches by Tobias Brink. Thanks!

Lib/configparser.py
Lib/test/test_cfgparser.py
Misc/ACKS

index 0ecff701adc46824401bc900fbfdfdf366626e6a..82b6f03061f9c594957c8f0073743054332eb2e7 100644 (file)
@@ -623,11 +623,12 @@ class RawConfigParser(MutableMapping):
         self._strict = strict
         self._allow_no_value = allow_no_value
         self._empty_lines_in_values = empty_lines_in_values
-        if interpolation is _UNSET:
-            self._interpolation = self._DEFAULT_INTERPOLATION
-        else:
-            self._interpolation = interpolation
         self.default_section=default_section
+        self._interpolation = interpolation
+        if self._interpolation is _UNSET:
+            self._interpolation = self._DEFAULT_INTERPOLATION
+        if self._interpolation is None:
+            self._interpolation = Interpolation()
 
     def defaults(self):
         return self._defaults
index 299f37a107d5d7c7be76b75b99e7ab153e1492d9..a712b2b396e1c834aa1adac047bb23b32b639f7b 100644 (file)
@@ -864,6 +864,43 @@ class ConfigParserTestCase(BasicTestCase):
         cf = self.newconfig()
         self.assertRaises(ValueError, cf.add_section, self.default_section)
 
+
+class ConfigParserTestCaseNoInterpolation(BasicTestCase):
+    config_class = configparser.ConfigParser
+    interpolation = None
+    ini = textwrap.dedent("""
+        [numbers]
+        one = 1
+        two = %(one)s * 2
+        three = ${common:one} * 3
+
+        [hexen]
+        sixteen = ${numbers:two} * 8
+    """).strip()
+
+    def assertMatchesIni(self, cf):
+        self.assertEqual(cf['numbers']['one'], '1')
+        self.assertEqual(cf['numbers']['two'], '%(one)s * 2')
+        self.assertEqual(cf['numbers']['three'], '${common:one} * 3')
+        self.assertEqual(cf['hexen']['sixteen'], '${numbers:two} * 8')
+
+    def test_no_interpolation(self):
+        cf = self.fromstring(self.ini)
+        self.assertMatchesIni(cf)
+
+    def test_empty_case(self):
+        cf = self.newconfig()
+        self.assertIsNone(cf.read_string(""))
+
+    def test_none_as_default_interpolation(self):
+        class CustomConfigParser(configparser.ConfigParser):
+            _DEFAULT_INTERPOLATION = None
+
+        cf = CustomConfigParser()
+        cf.read_string(self.ini)
+        self.assertMatchesIni(cf)
+
+
 class ConfigParserTestCaseLegacyInterpolation(ConfigParserTestCase):
     config_class = configparser.ConfigParser
     interpolation = configparser.LegacyInterpolation()
@@ -1444,6 +1481,7 @@ def test_main():
         ConfigParserTestCaseNoValue,
         ConfigParserTestCaseExtendedInterpolation,
         ConfigParserTestCaseLegacyInterpolation,
+        ConfigParserTestCaseNoInterpolation,
         ConfigParserTestCaseTrickyFile,
         MultilineValuesTestCase,
         RawConfigParserTestCase,
index ac86ca2723f148c9e936c5d41cf17ff8e6081b64..c3f4e94ba9397c24a46c555d89eb6e645f7d3ffb 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -113,6 +113,7 @@ Terrence Brannon
 Brian Brazil
 Dave Brennan
 Tom Bridgman
+Tobias Brink
 Richard Brodie
 Michael Broghton
 Daniel Brotsky