mirror of
https://github.com/python/cpython.git
synced 2026-06-28 03:41:13 +00:00
[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:
parent
b83961ade8
commit
aaf850fd33
3 changed files with 16 additions and 1 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Normalize all line endings (CR, CRLF, and LF) to LF+TAB when writing
|
||||
multi-line configparser values.
|
||||
Loading…
Add table
Add a link
Reference in a new issue