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

Skip to content

Commit 6908265

Browse files
committed
Issue #21159: Improve message in configparser.InterpolationMissingOptionError.
Patch from Łukasz Langa.
2 parents 7126eaa + f7a9267 commit 6908265

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

Lib/configparser.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,9 @@ class InterpolationMissingOptionError(InterpolationError):
263263
"""A string substitution required a setting which was not available."""
264264

265265
def __init__(self, option, section, rawval, reference):
266-
msg = ("Bad value substitution:\n"
267-
"\tsection: [%s]\n"
268-
"\toption : %s\n"
269-
"\tkey : %s\n"
270-
"\trawval : %s\n"
271-
% (section, option, reference, rawval))
266+
msg = ("Bad value substitution: option {!r} in section {!r} contains "
267+
"an interpolation key {!r} which is not a valid option name. "
268+
"Raw value: {!r}".format(option, section, reference, rawval))
272269
InterpolationError.__init__(self, option, section, msg)
273270
self.reference = reference
274271
self.args = (option, section, rawval, reference)
@@ -286,11 +283,11 @@ class InterpolationDepthError(InterpolationError):
286283
"""Raised when substitutions are nested too deeply."""
287284

288285
def __init__(self, option, section, rawval):
289-
msg = ("Value interpolation too deeply recursive:\n"
290-
"\tsection: [%s]\n"
291-
"\toption : %s\n"
292-
"\trawval : %s\n"
293-
% (section, option, rawval))
286+
msg = ("Recursion limit exceeded in value substitution: option {!r} "
287+
"in section {!r} contains an interpolation key which "
288+
"cannot be substituted in {} steps. Raw value: {!r}"
289+
"".format(option, section, MAX_INTERPOLATION_DEPTH,
290+
rawval))
294291
InterpolationError.__init__(self, option, section, msg)
295292
self.args = (option, section, rawval)
296293

@@ -406,8 +403,9 @@ def before_set(self, parser, section, option, value):
406403

407404
def _interpolate_some(self, parser, option, accum, rest, section, map,
408405
depth):
406+
rawval = parser.get(section, option, raw=True, fallback=rest)
409407
if depth > MAX_INTERPOLATION_DEPTH:
410-
raise InterpolationDepthError(option, section, rest)
408+
raise InterpolationDepthError(option, section, rawval)
411409
while rest:
412410
p = rest.find("%")
413411
if p < 0:
@@ -432,7 +430,7 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
432430
v = map[var]
433431
except KeyError:
434432
raise InterpolationMissingOptionError(
435-
option, section, rest, var) from None
433+
option, section, rawval, var) from None
436434
if "%" in v:
437435
self._interpolate_some(parser, option, accum, v,
438436
section, map, depth + 1)
@@ -466,8 +464,9 @@ def before_set(self, parser, section, option, value):
466464

467465
def _interpolate_some(self, parser, option, accum, rest, section, map,
468466
depth):
467+
rawval = parser.get(section, option, raw=True, fallback=rest)
469468
if depth > MAX_INTERPOLATION_DEPTH:
470-
raise InterpolationDepthError(option, section, rest)
469+
raise InterpolationDepthError(option, section, rawval)
471470
while rest:
472471
p = rest.find("$")
473472
if p < 0:
@@ -504,7 +503,7 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
504503
"More than one ':' found: %r" % (rest,))
505504
except (KeyError, NoSectionError, NoOptionError):
506505
raise InterpolationMissingOptionError(
507-
option, section, rest, ":".join(path)) from None
506+
option, section, rawval, ":".join(path)) from None
508507
if "$" in v:
509508
self._interpolate_some(parser, opt, accum, v, sect,
510509
dict(parser.items(sect, raw=True)),

Lib/test/test_configparser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,8 @@ def test_interpolation(self):
847847
"something with lots of interpolation (10 steps)")
848848
e = self.get_error(cf, configparser.InterpolationDepthError, "Foo", "bar11")
849849
if self.interpolation == configparser._UNSET:
850-
self.assertEqual(e.args, ("bar11", "Foo", "%(with1)s"))
850+
self.assertEqual(e.args, ("bar11", "Foo",
851+
"something %(with11)s lots of interpolation (11 steps)"))
851852
elif isinstance(self.interpolation, configparser.LegacyInterpolation):
852853
self.assertEqual(e.args, ("bar11", "Foo",
853854
"something %(with11)s lots of interpolation (11 steps)"))
@@ -861,7 +862,7 @@ def test_interpolation_missing_value(self):
861862
self.assertEqual(e.option, "name")
862863
if self.interpolation == configparser._UNSET:
863864
self.assertEqual(e.args, ('name', 'Interpolation Error',
864-
'', 'reference'))
865+
'%(reference)s', 'reference'))
865866
elif isinstance(self.interpolation, configparser.LegacyInterpolation):
866867
self.assertEqual(e.args, ('name', 'Interpolation Error',
867868
'%(reference)s', 'reference'))
@@ -1177,7 +1178,7 @@ def test_strange_options(self):
11771178
with self.assertRaises(exception_class) as cm:
11781179
cf['interpolated']['$trying']
11791180
self.assertEqual(cm.exception.reference, 'dollars:${sick')
1180-
self.assertEqual(cm.exception.args[2], '}') #rawval
1181+
self.assertEqual(cm.exception.args[2], '${dollars:${sick}}') #rawval
11811182

11821183
def test_case_sensitivity_basic(self):
11831184
ini = textwrap.dedent("""

0 commit comments

Comments
 (0)