From 59a7b9495b64f0be8992564d30217137f0f0a662 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:26:04 +0100 Subject: [PATCH] [3.12] gh-70835: Clarify error message for CSV file opened with wrong newline (GH-113786) (GH-113905) Based on patch by SilentGhost. (cherry picked from commit 568d220993fa9b4b812ff1b425edd80dbe17dda9) Co-authored-by: Serhiy Storchaka --- Lib/test/test_csv.py | 19 ++++++++++++------- Modules/_csv.c | 3 ++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index bc9961e0831f0e..30383698d04184 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -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', '']]) diff --git a/Modules/_csv.c b/Modules/_csv.c index 9ab2ad266c2739..91cb63628a1f7b 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -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;