]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113785: csv: fields starting with escapechar are not quoted (GH-122110)
authorMikołaj Kuranowski <mkuranowski@gmail.com>
Thu, 25 Jul 2024 07:04:47 +0000 (09:04 +0200)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2024 07:04:47 +0000 (10:04 +0300)
Lib/test/test_csv.py
Misc/NEWS.d/next/Library/2024-07-22-08-14-04.gh-issue-113785.6B_KNB.rst [new file with mode: 0644]
Modules/_csv.c

index d74ab7e016f78c0e4a025b085e9f015f781e37c5..c718ee1203cbe08aaeef0b25971f520ade7e516e 100644 (file)
@@ -454,6 +454,10 @@ class Test_Csv(unittest.TestCase):
                           quoting=csv.QUOTE_STRINGS)
         self._read_test(['1,@,3,@,5'], [['1', ',3,', '5']], quotechar='@')
         self._read_test(['1,\0,3,\0,5'], [['1', ',3,', '5']], quotechar='\0')
+        self._read_test(['1\\.5,\\.5,.5'], [[1.5, 0.5, 0.5]],
+                        quoting=csv.QUOTE_NONNUMERIC, escapechar='\\')
+        self._read_test(['1\\.5,\\.5,"\\.5"'], [[1.5, 0.5, ".5"]],
+                        quoting=csv.QUOTE_STRINGS, escapechar='\\')
 
     def test_read_skipinitialspace(self):
         self._read_test(['no space, space,  spaces,\ttab'],
diff --git a/Misc/NEWS.d/next/Library/2024-07-22-08-14-04.gh-issue-113785.6B_KNB.rst b/Misc/NEWS.d/next/Library/2024-07-22-08-14-04.gh-issue-113785.6B_KNB.rst
new file mode 100644 (file)
index 0000000..89d44a3
--- /dev/null
@@ -0,0 +1 @@
+:mod:`csv` now correctly parses numeric fields (when used with :const:`csv.QUOTE_NONNUMERIC` or :const:`csv.QUOTE_STRINGS`) which start with an escape character.
index 9964c383b8ad092ec98f17776793f3ed1684e0e9..737b2c7468e13cb43b0dae4be602cbf67e8de278 100644 (file)
@@ -749,7 +749,6 @@ parse_process_char(ReaderObj *self, _csvstate *module_state, Py_UCS4 c)
         }
         else if (c == dialect->escapechar) {
             /* possible escaped character */
-            self->unquoted_field = false;
             self->state = ESCAPED_CHAR;
         }
         else if (c == ' ' && dialect->skipinitialspace)