summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2020-09-20 06:38:07 (GMT)
committerGitHub <noreply@github.com>2020-09-20 06:38:07 (GMT)
commit5c0eed7375fdd791cc5e19ceabfab4170ad44062 (patch)
tree7ddeaf0079a48d462f5427618466ad7218272710
parentbfee9fad84531a471fd7864e88947320669f68e2 (diff)
downloadcpython-5c0eed7375fdd791cc5e19ceabfab4170ad44062.zip
cpython-5c0eed7375fdd791cc5e19ceabfab4170ad44062.tar.gz
cpython-5c0eed7375fdd791cc5e19ceabfab4170ad44062.tar.bz2
bpo-12178: Fix escaping of escapechar in csv.writer() (GH-13710)
Co-authored-by: Itay Elbirt <anotahacou@gmail.com>
-rw-r--r--Lib/test/test_csv.py14
-rw-r--r--Misc/NEWS.d/next/Library/2019-05-31-23-54-28.bpo-12178.N6FLCZ.rst3
-rw-r--r--Modules/_csv.c3
3 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 3816022..a98707c 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -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"')
diff --git a/Misc/NEWS.d/next/Library/2019-05-31-23-54-28.bpo-12178.N6FLCZ.rst b/Misc/NEWS.d/next/Library/2019-05-31-23-54-28.bpo-12178.N6FLCZ.rst
new file mode 100644
index 0000000..80e2a7b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-31-23-54-28.bpo-12178.N6FLCZ.rst
@@ -0,0 +1,3 @@
+:func:`csv.writer` now correctly escapes *escapechar* when input
+contains *escapechar*. Patch by Catalin Iacob, Berker Peksag,
+and Itay Elbirt.
diff --git a/Modules/_csv.c b/Modules/_csv.c
index da61db9..594f6c1 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -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;
}