Skip to content

Commit 70eb79c

Browse files
committed
Merged solution for #11324 from 3.2.
2 parents bf53a9c + 1aa422f commit 70eb79c

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
@@ -624,11 +624,12 @@ def __init__(self, defaults=None, dict_type=_default_dict,
624624
self._strict = strict
625625
self._allow_no_value = allow_no_value
626626
self._empty_lines_in_values = empty_lines_in_values
627-
if interpolation is _UNSET:
628-
self._interpolation = self._DEFAULT_INTERPOLATION
629-
else:
630-
self._interpolation = interpolation
631627
self.default_section=default_section
628+
self._interpolation = interpolation
629+
if self._interpolation is _UNSET:
630+
self._interpolation = self._DEFAULT_INTERPOLATION
631+
if self._interpolation is None:
632+
self._interpolation = Interpolation()
632633

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

Lib/test/test_configparser.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
@@ -115,6 +115,7 @@ Terrence Brannon
115115
Brian Brazil
116116
Dave Brennan
117117
Tom Bridgman
118+
Tobias Brink
118119
Richard Brodie
119120
Michael Broghton
120121
Daniel Brotsky

0 commit comments

Comments
 (0)