Skip to content

Commit

Permalink
[3.12] pythongh-70835: Clarify error message for CSV file opened with…
Browse files Browse the repository at this point in the history
… wrong newline (pythonGH-113786) (pythonGH-113905)

Based on patch by SilentGhost.
(cherry picked from commit 568d220)

Co-authored-by: Serhiy Storchaka <[email protected]>
  • Loading branch information
miss-islington and serhiy-storchaka authored Jan 10, 2024
1 parent 84677ff commit 59a7b94
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 12 additions & 7 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,18 @@ def test_read_oddinputs(self):
[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', '']])
Expand Down
3 changes: 2 additions & 1 deletion Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,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;
Expand Down

0 comments on commit 59a7b94

Please sign in to comment.