Make sure ConfigParser uses .optionxform() consistently; this affects

.has_option(), .remove_option(), and .set().

This closes SF tracker #232913.
This commit is contained in:
Fred Drake 2001-02-26 21:55:34 +00:00
parent ffc215a279
commit 3c823aa4b6
3 changed files with 28 additions and 0 deletions

View File

@ -330,6 +330,7 @@ class ConfigParser:
elif not self.has_section(section): elif not self.has_section(section):
return 0 return 0
else: else:
option = self.optionxform(option)
return self.__sections[section].has_key(option) return self.__sections[section].has_key(option)
def set(self, section, option, value): def set(self, section, option, value):
@ -341,6 +342,7 @@ class ConfigParser:
sectdict = self.__sections[section] sectdict = self.__sections[section]
except KeyError: except KeyError:
raise NoSectionError(section) raise NoSectionError(section)
option = self.optionxform(option)
sectdict[option] = value sectdict[option] = value
def write(self, fp): def write(self, fp):
@ -368,6 +370,7 @@ class ConfigParser:
sectdict = self.__sections[section] sectdict = self.__sections[section]
except KeyError: except KeyError:
raise NoSectionError(section) raise NoSectionError(section)
option = self.optionxform(option)
existed = sectdict.has_key(option) existed = sectdict.has_key(option)
if existed: if existed:
del sectdict[option] del sectdict[option]

View File

@ -1,5 +1,6 @@
test_cfgparser test_cfgparser
Testing basic accessors... Testing basic accessors...
Testing case sensitivity...
Testing value interpolation... Testing value interpolation...
Testing parse errors... Testing parse errors...
Testing query interface... Testing query interface...

View File

@ -48,6 +48,29 @@ def basic(src):
" that never existed") " that never existed")
def case_sensitivity():
print "Testing case sensitivity..."
cf = ConfigParser.ConfigParser()
cf.add_section("A")
cf.add_section("a")
L = cf.sections()
L.sort()
verify(L == ["A", "a"])
cf.set("a", "B", "value")
verify(cf.options("a") == ["b"])
verify(cf.get("a", "b", raw=1) == "value",
"could not locate option, expecting case-insensitive option names")
verify(cf.has_option("a", "b"))
cf.set("A", "A-B", "A-B value")
for opt in ("a-b", "A-b", "a-B", "A-B"):
verify(cf.has_option("A", opt),
"has_option() returned false for option which should exist")
verify(cf.options("A") == ["a-b"])
verify(cf.options("a") == ["b"])
cf.remove_option("a", "B")
verify(cf.options("a") == [])
def interpolation(src): def interpolation(src):
print "Testing value interpolation..." print "Testing value interpolation..."
cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
@ -149,6 +172,7 @@ foo=Default
foo[en]=English foo[en]=English
foo[de]=Deutsch foo[de]=Deutsch
""") """)
case_sensitivity()
interpolation(r""" interpolation(r"""
[Foo] [Foo]
bar=something %(with1)s interpolation (1 step) bar=something %(with1)s interpolation (1 step)