Skip to content

Commit 1aa422f

Browse files
committed
Closes python#11324: ConfigParser(interpolation=None) doesn't work.
Initial patches by Tobias Brink. Thanks!
1 parent f531113 commit 1aa422f

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎Lib/configparser.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,12 @@ def __init__(self, defaults=None, dict_type=_default_dict,
623623
self._strict = strict
624624
self._allow_no_value = allow_no_value
625625
self._empty_lines_in_values = empty_lines_in_values
626-
if interpolation is _UNSET:
627-
self._interpolation = self._DEFAULT_INTERPOLATION
628-
else:
629-
self._interpolation = interpolation
630626
self.default_section=default_section
627+
self._interpolation = interpolation
628+
if self._interpolation is _UNSET:
629+
self._interpolation = self._DEFAULT_INTERPOLATION
630+
if self._interpolation is None:
631+
self._interpolation = Interpolation()
631632

632633
def defaults(self):
633634
return self._defaults

‎Lib/test/test_cfgparser.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,43 @@ def test_add_section_default(self):
864864
cf = self.newconfig()
865865
self.assertRaises(ValueError, cf.add_section, self.default_section)
866866

867+
868+
class ConfigParserTestCaseNoInterpolation(BasicTestCase):
869+
config_class = configparser.ConfigParser
870+
interpolation = None
871+
ini = textwrap.dedent("""
872+
[numbers]
873+
one = 1
874+
two = %(one)s * 2
875+
three = ${common:one} * 3
876+
877+
[hexen]
878+
sixteen = ${numbers:two} * 8
879+
""").strip()
880+
881+
def assertMatchesIni(self, cf):
882+
self.assertEqual(cf['numbers']['one'], '1')
883+
self.assertEqual(cf['numbers']['two'], '%(one)s * 2')
884+
self.assertEqual(cf['numbers']['three'], '${common:one} * 3')
885+
self.assertEqual(cf['hexen']['sixteen'], '${numbers:two} * 8')
886+
887+
def test_no_interpolation(self):
888+
cf = self.fromstring(self.ini)
889+
self.assertMatchesIni(cf)
890+
891+
def test_empty_case(self):
892+
cf = self.newconfig()
893+
self.assertIsNone(cf.read_string(""))
894+
895+
def test_none_as_default_interpolation(self):
896+
class CustomConfigParser(configparser.ConfigParser):
897+
_DEFAULT_INTERPOLATION = None
898+
899+
cf = CustomConfigParser()
900+
cf.read_string(self.ini)
901+
self.assertMatchesIni(cf)
902+
903+
867904
class ConfigParserTestCaseLegacyInterpolation(ConfigParserTestCase):
868905
config_class = configparser.ConfigParser
869906
interpolation = configparser.LegacyInterpolation()
@@ -1444,6 +1481,7 @@ def test_main():
14441481
ConfigParserTestCaseNoValue,
14451482
ConfigParserTestCaseExtendedInterpolation,
14461483
ConfigParserTestCaseLegacyInterpolation,
1484+
ConfigParserTestCaseNoInterpolation,
14471485
ConfigParserTestCaseTrickyFile,
14481486
MultilineValuesTestCase,
14491487
RawConfigParserTestCase,

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Terrence Brannon
113113
Brian Brazil
114114
Dave Brennan
115115
Tom Bridgman
116+
Tobias Brink
116117
Richard Brodie
117118
Michael Broghton
118119
Daniel Brotsky

0 commit comments

Comments
 (0)