gh-130941: Fix configparser parsing values with allow_no_value and interpolation set (GH-130949)

This commit is contained in:
sobolevn 2025-04-16 13:39:11 +03:00 committed by GitHub
parent 8b7cb947c5
commit c35c7353eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View File

@ -541,6 +541,8 @@ class ExtendedInterpolation(Interpolation):
except (KeyError, NoSectionError, NoOptionError):
raise InterpolationMissingOptionError(
option, section, rawval, ":".join(path)) from None
if v is None:
continue
if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)),

View File

@ -1328,6 +1328,47 @@ class ConfigParserTestCaseNoValue(ConfigParserTestCase):
allow_no_value = True
class NoValueAndExtendedInterpolation(CfgParserTestCaseClass):
interpolation = configparser.ExtendedInterpolation()
allow_no_value = True
def test_interpolation_with_allow_no_value(self):
config = textwrap.dedent("""
[dummy]
a
b = ${a}
""")
cf = self.fromstring(config)
self.assertIs(cf["dummy"]["a"], None)
self.assertEqual(cf["dummy"]["b"], "")
def test_explicit_none(self):
config = textwrap.dedent("""
[dummy]
a = None
b = ${a}
""")
cf = self.fromstring(config)
self.assertEqual(cf["dummy"]["a"], "None")
self.assertEqual(cf["dummy"]["b"], "None")
class ConfigParserNoValueAndExtendedInterpolationTest(
NoValueAndExtendedInterpolation,
unittest.TestCase,
):
config_class = configparser.ConfigParser
class RawConfigParserNoValueAndExtendedInterpolationTest(
NoValueAndExtendedInterpolation,
unittest.TestCase,
):
config_class = configparser.RawConfigParser
class ConfigParserTestCaseTrickyFile(CfgParserTestCaseClass, unittest.TestCase):
config_class = configparser.ConfigParser
delimiters = {'='}

View File

@ -0,0 +1,2 @@
Fix :class:`configparser.ConfigParser` parsing empty interpolation with
``allow_no_value`` set to ``True``.