FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.11] gh-143927: Normalize all line endings (CR, CRLF, and LF) in configparser (GH-143929) by miss-islington · Pull Request #152006 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 3 additions & 1 deletion Lib/configparser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,9 @@ def _write_section(self, fp, section_name, section_items, delimiter):
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))
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_configparser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,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,
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Normalize all line endings (CR, CRLF, and LF) to LF+TAB when writing
multi-line configparser values.
Loading

Back | FazBrowse Home | New Git URL