8000 [3.9] bpo-26407: Do not mask errors in csv. (GH-20536) by miss-islington · Pull Request #21047 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.9] bpo-26407: Do not mask errors in csv. (GH-20536) #21047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
from textwrap import dedent
from collections import OrderedDict


class BadIterable:
def __iter__(self):
raise OSError


class Test_Csv(unittest.TestCase):
"""
Test the underlying C csv parser in ways that are not appropriate
Expand All @@ -40,9 +46,15 @@ def _test_arg_valid(self, ctor, arg):

def test_reader_arg_valid(self):
self._test_arg_valid(csv.reader, [])
self.assertRaises(OSError, csv.reader, BadIterable())

def test_writer_arg_valid(self):
self._test_arg_valid(csv.writer, StringIO())
class BadWriter:
@property
def write(self):
raise OSError
self.assertRaises(OSError, csv.writer, BadWriter())

def _test_default_attrs(self, ctor, *args):
obj = ctor(*args)
Expand Down Expand Up @@ -141,6 +153,7 @@ def test_write_arg_valid(self):
self._write_test([None], '""')
self._write_error_test(csv.Error, [None], quoting = csv.QUOTE_NONE)
# Check that exceptions are passed up the chain
self._write_error_test(OSError, BadIterable())
class BadList:
def __len__(self):
return 10;
Expand Down Expand Up @@ -230,6 +243,12 @@ def test_writerows_with_none(self):
fileobj.seek(0)
self.assertEqual(fileobj.read(), 'a\r\n""\r\n')

def test_writerows_errors(self):
with TemporaryFile("w+", newline='') as fileobj:
writer = csv.writer(fileobj)
self.assertRaises(TypeError, writer.writerows, None)
self.assertRaises(OSError, writer.writerows, BadIterable())

@support.cpython_only
def test_writerows_legacy_strings(self):
import _testcapi
Expand Down Expand Up @@ -334,7 +353,6 @@ def test_read_linenum(self):
def test_roundtrip_quoteed_newlines(self):
with TemporaryFile("w+", newline='') as fileobj:
writer = csv.writer(fileobj)
self.assertRaises(TypeError, writer.writerows, None)
rows = [['a\nb','b'],['c','x\r\nd']]
writer.writerows(rows)
fileobj.seek(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Unexpected errors in calling the ``__iter__`` method are no longer masked
by ``TypeError`` in :func:`csv.reader`, :func:`csv.writer.writerow` and
:meth:`csv.writer.writerows`.
16 changes: 8 additions & 8 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,6 @@ csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
}
self->input_iter = PyObject_GetIter(iterator);
if (self->input_iter == NULL) {
PyErr_SetString(PyExc_TypeError,
"argument 1 must be an iterator");
Py_DECREF(self);
return NULL;
}
Expand Down Expand Up @@ -1171,10 +1169,14 @@ csv_writerow(WriterObj *self, PyObject *seq)
PyObject *iter, *field, *line, *result;

iter = PyObject_GetIter(seq);
if (iter == NULL)
return PyErr_Format(_csvstate_global->error_obj,
"iterable expected, not %.200s",
Py_TYPE(seq)->tp_name);
if (iter == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Format(_csvstate_global->error_obj,
"iterable expected, not %.200s",
Py_TYPE(seq)->tp_name);
}
return NULL;
}

/* Join all fields in internal buffer.
*/
Expand Down Expand Up @@ -1264,8 +1266,6 @@ csv_writerows(WriterObj *self, PyObject *seqseq)

row_iter = PyObject_GetIter(seqseq);
if (row_iter == NULL) {
PyErr_SetString(PyExc_TypeError,
"writerows() argument must be iterable");
return NULL;
}
while ((row_obj = PyIter_Next(row_iter))) {
Expand Down
0