Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c823aa

Browse files
committed
Make sure ConfigParser uses .optionxform() consistently; this affects
.has_option(), .remove_option(), and .set(). This closes SF tracker #232913.
1 parent ffc215a commit 3c823aa

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

Lib/ConfigParser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ def has_option(self, section, option):
330330
elif not self.has_section(section):
331331
return 0
332332
else:
333+
option = self.optionxform(option)
333334
return self.__sections[section].has_key(option)
334335

335336
def set(self, section, option, value):
@@ -341,6 +342,7 @@ def set(self, section, option, value):
341342
sectdict = self.__sections[section]
342343
except KeyError:
343344
raise NoSectionError(section)
345+
option = self.optionxform(option)
344346
sectdict[option] = value
345347

346348
def write(self, fp):
@@ -368,6 +370,7 @@ def remove_option(self, section, option):
368370
sectdict = self.__sections[section]
369371
except KeyError:
370372
raise NoSectionError(section)
373+
option = self.optionxform(option)
371374
existed = sectdict.has_key(option)
372375
if existed:
373376
del sectdict[option]

Lib/test/output/test_cfgparser

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
test_cfgparser
22
Testing basic accessors...
3+
Testing case sensitivity...
34
Testing value interpolation...
45
Testing parse errors...
56
Testing query interface...

Lib/test/test_cfgparser.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ def basic(src):
4848
" that never existed")
4949

5050

51+
def case_sensitivity():
52+
print "Testing case sensitivity..."
53+
cf = ConfigParser.ConfigParser()
54+
cf.add_section("A")
55+
cf.add_section("a")
56+
L = cf.sections()
57+
L.sort()
58+
verify(L == ["A", "a"])
59+
cf.set("a", "B", "value")
60+
verify(cf.options("a") == ["b"])
61+
verify(cf.get("a", "b", raw=1) == "value",
62+
"could not locate option, expecting case-insensitive option names")
63+
verify(cf.has_option("a", "b"))
64+
cf.set("A", "A-B", "A-B value")
65+
for opt in ("a-b", "A-b", "a-B", "A-B"):
66+
verify(cf.has_option("A", opt),
67+
"has_option() returned false for option which should exist")
68+
verify(cf.options("A") == ["a-b"])
69+
verify(cf.options("a") == ["b"])
70+
cf.remove_option("a", "B")
71+
verify(cf.options("a") == [])
72+
73+
5174
def interpolation(src):
5275
print "Testing value interpolation..."
5376
cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
@@ -149,6 +172,7 @@ def expect_parse_error(exctype, src):
149172
foo[en]=English
150173
foo[de]=Deutsch
151174
""")
175+
case_sensitivity()
152176
interpolation(r"""
153177
[Foo]
154178
bar=something %(with1)s interpolation (1 step)

0 commit comments

Comments
 (0)