bpo-23835: Restore legacy defaults= behavior for RawConfigParser (#3191) · python/cpython@a5fab17 · GitHub
Skip to content

Commit a5fab17

Browse files
authored
bpo-23835: Restore legacy defaults= behavior for RawConfigParser (#3191)
The fix for bpo-23835 fixed ConfigParser behavior in defaults= handling. Unfortunately, it caused a backwards compatibility regression with RawConfigParser objects which allow for non-string values. This commit restores the legacy behavior for RawConfigParser only.
1 parent a6296d3 commit a5fab17

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

Doc/library/configparser.rst

Lines changed: 4 additions & 2 deletions

Lib/configparser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
635635
if converters is not _UNSET:
636636
self._converters.update(converters)
637637
if defaults:
638-
self.read_dict({default_section: defaults})
638+
self._read_defaults(defaults)
639639

640640
def defaults(self):
641641
return self._defaults
@@ -1121,6 +1121,12 @@ def _join_multiline_values(self):
11211121
section,
11221122
name, val)
11231123

1124+
def _read_defaults(self, defaults):
1125+
"""Read the defaults passed in the initializer.
1126+
Note: values can be non-string."""
1127+
for key, value in defaults.items():
1128+
self._defaults[self.optionxform(key)] = value
1129+
11241130
def _handle_error(self, exc, fpname, lineno, line):
11251131
if not exc:
11261132
exc = ParsingError(fpname)
@@ -1198,6 +1204,11 @@ def add_section(self, section):
11981204
self._validate_value_types(section=section)
11991205
super().add_section(section)
12001206

1207+
def _read_defaults(self, defaults):
1208+
"""Reads the defaults passed in the initializer, implicitly converting
1209+
values to strings like the rest of the API."""
1210+
self.read_dict({self.default_section: defaults})
1211+
12011212

12021213
class SafeConfigParser(ConfigParser):
12031214
"""ConfigParser alias for backwards compatibility purposes."""

Lib/test/test_configparser.py

Lines changed: 18 additions & 9 deletions

0 commit comments

Comments
 (0)