summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2012-12-31 12:57:21 (GMT)
committerŁukasz Langa <lukasz@langa.pl>2012-12-31 12:57:21 (GMT)
commit8d518970ce30387db8168ea45402a871167562df (patch)
treea82df5f34c385b66c6841841a2688248aadf7a56
parent528b825cb1db4611c80ea2dff74b47eb71a4d029 (diff)
parent0210194d48569ce136bad4d324ade03767356905 (diff)
downloadcpython-8d518970ce30387db8168ea45402a871167562df.zip
cpython-8d518970ce30387db8168ea45402a871167562df.tar.gz
cpython-8d518970ce30387db8168ea45402a871167562df.tar.bz2
Merged `parser['DEFAULT'].__setitem__` fix (issue #16820) from 3.3.
-rw-r--r--Lib/configparser.py5
-rw-r--r--Lib/test/test_configparser.py28
2 files changed, 32 insertions, 1 deletions
diff --git a/Lib/configparser.py b/Lib/configparser.py
index c0272cd..a6ac059 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -960,7 +960,10 @@ class RawConfigParser(MutableMapping):
# XXX this is not atomic if read_dict fails at any point. Then again,
# no update method in configparser is atomic in this implementation.
- self.remove_section(key)
+ if key == self.default_section:
+ self._defaults.clear()
+ else:
+ self.remove_section(key)
self.read_dict({key: value})
def __delitem__(self, key):
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index 37dee74..3823641 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -797,6 +797,34 @@ boolean {0[0]} NO
self.assertEqual(set(cf.sections()), set())
self.assertEqual(set(cf[self.default_section].keys()), {'foo'})
+ def test_setitem(self):
+ cf = self.fromstring("""
+ [section1]
+ name1 {0[0]} value1
+ [section2]
+ name2 {0[0]} value2
+ [section3]
+ name3 {0[0]} value3
+ """.format(self.delimiters), defaults={"nameD": "valueD"})
+ self.assertEqual(set(cf['section1'].keys()), {'name1', 'named'})
+ self.assertEqual(set(cf['section2'].keys()), {'name2', 'named'})
+ self.assertEqual(set(cf['section3'].keys()), {'name3', 'named'})
+ self.assertEqual(cf['section1']['name1'], 'value1')
+ self.assertEqual(cf['section2']['name2'], 'value2')
+ self.assertEqual(cf['section3']['name3'], 'value3')
+ cf['section2'] = {'name22': 'value22'}
+ self.assertEqual(set(cf['section2'].keys()), {'name22', 'named'})
+ self.assertEqual(cf['section2']['name22'], 'value22')
+ self.assertNotIn('name2', cf['section2'])
+ cf['section3'] = {}
+ self.assertEqual(set(cf['section3'].keys()), {'named'})
+ self.assertNotIn('name3', cf['section3'])
+ cf[self.default_section] = {}
+ self.assertEqual(set(cf[self.default_section].keys()), set())
+ self.assertEqual(set(cf['section1'].keys()), {'name1'})
+ self.assertEqual(set(cf['section2'].keys()), {'name22'})
+ self.assertEqual(set(cf['section3'].keys()), set())
+
class StrictTestCase(BasicTestCase):
config_class = configparser.RawConfigParser