bpo-12178: Fix escaping of escapechar in csv.writer() (GH-13710)
Co-authored-by: Itay Elbirt <anotahacou@gmail.com>
This commit is contained in:
parent
bfee9fad84
commit
5c0eed7375
|
@ -202,6 +202,20 @@ class Test_Csv(unittest.TestCase):
|
||||||
escapechar='\\', quoting = csv.QUOTE_NONE)
|
escapechar='\\', quoting = csv.QUOTE_NONE)
|
||||||
self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
|
self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
|
||||||
escapechar='\\', quoting = csv.QUOTE_NONE)
|
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):
|
def test_write_iterable(self):
|
||||||
self._write_test(iter(['a', 1, 'p,q']), 'a,1,"p,q"')
|
self._write_test(iter(['a', 1, 'p,q']), 'a,1,"p,q"')
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
:func:`csv.writer` now correctly escapes *escapechar* when input
|
||||||
|
contains *escapechar*. Patch by Catalin Iacob, Berker Peksag,
|
||||||
|
and Itay Elbirt.
|
|
@ -1040,6 +1040,9 @@ join_append_data(WriterObj *self, unsigned int field_kind, const void *field_dat
|
||||||
else
|
else
|
||||||
want_escape = 1;
|
want_escape = 1;
|
||||||
}
|
}
|
||||||
|
else if (c == dialect->escapechar) {
|
||||||
|
want_escape = 1;
|
||||||
|
}
|
||||||
if (!want_escape)
|
if (!want_escape)
|
||||||
*quoted = 1;
|
*quoted = 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue