From 99a7bdc646f1a7020231156e4aca19b949d8ee71 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:06:39 +0100 Subject: [PATCH] [3.11] gh-70835: Clarify error message for CSV file opened with wrong newline (GH-113786) (GH-113906) 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 05653a268417..1bf487bcf9bd 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -305,13 +305,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', '']]) diff --git a/Modules/_csv.c b/Modules/_csv.c index 7314d9c70537..407d6f03540d 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; -- 2.47.3