From ffc9b693fef40f98710dd52318d485a3b898fa7c Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Tue, 24 Nov 2026 21:58:05 -0600 Subject: [PATCH 1/4] Reduce the complexity of _interpolate_some In configparser, interpolation -- both basic and extended -- follows a pattern in which an interpolation delimiter is used to detect the start of a match, and then the suite under that guard: 1. scans for the next interpolation match 2. errors if there was no match 3. slices the remainder string (thus advancing the overall state of the string scanning at work) 4. pulls out the reference information for what values to interpolate 5. looks up that reference information 6. recurses, doing further interpolations on the result We can bundle together the responsibilities of 1-5 of these steps into a new helper, `_do_one_interpolation()`, with the same shape for both basic and extended interpolation (basic interpolation will not use all of the return values) to improve readability. As an included readability change, the return values `opt` and `sect` (used in extended interpolation) are renamed to `ref_option` and `ref_section`, in order to better disambiguate them from the originating `option` and `section` variables, which are necessarily handled in the same scope. --- Lib/configparser.py | 118 ++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index b7a0b0b56893c5..40f930284254fa 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -486,21 +486,9 @@ def _interpolate_some(self, parser, option, accum, rest, section, map, depth): accum.append("%") rest = rest[2:] elif c == "(": - m = self._KEYCRE.match(rest) - if m is None: - raise InterpolationSyntaxError( - option, - section, - f"bad interpolation variable reference {rest!r}", - ) - var = parser.optionxform(m.group(1)) - rest = rest[m.end() :] - try: - v = map[var] - except KeyError: - raise InterpolationMissingOptionError( - option, section, rawval, var - ) from None + v, rest, _, _ = self._do_one_interpolation( + rest, rawval, parser, section, option, map + ) if "%" in v: self._interpolate_some( parser, option, accum, v, section, map, depth + 1 @@ -514,6 +502,29 @@ def _interpolate_some(self, parser, option, accum, rest, section, map, depth): f"'%' must be followed by '%' or '(', found: {rest!r}", ) + def _do_one_interpolation( + self, value, rawval, parser, section, option, map + ): + m = self._KEYCRE.match(value) + if m is None: + raise InterpolationSyntaxError( + option, + section, + f"bad interpolation variable reference {value!r}", + ) + + remainder = value[m.end() :] + ref_section = section + ref_option = parser.optionxform(m.group(1)) + try: + result = map[ref_option] + except KeyError: + raise InterpolationMissingOptionError( + option, section, rawval, ref_option + ) from None + + return (result, remainder, ref_section, ref_option) + class ExtendedInterpolation(Interpolation): """Advanced variant of interpolation, supports the syntax used by @@ -537,14 +548,7 @@ def before_set(self, parser, section, option, value): return value def _interpolate_some( - self, - parser, - option, # noqa: C901 # gh-159686 - accum, - rest, - section, - map, - depth, + self, parser, option, accum, rest, section, map, depth ): rawval = parser.get(section, option, raw=True, fallback=rest) if depth > MAX_INTERPOLATION_DEPTH: @@ -563,43 +567,19 @@ def _interpolate_some( accum.append("$") rest = rest[2:] elif c == "{": - m = self._KEYCRE.match(rest) - if m is None: - raise InterpolationSyntaxError( - option, - section, - f"bad interpolation variable reference {rest!r}", - ) - path = m.group(1).split(':') - rest = rest[m.end() :] - sect = section - opt = option - try: - if len(path) == 1: - opt = parser.optionxform(path[0]) - v = map[opt] - elif len(path) == 2: - sect = path[0] - opt = parser.optionxform(path[1]) - v = parser.get(sect, opt, raw=True) - else: - raise InterpolationSyntaxError( - option, section, f"More than one ':' found: {rest!r}" - ) - except (KeyError, NoSectionError, NoOptionError): - raise InterpolationMissingOptionError( - option, section, rawval, ":".join(path) - ) from None + v, rest, ref_section, ref_option = self._do_one_interpolation( + rest, rawval, parser, section, option, map + ) if v is None: continue if "$" in v: self._interpolate_some( parser, - opt, + ref_option, accum, v, - sect, - dict(parser.items(sect, raw=True)), + ref_section, + dict(parser.items(ref_section, raw=True)), depth + 1, ) else: @@ -611,6 +591,38 @@ def _interpolate_some( f"'$' must be followed by '$' or '{{', found: {rest!r}", ) + def _do_one_interpolation( + self, value, rawval, parser, section, option, map + ): + m = self._KEYCRE.match(value) + if m is None: + raise InterpolationSyntaxError( + option, + section, + f"bad interpolation variable reference {value!r}", + ) + + path = m.group(1).split(":") + remainder = value[m.end() :] + try: + if len(path) == 1: + option = parser.optionxform(path[0]) + result = map[option] + elif len(path) == 2: + section, option = path + option = parser.optionxform(option) + result = parser.get(section, option, raw=True) + else: + raise InterpolationSyntaxError( + option, section, f"More than one ':' found: {remainder!r}" + ) + except (KeyError, NoSectionError, NoOptionError): + raise InterpolationMissingOptionError( + option, section, rawval, ":".join(path) + ) from None + + return (result, remainder, section, option) + class _ReadState: elements_added: set[str] From 047b9e2b279c392065cc6db52adf50abe5266b62 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Tue, 24 Nov 2026 23:10:17 -0600 Subject: [PATCH 2/4] Improve phrasing of interpolation scanning loops For basic and extended interpolation, improve the naming of local variables to be clearer -- `pos` for position and `next_char` rather than `c` -- and use a walrus-inside a comparison for the loop conditions so that the loops are "while there is more interpolation to do" rather than "while there is more string to consume". The overall result is slightly less nested control flow and slightly more readable naming. --- Lib/configparser.py | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 40f930284254fa..d6357e8d05af29 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -472,20 +472,15 @@ 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, rawval) - while rest: - p = rest.find("%") - if p < 0: - accum.append(rest) - return - if p > 0: - accum.append(rest[:p]) - rest = rest[p:] - # p is no longer used - c = rest[1:2] - if c == "%": + while (pos := rest.find("%")) >= 0: + if pos > 0: + accum.append(rest[:pos]) + rest = rest[pos:] + next_char = rest[1:2] + if next_char == "%": accum.append("%") rest = rest[2:] - elif c == "(": + elif next_char == "(": v, rest, _, _ = self._do_one_interpolation( rest, rawval, parser, section, option, map ) @@ -501,6 +496,8 @@ def _interpolate_some(self, parser, option, accum, rest, section, map, depth): section, f"'%' must be followed by '%' or '(', found: {rest!r}", ) + if rest: + accum.append(rest) def _do_one_interpolation( self, value, rawval, parser, section, option, map @@ -553,20 +550,15 @@ def _interpolate_some( rawval = parser.get(section, option, raw=True, fallback=rest) if depth > MAX_INTERPOLATION_DEPTH: raise InterpolationDepthError(option, section, rawval) - while rest: - p = rest.find("$") - if p < 0: - accum.append(rest) - return - if p > 0: - accum.append(rest[:p]) - rest = rest[p:] - # p is no longer used - c = rest[1:2] - if c == "$": + while (pos := rest.find("$")) >= 0: + if pos > 0: + accum.append(rest[:pos]) + rest = rest[pos:] + next_char = rest[1:2] + if next_char == "$": accum.append("$") rest = rest[2:] - elif c == "{": + elif next_char == "{": v, rest, ref_section, ref_option = self._do_one_interpolation( rest, rawval, parser, section, option, map ) @@ -590,6 +582,8 @@ def _interpolate_some( section, f"'$' must be followed by '$' or '{{', found: {rest!r}", ) + if rest: + accum.append(rest) def _do_one_interpolation( self, value, rawval, parser, section, option, map From 28939e6fb2d53b38d6244c9929e465f2e2754600 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Tue, 24 Nov 2026 23:18:05 -0600 Subject: [PATCH 3/4] In configparser interpolation, replace 'accum' Rather than appending data to an accumulator list, yield items in a generator. This has two key benefits: 1. Reduces complexity, by removing a variable we had to pass around 2. The sub-accumulators in recursive calls are now explicitly marked with `yield from` syntax --- Lib/configparser.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index d6357e8d05af29..94afc9b2ef69dd 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -454,9 +454,9 @@ class BasicInterpolation(Interpolation): _KEYCRE = re.compile(r"%\(([^)]+)\)s") def before_get(self, parser, section, option, value, defaults): - L = [] - self._interpolate_some(parser, option, L, value, section, defaults, 1) - return ''.join(L) + return ''.join( + self._interpolate_some(parser, option, value, section, defaults, 1) + ) def before_set(self, parser, section, option, value): tmp_value = value.replace('%%', '') # escaped percent signs @@ -468,28 +468,28 @@ def before_set(self, parser, section, option, value): ) return value - def _interpolate_some(self, parser, option, accum, rest, section, map, depth): + def _interpolate_some(self, parser, option, rest, section, map, depth): rawval = parser.get(section, option, raw=True, fallback=rest) if depth > MAX_INTERPOLATION_DEPTH: raise InterpolationDepthError(option, section, rawval) while (pos := rest.find("%")) >= 0: if pos > 0: - accum.append(rest[:pos]) + yield rest[:pos] rest = rest[pos:] next_char = rest[1:2] if next_char == "%": - accum.append("%") + yield "%" rest = rest[2:] elif next_char == "(": v, rest, _, _ = self._do_one_interpolation( rest, rawval, parser, section, option, map ) if "%" in v: - self._interpolate_some( - parser, option, accum, v, section, map, depth + 1 + yield from self._interpolate_some( + parser, option, v, section, map, depth + 1 ) else: - accum.append(v) + yield v else: raise InterpolationSyntaxError( option, @@ -497,7 +497,7 @@ def _interpolate_some(self, parser, option, accum, rest, section, map, depth): f"'%' must be followed by '%' or '(', found: {rest!r}", ) if rest: - accum.append(rest) + yield rest def _do_one_interpolation( self, value, rawval, parser, section, option, map @@ -530,9 +530,9 @@ class ExtendedInterpolation(Interpolation): _KEYCRE = re.compile(r"\$\{([^}]+)\}") def before_get(self, parser, section, option, value, defaults): - L = [] - self._interpolate_some(parser, option, L, value, section, defaults, 1) - return ''.join(L) + return ''.join( + self._interpolate_some(parser, option, value, section, defaults, 1) + ) def before_set(self, parser, section, option, value): tmp_value = value.replace('$$', '') # escaped dollar signs @@ -545,18 +545,18 @@ def before_set(self, parser, section, option, value): return value def _interpolate_some( - self, parser, option, accum, rest, section, map, depth + self, parser, option, rest, section, map, depth ): rawval = parser.get(section, option, raw=True, fallback=rest) if depth > MAX_INTERPOLATION_DEPTH: raise InterpolationDepthError(option, section, rawval) while (pos := rest.find("$")) >= 0: if pos > 0: - accum.append(rest[:pos]) + yield rest[:pos] rest = rest[pos:] next_char = rest[1:2] if next_char == "$": - accum.append("$") + yield "$" rest = rest[2:] elif next_char == "{": v, rest, ref_section, ref_option = self._do_one_interpolation( @@ -565,17 +565,16 @@ def _interpolate_some( if v is None: continue if "$" in v: - self._interpolate_some( + yield from self._interpolate_some( parser, ref_option, - accum, v, ref_section, dict(parser.items(ref_section, raw=True)), depth + 1, ) else: - accum.append(v) + yield v else: raise InterpolationSyntaxError( option, @@ -583,7 +582,7 @@ def _interpolate_some( f"'$' must be followed by '$' or '{{', found: {rest!r}", ) if rest: - accum.append(rest) + yield rest def _do_one_interpolation( self, value, rawval, parser, section, option, map From 42d81afd43661dcdc3f085d2ec28b9c36d11ab8f Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Tue, 24 Nov 2026 23:25:10 -0600 Subject: [PATCH 4/4] Make arg order more consistent in interpolation (parser, section, option, value, ...) is the ordering used in public APIs, so make it the ordering used in private APIs as well. --- Lib/configparser.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 94afc9b2ef69dd..b3e80c6e6fcb91 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -455,7 +455,7 @@ class BasicInterpolation(Interpolation): def before_get(self, parser, section, option, value, defaults): return ''.join( - self._interpolate_some(parser, option, value, section, defaults, 1) + self._interpolate_some(parser, section, option, value, defaults, 1) ) def before_set(self, parser, section, option, value): @@ -468,7 +468,7 @@ def before_set(self, parser, section, option, value): ) return value - def _interpolate_some(self, parser, option, rest, section, map, depth): + def _interpolate_some(self, parser, section, option, rest, map, depth): rawval = parser.get(section, option, raw=True, fallback=rest) if depth > MAX_INTERPOLATION_DEPTH: raise InterpolationDepthError(option, section, rawval) @@ -482,11 +482,11 @@ def _interpolate_some(self, parser, option, rest, section, map, depth): rest = rest[2:] elif next_char == "(": v, rest, _, _ = self._do_one_interpolation( - rest, rawval, parser, section, option, map + parser, section, option, rest, rawval, map ) if "%" in v: yield from self._interpolate_some( - parser, option, v, section, map, depth + 1 + parser, section, option, v, map, depth + 1 ) else: yield v @@ -500,7 +500,7 @@ def _interpolate_some(self, parser, option, rest, section, map, depth): yield rest def _do_one_interpolation( - self, value, rawval, parser, section, option, map + self, parser, section, option, value, rawval, map ): m = self._KEYCRE.match(value) if m is None: @@ -531,7 +531,7 @@ class ExtendedInterpolation(Interpolation): def before_get(self, parser, section, option, value, defaults): return ''.join( - self._interpolate_some(parser, option, value, section, defaults, 1) + self._interpolate_some(parser, section, option, value, defaults, 1) ) def before_set(self, parser, section, option, value): @@ -544,9 +544,7 @@ def before_set(self, parser, section, option, value): ) return value - def _interpolate_some( - self, parser, option, rest, section, map, depth - ): + def _interpolate_some(self, parser, section, option, rest, map, depth): rawval = parser.get(section, option, raw=True, fallback=rest) if depth > MAX_INTERPOLATION_DEPTH: raise InterpolationDepthError(option, section, rawval) @@ -560,16 +558,16 @@ def _interpolate_some( rest = rest[2:] elif next_char == "{": v, rest, ref_section, ref_option = self._do_one_interpolation( - rest, rawval, parser, section, option, map + parser, section, option, rest, rawval, map ) if v is None: continue if "$" in v: yield from self._interpolate_some( parser, + ref_section, ref_option, v, - ref_section, dict(parser.items(ref_section, raw=True)), depth + 1, ) @@ -585,7 +583,7 @@ def _interpolate_some( yield rest def _do_one_interpolation( - self, value, rawval, parser, section, option, map + self, parser, section, option, value, rawval, map ): m = self._KEYCRE.match(value) if m is None: