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

Berker Peksag webhook-mailer at python.org
Sun Sep 20 02:38:15 EDT 2020


https://github.com/python/cpython/commit/5c0eed7375fdd791cc5e19ceabfab4170ad44062
commit: 5c0eed7375fdd791cc5e19ceabfab4170ad44062
branch: master
author: Berker Peksag <berker.peksag at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-09-20T09:38:07+03:00
summary:

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

Co-authored-by: Itay Elbirt <anotahacou at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-05-31-23-54-28.bpo-12178.N6FLCZ.rst
M Lib/test/test_csv.py
M Modules/_csv.c

diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 38160220853ea..a98707ce3caed 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -202,6 +202,20 @@ def test_write_escape(self):
                          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 0000000000000..80e2a7b5fbb2c
--- /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 da61db9377f94..594f6c1472726 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;
             }



More information about the Python-checkins mailing list