From b12f0b581a6f268d0611c87012d1273aeca220b8 Mon Sep 17 00:00:00 2001 From: Facundo Batista Date: Sat, 23 Feb 2008 12:46:10 +0000 Subject: Issue 1781. Now ConfigParser.add_section does not let you add a DEFAULT section any more, because it duplicated sections with the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann. --- Doc/library/configparser.rst | 5 +++-- Lib/ConfigParser.py | 6 +++++- Lib/test/test_cfgparser.py | 8 ++++++++ Misc/NEWS | 3 +++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index ec3b022..96c001f 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -187,8 +187,9 @@ RawConfigParser Objects .. method:: RawConfigParser.add_section(section) Add a section named *section* to the instance. If a section by the given name - already exists, :exc:`DuplicateSectionError` is raised. - + already exists, :exc:`DuplicateSectionError` is raised. If the name + ``DEFAULT`` (or any of it's case-insensitive variants) is passed, + :exc:`ValueError` is raised. .. method:: RawConfigParser.has_section(section) diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py index 131d697..65c6ae2 100644 --- a/Lib/ConfigParser.py +++ b/Lib/ConfigParser.py @@ -235,8 +235,12 @@ class RawConfigParser: """Create a new section in the configuration. Raise DuplicateSectionError if a section by the specified name - already exists. + already exists. Raise ValueError if name is DEFAULT or any of it's + case-insensitive variants. """ + if section.lower() == "default": + raise ValueError, 'Invalid section name: %s' % section + if section in self._sections: raise DuplicateSectionError(section) self._sections[section] = self._dict() diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py index c4df741..a8b5d7c 100644 --- a/Lib/test/test_cfgparser.py +++ b/Lib/test/test_cfgparser.py @@ -446,6 +446,14 @@ class SafeConfigParserTestCase(ConfigParserTestCase): self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0) self.assertRaises(TypeError, cf.set, "sect", "option2", object()) + def test_add_section_default_1(self): + cf = self.newconfig() + self.assertRaises(ValueError, cf.add_section, "default") + + def test_add_section_default_2(self): + cf = self.newconfig() + self.assertRaises(ValueError, cf.add_section, "DEFAULT") + class SortedTestCase(RawConfigParserTestCase): def newconfig(self, defaults=None): self.cf = self.config_class(defaults=defaults, dict_type=SortedDict) diff --git a/Misc/NEWS b/Misc/NEWS index a33bf71..89f4c49 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -434,6 +434,9 @@ Core and builtins Library ------- +- Issue 1781: ConfigParser now does not let you add the "default" section + (ignore-case) + - Removed uses of dict.has_key() from distutils, and uses of callable() from copy_reg.py, so the interpreter now starts up without warnings when '-3' is given. More work like this needs to -- cgit v0.12