configparser: preserve section order when using `__setitem__` (issue #16820)

This commit is contained in:
Łukasz Langa 2013-01-01 22:33:19 +01:00
parent eb6aa5c525
commit a821f82f66
2 changed files with 6 additions and 2 deletions

View File

@ -962,8 +962,8 @@ class RawConfigParser(MutableMapping):
# no update method in configparser is atomic in this implementation. # no update method in configparser is atomic in this implementation.
if key == self.default_section: if key == self.default_section:
self._defaults.clear() self._defaults.clear()
else: elif key in self._sections:
self.remove_section(key) self._sections[key].clear()
self.read_dict({key: value}) self.read_dict({key: value})
def __delitem__(self, key): def __delitem__(self, key):

View File

@ -812,18 +812,22 @@ boolean {0[0]} NO
self.assertEqual(cf['section1']['name1'], 'value1') self.assertEqual(cf['section1']['name1'], 'value1')
self.assertEqual(cf['section2']['name2'], 'value2') self.assertEqual(cf['section2']['name2'], 'value2')
self.assertEqual(cf['section3']['name3'], 'value3') self.assertEqual(cf['section3']['name3'], 'value3')
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
cf['section2'] = {'name22': 'value22'} cf['section2'] = {'name22': 'value22'}
self.assertEqual(set(cf['section2'].keys()), {'name22', 'named'}) self.assertEqual(set(cf['section2'].keys()), {'name22', 'named'})
self.assertEqual(cf['section2']['name22'], 'value22') self.assertEqual(cf['section2']['name22'], 'value22')
self.assertNotIn('name2', cf['section2']) self.assertNotIn('name2', cf['section2'])
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
cf['section3'] = {} cf['section3'] = {}
self.assertEqual(set(cf['section3'].keys()), {'named'}) self.assertEqual(set(cf['section3'].keys()), {'named'})
self.assertNotIn('name3', cf['section3']) self.assertNotIn('name3', cf['section3'])
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
cf[self.default_section] = {} cf[self.default_section] = {}
self.assertEqual(set(cf[self.default_section].keys()), set()) self.assertEqual(set(cf[self.default_section].keys()), set())
self.assertEqual(set(cf['section1'].keys()), {'name1'}) self.assertEqual(set(cf['section1'].keys()), {'name1'})
self.assertEqual(set(cf['section2'].keys()), {'name22'}) self.assertEqual(set(cf['section2'].keys()), {'name22'})
self.assertEqual(set(cf['section3'].keys()), set()) self.assertEqual(set(cf['section3'].keys()), set())
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
class StrictTestCase(BasicTestCase): class StrictTestCase(BasicTestCase):