gh-70835: Clarify error message for CSV file opened with wrong newline (GH-113786)

Based on patch by SilentGhost.
This commit is contained in:
Serhiy Storchaka 2024-01-10 14:52:29 +02:00 committed by GitHub
parent aef4a1203c
commit 568d220993
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View File

@ -297,13 +297,18 @@ class Test_Csv(unittest.TestCase):
[b'abc'], None)
def test_read_eol(self):
self._read_test(['a,b'], [['a','b']])
self._read_test(['a,b\n'], [['a','b']])
self._read_test(['a,b\r\n'], [['a','b']])
self._read_test(['a,b\r'], [['a','b']])
self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], [])
self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], [])
self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], [])
self._read_test(['a,b', 'c,d'], [['a','b'], ['c','d']])
self._read_test(['a,b\n', 'c,d\n'], [['a','b'], ['c','d']])
self._read_test(['a,b\r\n', 'c,d\r\n'], [['a','b'], ['c','d']])
self._read_test(['a,b\r', 'c,d\r'], [['a','b'], ['c','d']])
errmsg = "with newline=''"
with self.assertRaisesRegex(csv.Error, errmsg):
next(csv.reader(['a,b\rc,d']))
with self.assertRaisesRegex(csv.Error, errmsg):
next(csv.reader(['a,b\nc,d']))
with self.assertRaisesRegex(csv.Error, errmsg):
next(csv.reader(['a,b\r\nc,d']))
def test_read_eof(self):
self._read_test(['a,"'], [['a', '']])

View File

@ -837,7 +837,8 @@ parse_process_char(ReaderObj *self, _csvstate *module_state, Py_UCS4 c)
self->state = START_RECORD;
else {
PyErr_Format(module_state->error_obj,
"new-line character seen in unquoted field - do you need to open the file in universal-newline mode?");
"new-line character seen in unquoted field - "
"do you need to open the file with newline=''?");
return -1;
}
break;