Skip to content

bpo-32108: Don't clear configparser values if key is assigned to itself #7588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,8 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
# To conform with the mapping protocol, overwrites existing values in
# the section.

if key in self and self[key] is value:
return
# XXX this is not atomic if read_dict fails at any point. Then again,
# no update method in configparser is atomic in this implementation.
if key == self.default_section:
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,12 +850,18 @@ def test_setitem(self):
self.assertEqual(set(cf['section3'].keys()), {'named'})
self.assertNotIn('name3', cf['section3'])
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
# For bpo-32108, assigning default_section to itself.
cf[self.default_section] = cf[self.default_section]
self.assertNotEqual(set(cf[self.default_section].keys()), set())
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())
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
# For bpo-32108, assigning section to itself.
cf['section2'] = cf['section2']
self.assertEqual(set(cf['section2'].keys()), {'name22'})

def test_invalid_multiline_value(self):
if self.allow_no_value:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In configparser, don't clear section when it is assigned to itself.