Skip to content

Commit aaf850f

Browse files
[3.13] gh-143927: Normalize all line endings (CR, CRLF, and LF) in configparser (GH-143929) (GH-152004)
(cherry picked from commit 5858e42) Co-authored-by: Seth Larson <seth@python.org>
1 parent b83961a commit aaf850f

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

Lib/configparser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,9 @@ def _write_section(self, fp, section_name, section_items, delimiter, unnamed=Fal
973973
value = self._interpolation.before_write(self, section_name, key,
974974
value)
975975
if value is not None or not self._allow_no_value:
976-
value = delimiter + str(value).replace('\n', '\n\t')
976+
# Convert all possible line-endings into '\n\t'
977+
value = (delimiter + str(value).replace('\r\n', '\n')
978+
.replace('\r', '\n').replace('\n', '\n\t'))
977979
else:
978980
value = ""
979981
fp.write("{}{}\n".format(key, value))

Lib/test/test_configparser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,17 @@ def test_default_case_sensitivity(self):
526526
cf.get(self.default_section, "Foo"), "Bar",
527527
"could not locate option, expecting case-insensitive defaults")
528528

529+
def test_crlf_normalization(self):
530+
cf = self.newconfig({"key1": "a\nb","key2": "a\rb", "key3": "a\r\nb", "key4": "a\r\nb"})
531+
buf = io.StringIO()
532+
cf.write(buf)
533+
cf_str = buf.getvalue()
534+
self.assertNotIn("\r", cf_str)
535+
self.assertNotIn("\r\n", cf_str)
536+
self.assertEqual(cf_str.count("\n"), 10)
537+
self.assertEqual(cf_str.count("\n\t"), 4)
538+
self.assertTrue(cf_str.endswith("\n\n"))
539+
529540
def test_parse_errors(self):
530541
cf = self.newconfig()
531542
self.parse_error(cf, configparser.ParsingError,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Normalize all line endings (CR, CRLF, and LF) to LF+TAB when writing
2+
multi-line configparser values.

0 commit comments

Comments
 (0)