[3.13] gh-143927: Normalize all line endings (CR, CRLF, and LF) in configparser (GH-143929) (GH-152004)

(cherry picked from commit 5858e42c53)

Co-authored-by: Seth Larson <seth@python.org>
This commit is contained in:
Miss Islington (bot) 2026-06-24 11:46:43 +02:00 committed by GitHub
parent b83961ade8
commit aaf850fd33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 1 deletions

View file

@ -973,7 +973,9 @@ def _write_section(self, fp, section_name, section_items, delimiter, unnamed=Fal
value = self._interpolation.before_write(self, section_name, key,
value)
if value is not None or not self._allow_no_value:
value = delimiter + str(value).replace('\n', '\n\t')
# Convert all possible line-endings into '\n\t'
value = (delimiter + str(value).replace('\r\n', '\n')
.replace('\r', '\n').replace('\n', '\n\t'))
else:
value = ""
fp.write("{}{}\n".format(key, value))

View file

@ -526,6 +526,17 @@ def test_default_case_sensitivity(self):
cf.get(self.default_section, "Foo"), "Bar",
"could not locate option, expecting case-insensitive defaults")
def test_crlf_normalization(self):
cf = self.newconfig({"key1": "a\nb","key2": "a\rb", "key3": "a\r\nb", "key4": "a\r\nb"})
buf = io.StringIO()
cf.write(buf)
cf_str = buf.getvalue()
self.assertNotIn("\r", cf_str)
self.assertNotIn("\r\n", cf_str)
self.assertEqual(cf_str.count("\n"), 10)
self.assertEqual(cf_str.count("\n\t"), 4)
self.assertTrue(cf_str.endswith("\n\n"))
def test_parse_errors(self):
cf = self.newconfig()
self.parse_error(cf, configparser.ParsingError,

View file

@ -0,0 +1,2 @@
Normalize all line endings (CR, CRLF, and LF) to LF+TAB when writing
multi-line configparser values.