Skip to content

Commit 0c73259

Browse files
committed
gh-153855: Fix BasicInterpolation crash on None-valued interpolation reference
1 parent b090d06 commit 0c73259

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

Lib/configparser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
471471
except KeyError:
472472
raise InterpolationMissingOptionError(
473473
option, section, rawval, var) from None
474+
475+
if v is None:
476+
continue
477+
474478
if "%" in v:
475479
self._interpolate_some(parser, option, accum, v,
476480
section, map, depth + 1)

Lib/test/test_configparser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,17 @@ class RawConfigParserNoValueAndExtendedInterpolationTest(
13791379
):
13801380
config_class = configparser.RawConfigParser
13811381

1382+
class BasicInterpolationWithAllowNoValueTest(unittest.TestCase):
1383+
def test_interpolation_with_allow_no_value(self):
1384+
cf = configparser.ConfigParser(allow_no_value=True)
1385+
cf.read_string(textwrap.dedent("""
1386+
[s]
1387+
a
1388+
b = x%(a)sy
1389+
"""))
1390+
self.assertIsNone(cf["s"]["a"])
1391+
self.assertEqual(cf.get("s", "b"), "xy")
1392+
13821393

13831394
class ConfigParserTestCaseTrickyFile(CfgParserTestCaseClass, unittest.TestCase):
13841395
config_class = configparser.ConfigParser
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :class:`configparser.BasicInterpolation` raising ``TypeError`` when interpolating a reference to a value-less option with ``allow_no_value=True``; it now treats the missing value as empty, like :class:`configparser.ExtendedInterpolation`.

0 commit comments

Comments
 (0)