[Python-checkins] cpython (merge 3.3 -> default): Merged `parser.clean()` fix (issue #16820) from 3.2 through 3.3.

lukasz.langa python-checkins at python.org
Mon Dec 31 03:44:14 CET 2012


http://hg.python.org/cpython/rev/c6f9bc5a0cf1
changeset:   81167:c6f9bc5a0cf1
parent:      81163:b4975cb4be95
parent:      81166:4fc2fea807e6
user:        Łukasz Langa <lukasz at langa.pl>
date:        Mon Dec 31 03:43:37 2012 +0100
summary:
  Merged `parser.clean()` fix (issue #16820) from 3.2 through 3.3.

files:
  Doc/library/configparser.rst  |   8 +++++-
  Lib/configparser.py           |  13 +++++++++++
  Lib/test/test_configparser.py |  27 +++++++++++++++++++++++
  Misc/ACKS                     |   1 +
  4 files changed, 48 insertions(+), 1 deletions(-)


diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst
--- a/Doc/library/configparser.rst
+++ b/Doc/library/configparser.rst
@@ -389,7 +389,13 @@
   the default value to be visible again.  Trying to delete a default value
   causes a ``KeyError``.
 
-* Trying to delete the ``DEFAULTSECT`` raises ``ValueError``.
+* ``DEFAULTSECT`` cannot be removed from the parser:
+
+  * trying to delete it raises ``ValueError``,
+
+  * ``parser.clear()`` leaves it intact,
+
+  * ``parser.popitem()`` never returns it.
 
 * ``parser.get(section, option, **kwargs)`` - the second argument is **not**
   a fallback value. Note however that the section-level ``get()`` methods are
diff --git a/Lib/configparser.py b/Lib/configparser.py
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -852,6 +852,19 @@
             value_getter = lambda option: d[option]
         return [(option, value_getter(option)) for option in d.keys()]
 
+    def popitem(self):
+        """Remove a section from the parser and return it as
+        a (section_name, section_proxy) tuple. If no section is present, raise
+        KeyError.
+
+        The section DEFAULT is never returned because it cannot be removed.
+        """
+        for key in self.sections():
+            value = self[key]
+            del self[key]
+            return key, value
+        raise KeyError
+
     def optionxform(self, optionstr):
         return optionstr.lower()
 
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -770,6 +770,33 @@
         with self.assertRaises(configparser.NoSectionError):
             cf.items("no such section")
 
+    def test_popitem(self):
+        cf = self.fromstring("""
+            [section1]
+            name1 {0[0]} value1
+            [section2]
+            name2 {0[0]} value2
+            [section3]
+            name3 {0[0]} value3
+        """.format(self.delimiters), defaults={"default": "<default>"})
+        self.assertEqual(cf.popitem()[0], 'section1')
+        self.assertEqual(cf.popitem()[0], 'section2')
+        self.assertEqual(cf.popitem()[0], 'section3')
+        with self.assertRaises(KeyError):
+            cf.popitem()
+
+    def test_clear(self):
+        cf = self.newconfig({"foo": "Bar"})
+        self.assertEqual(
+            cf.get(self.default_section, "Foo"), "Bar",
+            "could not locate option, expecting case-insensitive option names")
+        cf['zing'] = {'option1': 'value1', 'option2': 'value2'}
+        self.assertEqual(cf.sections(), ['zing'])
+        self.assertEqual(set(cf['zing'].keys()), {'option1', 'option2', 'foo'})
+        cf.clear()
+        self.assertEqual(set(cf.sections()), set())
+        self.assertEqual(set(cf[self.default_section].keys()), {'foo'})
+
 
 class StrictTestCase(BasicTestCase):
     config_class = configparser.RawConfigParser
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1057,6 +1057,7 @@
 Andreas Schawo
 Neil Schemenauer
 David Scherer
+Wolfgang Scherer
 Hynek Schlawack
 Bob Schmertz
 Gregor Schmid

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list