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))
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,