Skip to content

Commit

Permalink
Issue python#21159: Improve message in configparser.InterpolationMiss…
Browse files Browse the repository at this point in the history
…ingOptionError.

Patch from Łukasz Langa.
  • Loading branch information
rbtcollins committed Aug 14, 2015
2 parents 7126eaa + f7a9267 commit 6908265
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
29 changes: 14 additions & 15 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,9 @@ class InterpolationMissingOptionError(InterpolationError):
"""A string substitution required a setting which was not available."""

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

def __init__(self, option, section, rawval):
msg = ("Value interpolation too deeply recursive:\n"
"\tsection: [%s]\n"
"\toption : %s\n"
"\trawval : %s\n"
% (section, option, rawval))
msg = ("Recursion limit exceeded in value substitution: option {!r} "
"in section {!r} contains an interpolation key which "
"cannot be substituted in {} steps. Raw value: {!r}"
"".format(option, section, MAX_INTERPOLATION_DEPTH,
rawval))
InterpolationError.__init__(self, option, section, msg)
self.args = (option, section, rawval)

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

def _interpolate_some(self, parser, option, accum, rest, section, map,
depth):
rawval = parser.get(section, option, raw=True, fallback=rest)
if depth > MAX_INTERPOLATION_DEPTH:
raise InterpolationDepthError(option, section, rest)
raise InterpolationDepthError(option, section, rawval)
while rest:
p = rest.find("%")
if p < 0:
Expand All @@ -432,7 +430,7 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
v = map[var]
except KeyError:
raise InterpolationMissingOptionError(
option, section, rest, var) from None
option, section, rawval, var) from None
if "%" in v:
self._interpolate_some(parser, option, accum, v,
section, map, depth + 1)
Expand Down Expand Up @@ -466,8 +464,9 @@ def before_set(self, parser, section, option, value):

def _interpolate_some(self, parser, option, accum, rest, section, map,
depth):
rawval = parser.get(section, option, raw=True, fallback=rest)
if depth > MAX_INTERPOLATION_DEPTH:
raise InterpolationDepthError(option, section, rest)
raise InterpolationDepthError(option, section, rawval)
while rest:
p = rest.find("$")
if p < 0:
Expand Down Expand Up @@ -504,7 +503,7 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
"More than one ':' found: %r" % (rest,))
except (KeyError, NoSectionError, NoOptionError):
raise InterpolationMissingOptionError(
option, section, rest, ":".join(path)) from None
option, section, rawval, ":".join(path)) from None
if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)),
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,8 @@ def test_interpolation(self):
"something with lots of interpolation (10 steps)")
e = self.get_error(cf, configparser.InterpolationDepthError, "Foo", "bar11")
if self.interpolation == configparser._UNSET:
self.assertEqual(e.args, ("bar11", "Foo", "%(with1)s"))
self.assertEqual(e.args, ("bar11", "Foo",
"something %(with11)s lots of interpolation (11 steps)"))
elif isinstance(self.interpolation, configparser.LegacyInterpolation):
self.assertEqual(e.args, ("bar11", "Foo",
"something %(with11)s lots of interpolation (11 steps)"))
Expand All @@ -861,7 +862,7 @@ def test_interpolation_missing_value(self):
self.assertEqual(e.option, "name")
if self.interpolation == configparser._UNSET:
self.assertEqual(e.args, ('name', 'Interpolation Error',
'', 'reference'))
'%(reference)s', 'reference'))
elif isinstance(self.interpolation, configparser.LegacyInterpolation):
self.assertEqual(e.args, ('name', 'Interpolation Error',
'%(reference)s', 'reference'))
Expand Down Expand Up @@ -1177,7 +1178,7 @@ def test_strange_options(self):
with self.assertRaises(exception_class) as cm:
cf['interpolated']['$trying']
self.assertEqual(cm.exception.reference, 'dollars:${sick')
self.assertEqual(cm.exception.args[2], '}') #rawval
self.assertEqual(cm.exception.args[2], '${dollars:${sick}}') #rawval

def test_case_sensitivity_basic(self):
ini = textwrap.dedent("""
Expand Down

0 comments on commit 6908265

Please sign in to comment.