Skip to content

Commit ee8f681

Browse files
[3.13] gh-130941: Fix configparser parsing values with allow_no_value and interpolation set (GH-130949) (#132588)
gh-130941: Fix `configparser` parsing values with `allow_no_value` and `interpolation` set (GH-130949) (cherry picked from commit c35c735) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 8128bcf commit ee8f681

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Lib/configparser.py

+2
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
526526
except (KeyError, NoSectionError, NoOptionError):
527527
raise InterpolationMissingOptionError(
528528
option, section, rawval, ":".join(path)) from None
529+
if v is None:
530+
continue
529531
if "$" in v:
530532
self._interpolate_some(parser, opt, accum, v, sect,
531533
dict(parser.items(sect, raw=True)),

Lib/test/test_configparser.py

+41
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,47 @@ class ConfigParserTestCaseNoValue(ConfigParserTestCase):
13281328
allow_no_value = True
13291329

13301330

1331+
class NoValueAndExtendedInterpolation(CfgParserTestCaseClass):
1332+
interpolation = configparser.ExtendedInterpolation()
1333+
allow_no_value = True
1334+
1335+
def test_interpolation_with_allow_no_value(self):
1336+
config = textwrap.dedent("""
1337+
[dummy]
1338+
a
1339+
b = ${a}
1340+
""")
1341+
cf = self.fromstring(config)
1342+
1343+
self.assertIs(cf["dummy"]["a"], None)
1344+
self.assertEqual(cf["dummy"]["b"], "")
1345+
1346+
def test_explicit_none(self):
1347+
config = textwrap.dedent("""
1348+
[dummy]
1349+
a = None
1350+
b = ${a}
1351+
""")
1352+
cf = self.fromstring(config)
1353+
1354+
self.assertEqual(cf["dummy"]["a"], "None")
1355+
self.assertEqual(cf["dummy"]["b"], "None")
1356+
1357+
1358+
class ConfigParserNoValueAndExtendedInterpolationTest(
1359+
NoValueAndExtendedInterpolation,
1360+
unittest.TestCase,
1361+
):
1362+
config_class = configparser.ConfigParser
1363+
1364+
1365+
class RawConfigParserNoValueAndExtendedInterpolationTest(
1366+
NoValueAndExtendedInterpolation,
1367+
unittest.TestCase,
1368+
):
1369+
config_class = configparser.RawConfigParser
1370+
1371+
13311372
class ConfigParserTestCaseTrickyFile(CfgParserTestCaseClass, unittest.TestCase):
13321373
config_class = configparser.ConfigParser
13331374
delimiters = {'='}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`configparser.ConfigParser` parsing empty interpolation with
2+
``allow_no_value`` set to ``True``.

0 commit comments

Comments
 (0)