Skip to content

Commit 9d7209f

Browse files
[3.12] gh-113785: csv: fields starting with escapechar are not quoted (GH-122110) (GH-122259)
(cherry picked from commit a3327db) Co-authored-by: Mikołaj Kuranowski <mkuranowski@gmail.com>
1 parent c57a33d commit 9d7209f

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

Lib/test/test_csv.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ def test_read_quoting(self):
425425
quoting=csv.QUOTE_NONNUMERIC)
426426
self._read_test(['1,@,3,@,5'], [['1', ',3,', '5']], quotechar='@')
427427
self._read_test(['1,\0,3,\0,5'], [['1', ',3,', '5']], quotechar='\0')
428+
self._read_test(['1\\.5,\\.5,.5'], [[1.5, 0.5, 0.5]],
429+
quoting=csv.QUOTE_NONNUMERIC, escapechar='\\')
428430

429431
def test_read_skipinitialspace(self):
430432
self._read_test(['no space, space, spaces,\ttab'],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`csv` now correctly parses numeric fields (when used with :const:`csv.QUOTE_NONNUMERIC`) which start with an escape character.

Modules/_csv.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ parse_process_char(ReaderObj *self, _csvstate *module_state, Py_UCS4 c)
701701
}
702702
else if (c == dialect->escapechar) {
703703
/* possible escaped character */
704+
if (dialect->quoting == QUOTE_NONNUMERIC)
705+
self->numeric_field = 1;
704706
self->state = ESCAPED_CHAR;
705707
}
706708
else if (c == ' ' && dialect->skipinitialspace)

0 commit comments

Comments
 (0)