summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSenthil Kumaran <senthil@uthcode.com>2012-09-25 02:30:27 -0700
committerSenthil Kumaran <senthil@uthcode.com>2012-09-25 02:30:27 -0700
commit67b7b98a479a5703a5707b6ea0cf81d3e0b441ea (patch)
tree159bf3e8c217bfb7bf16ac7bccc3e73dde62399a
parent9bb56a66c548e84012de141e4665ea39701c1e5f (diff)
downloadcpython-git-67b7b98a479a5703a5707b6ea0cf81d3e0b441ea.tar.gz
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
-rw-r--r--Lib/test/test_csv.py9
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_csv.c10
3 files changed, 19 insertions, 3 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 8ca1e62c4a..55796a204a 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -225,6 +225,15 @@ class Test_Csv(unittest.TestCase):
self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], [])
self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], [])
+ def test_read_eof(self):
+ self._read_test(['a,"'], [['a', '']])
+ self._read_test(['"a'], [['a']])
+ self._read_test(['^'], [['\n']], escapechar='^')
+ self.assertRaises(csv.Error, self._read_test, ['a,"'], [], strict=True)
+ self.assertRaises(csv.Error, self._read_test, ['"a'], [], strict=True)
+ self.assertRaises(csv.Error, self._read_test,
+ ['^'], [], escapechar='^', strict=True)
+
def test_read_escape(self):
self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\')
self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\')
diff --git a/Misc/NEWS b/Misc/NEWS
index 3283117d61..ab3c7d447a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,9 @@ Core and Builtins
Library
-------
+- Issue #16013: Fix CSV Reader parsing issue with ending quote characters.
+ Patch by Serhiy Storchaka.
+
- Issue #15421: fix an OverflowError in Calendar.itermonthdates() after
datetime.MAXYEAR. Patch by Cédric Krier.
diff --git a/Modules/_csv.c b/Modules/_csv.c
index 6c564d727b..88d4f9774f 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -759,9 +759,13 @@ Reader_iternext(ReaderObj *self)
lineobj = PyIter_Next(self->input_iter);
if (lineobj == NULL) {
/* End of input OR exception */
- if (!PyErr_Occurred() && self->field_len != 0)
- PyErr_Format(error_obj,
- "newline inside string");
+ if (!PyErr_Occurred() && (self->field_len != 0 ||
+ self->state == IN_QUOTED_FIELD)) {
+ if (self->dialect->strict)
+ PyErr_SetString(error_obj, "unexpected end of data");
+ else if (parse_save_field(self) >= 0)
+ break;
+ }
return NULL;
}
if (!PyUnicode_Check(lineobj)) {