Skip to content

Commit 2a122fd

Browse files
committed
gh-143927: Normalize all line endings (CR, CRLF, and LF) in configparser
1 parent 3e93225 commit 2a122fd

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/configparser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,9 @@ def _write_section(self, fp, section_name, section_items, delimiter, unnamed=Fal
985985
value = self._interpolation.before_write(self, section_name, key,
986986
value)
987987
if value is not None or not self._allow_no_value:
988-
value = delimiter + str(value).replace('\n', '\n\t')
988+
# Convert all possible line-endings into '\n\t'
989+
value = (delimiter + str(value).replace('\r\n', '\n')
990+
.replace('\r', '\n').replace('\n', '\n\t'))
989991
else:
990992
value = ""
991993
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)