]> granicus.if.org Git - python/commitdiff
bpo-23835: Enforce that configparser defaults are strings (#2558)
authorJames Tocknell <aragilar+github@gmail.com>
Mon, 21 Aug 2017 22:46:30 +0000 (08:46 +1000)
committerŁukasz Langa <lukasz@langa.pl>
Mon, 21 Aug 2017 22:46:30 +0000 (15:46 -0700)
* Enforce that configparser defaults are strings
* Update test_configparser.py

Lib/configparser.py
Lib/test/test_configparser.py

index ea971f3933859a0c3cc15e4e66764e6038da18e4..360924008dcb7d619a6834e77ba12d732aa922de 100644 (file)
@@ -610,9 +610,6 @@ class RawConfigParser(MutableMapping):
         self._converters = ConverterMapping(self)
         self._proxies = self._dict()
         self._proxies[default_section] = SectionProxy(self, default_section)
-        if defaults:
-            for key, value in defaults.items():
-                self._defaults[self.optionxform(key)] = value
         self._delimiters = tuple(delimiters)
         if delimiters == ('=', ':'):
             self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
@@ -637,6 +634,8 @@ class RawConfigParser(MutableMapping):
             self._interpolation = Interpolation()
         if converters is not _UNSET:
             self._converters.update(converters)
+        if defaults:
+            self.read_dict({default_section: defaults})
 
     def defaults(self):
         return self._defaults
index 72c3f19fb41f569364d021e6af6cfec2210a46cf..be22fa4031075e939cae68b55ae932728fcc9f9a 100644 (file)
@@ -855,6 +855,15 @@ boolean {0[0]} NO
         self.assertEqual(cf.get('DEFAULT', 'test'), 'test')
         self.assertEqual(cf['DEFAULT']['test'], 'test')
 
+    def test_defaults_keyword(self):
+        # test that bpo-23835 is fixed
+        cf = self.newconfig(defaults={1: 2.4})
+        self.assertEqual(cf[self.default_section]['1'], '2.4')
+        self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.4)
+        cf = self.newconfig(defaults={"A": 5.2})
+        self.assertEqual(cf[self.default_section]['a'], '5.2')
+        self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.2)
+
 
 class StrictTestCase(BasicTestCase, unittest.TestCase):
     config_class = configparser.RawConfigParser