Skip to content

Commit c35c735

Browse files
authored
gh-130941: Fix configparser parsing values with allow_no_value and interpolation set (GH-130949)
1 parent 8b7cb94 commit c35c735

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
@@ -541,6 +541,8 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
541541
except (KeyError, NoSectionError, NoOptionError):
542542
raise InterpolationMissingOptionError(
543543
option, section, rawval, ":".join(path)) from None
544+
if v is None:
545+
continue
544546
if "$" in v:
545547
self._interpolate_some(parser, opt, accum, v, sect,
546548
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)