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

Skip to content

Commit e698cd5

Browse files
committed
Closes #11858: configparser.ExtendedInterpolation and section case.
Patch by ゆかり ぴんく魔女. Thanks!
1 parent 8a410d3 commit e698cd5

2 files changed

Lines changed: 72 additions & 6 deletions

File tree

Lib/configparser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,17 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
481481
if m is None:
482482
raise InterpolationSyntaxError(option, section,
483483
"bad interpolation variable reference %r" % rest)
484-
path = parser.optionxform(m.group(1)).split(':')
484+
path = m.group(1).split(':')
485485
rest = rest[m.end():]
486486
sect = section
487487
opt = option
488488
try:
489489
if len(path) == 1:
490-
opt = path[0]
490+
opt = parser.optionxform(path[0])
491491
v = map[opt]
492492
elif len(path) == 2:
493493
sect = path[0]
494-
opt = path[1]
494+
opt = parser.optionxform(path[1])
495495
v = parser.get(sect, opt, raw=True)
496496
else:
497497
raise InterpolationSyntaxError(
@@ -1056,6 +1056,8 @@ def _read(self, fp, fpname):
10561056
if not optname:
10571057
e = self._handle_error(e, fpname, lineno, line)
10581058
optname = self.optionxform(optname.rstrip())
1059+
if hasattr(self, '__ping__'):
1060+
import pdb; pdb.set_trace()
10591061
if (self._strict and
10601062
(sectname, optname) in elements_added):
10611063
raise DuplicateOptionError(sectname, optname,

Lib/test/test_cfgparser.py

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ def keys(self):
2020
def values(self):
2121
return [i[1] for i in self.items()]
2222

23-
def iteritems(self): return iter(self.items())
24-
def iterkeys(self): return iter(self.keys())
23+
def iteritems(self):
24+
return iter(self.items())
25+
26+
def iterkeys(self):
27+
return iter(self.keys())
28+
29+
def itervalues(self):
30+
return iter(self.values())
31+
2532
__iter__ = iterkeys
26-
def itervalues(self): return iter(self.values())
2733

2834

2935
class CfgParserTestCaseClass(unittest.TestCase):
@@ -986,6 +992,14 @@ class ConfigParserTestCaseExtendedInterpolation(BasicTestCase):
986992
config_class = configparser.ConfigParser
987993
interpolation = configparser.ExtendedInterpolation()
988994
default_section = 'common'
995+
strict = True
996+
997+
def fromstring(self, string, defaults=None, optionxform=None):
998+
cf = self.newconfig(defaults)
999+
if optionxform:
1000+
cf.optionxform = optionxform
1001+
cf.read_string(string)
1002+
return cf
9891003

9901004
def test_extended_interpolation(self):
9911005
cf = self.fromstring(textwrap.dedent("""
@@ -1070,6 +1084,56 @@ def test_strange_options(self):
10701084
self.assertEqual(cm.exception.reference, 'dollars:${sick')
10711085
self.assertEqual(cm.exception.args[2], '}') #rawval
10721086

1087+
def test_case_sensitivity_basic(self):
1088+
ini = textwrap.dedent("""
1089+
[common]
1090+
optionlower = value
1091+
OptionUpper = Value
1092+
1093+
[Common]
1094+
optionlower = a better ${common:optionlower}
1095+
OptionUpper = A Better ${common:OptionUpper}
1096+
1097+
[random]
1098+
foolower = ${common:optionlower} redefined
1099+
FooUpper = ${Common:OptionUpper} Redefined
1100+
""").strip()
1101+
1102+
cf = self.fromstring(ini)
1103+
eq = self.assertEqual
1104+
eq(cf['common']['optionlower'], 'value')
1105+
eq(cf['common']['OptionUpper'], 'Value')
1106+
eq(cf['Common']['optionlower'], 'a better value')
1107+
eq(cf['Common']['OptionUpper'], 'A Better Value')
1108+
eq(cf['random']['foolower'], 'value redefined')
1109+
eq(cf['random']['FooUpper'], 'A Better Value Redefined')
1110+
1111+
def test_case_sensitivity_conflicts(self):
1112+
ini = textwrap.dedent("""
1113+
[common]
1114+
option = value
1115+
Option = Value
1116+
1117+
[Common]
1118+
option = a better ${common:option}
1119+
Option = A Better ${common:Option}
1120+
1121+
[random]
1122+
foo = ${common:option} redefined
1123+
Foo = ${Common:Option} Redefined
1124+
""").strip()
1125+
with self.assertRaises(configparser.DuplicateOptionError):
1126+
cf = self.fromstring(ini)
1127+
1128+
# raw options
1129+
cf = self.fromstring(ini, optionxform=lambda opt: opt)
1130+
eq = self.assertEqual
1131+
eq(cf['common']['option'], 'value')
1132+
eq(cf['common']['Option'], 'Value')
1133+
eq(cf['Common']['option'], 'a better value')
1134+
eq(cf['Common']['Option'], 'A Better Value')
1135+
eq(cf['random']['foo'], 'value redefined')
1136+
eq(cf['random']['Foo'], 'A Better Value Redefined')
10731137

10741138
def test_other_errors(self):
10751139
cf = self.fromstring("""

0 commit comments

Comments
 (0)