Skip to content

Commit 33cd058

Browse files
csabellaambv
authored andcommitted
bpo-32108: Don't clear configparser values if key is assigned to itself (GH-7588)
1 parent c3f55be commit 33cd058

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/configparser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,8 @@ def __getitem__(self, key):
963963
def __setitem__(self, key, value):
964964
# To conform with the mapping protocol, overwrites existing values in
965965
# the section.
966-
966+
if key in self and self[key] is value:
967+
return
967968
# XXX this is not atomic if read_dict fails at any point. Then again,
968969
# no update method in configparser is atomic in this implementation.
969970
if key == self.default_section:

Lib/test/test_configparser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,18 @@ def test_setitem(self):
850850
self.assertEqual(set(cf['section3'].keys()), {'named'})
851851
self.assertNotIn('name3', cf['section3'])
852852
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
853+
# For bpo-32108, assigning default_section to itself.
854+
cf[self.default_section] = cf[self.default_section]
855+
self.assertNotEqual(set(cf[self.default_section].keys()), set())
853856
cf[self.default_section] = {}
854857
self.assertEqual(set(cf[self.default_section].keys()), set())
855858
self.assertEqual(set(cf['section1'].keys()), {'name1'})
856859
self.assertEqual(set(cf['section2'].keys()), {'name22'})
857860
self.assertEqual(set(cf['section3'].keys()), set())
858861
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
862+
# For bpo-32108, assigning section to itself.
863+
cf['section2'] = cf['section2']
864+
self.assertEqual(set(cf['section2'].keys()), {'name22'})
859865

860866
def test_invalid_multiline_value(self):
861867
if self.allow_no_value:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In configparser, don't clear section when it is assigned to itself.

0 commit comments

Comments
 (0)