bpo-12178: Fix escaping of escapechar in csv.writer() (GH-13710)

Co-authored-by: Itay Elbirt <anotahacou@gmail.com>
This commit is contained in:
Berker Peksag 2020-09-20 09:38:07 +03:00 committed by GitHub
parent bfee9fad84
commit 5c0eed7375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -202,6 +202,20 @@ class Test_Csv(unittest.TestCase):
escapechar='\\', quoting = csv.QUOTE_NONE)
self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
escapechar='\\', quoting = csv.QUOTE_NONE)
self._write_test(['\\', 'a'], '\\\\,a',
escapechar='\\', quoting=csv.QUOTE_NONE)
self._write_test(['\\', 'a'], '\\\\,a',
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
self._write_test(['\\', 'a'], '"\\\\","a"',
escapechar='\\', quoting=csv.QUOTE_ALL)
self._write_test(['\\ ', 'a'], '\\\\ ,a',
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
self._write_test(['\\,', 'a'], '\\\\\\,,a',
escapechar='\\', quoting=csv.QUOTE_NONE)
self._write_test([',\\', 'a'], '",\\\\",a',
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
self._write_test(['C\\', '6', '7', 'X"'], 'C\\\\,6,7,"X"""',
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
def test_write_iterable(self):
self._write_test(iter(['a', 1, 'p,q']), 'a,1,"p,q"')

View File

@ -0,0 +1,3 @@
:func:`csv.writer` now correctly escapes *escapechar* when input
contains *escapechar*. Patch by Catalin Iacob, Berker Peksag,
and Itay Elbirt.

View File

@ -1040,6 +1040,9 @@ join_append_data(WriterObj *self, unsigned int field_kind, const void *field_dat
else
want_escape = 1;
}
else if (c == dialect->escapechar) {
want_escape = 1;
}
if (!want_escape)
*quoted = 1;
}