[Python-checkins] bpo-32108: Don't clear configparser values if key is assigned to itself (GH-7588)

Łukasz Langa webhook-mailer at python.org
Tue Jun 12 16:37:58 EDT 2018


https://github.com/python/cpython/commit/33cd058f21d0673253c88cea70388282918992bc
commit: 33cd058f21d0673253c88cea70388282918992bc
branch: master
author: Cheryl Sabella <cheryl.sabella at gmail.com>
committer: Łukasz Langa <lukasz at langa.pl>
date: 2018-06-12T13:37:51-07:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2018-06-10-12-15-26.bpo-32108.iEkvh0.rst
M Lib/configparser.py
M Lib/test/test_configparser.py

diff --git a/Lib/configparser.py b/Lib/configparser.py
index ea788aec5100..4a16101c7a3a 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -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:
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index f4df622050b9..f16da116a745 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -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:
diff --git a/Misc/NEWS.d/next/Library/2018-06-10-12-15-26.bpo-32108.iEkvh0.rst b/Misc/NEWS.d/next/Library/2018-06-10-12-15-26.bpo-32108.iEkvh0.rst
new file mode 100644
index 000000000000..154d88471f6a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-06-10-12-15-26.bpo-32108.iEkvh0.rst
@@ -0,0 +1 @@
+In configparser, don't clear section when it is assigned to itself.



More information about the Python-checkins mailing list