Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 10c476d

Browse files
committed
Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
in issue1395. All other places in this file already use newline=''... Also check that csv.reader is given an iterable returning strings.
1 parent a2d1d7e commit 10c476d

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

Lib/test/test_csv.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import os
88
import unittest
9-
from io import StringIO
9+
from io import StringIO, BytesIO
1010
from tempfile import TemporaryFile
1111
import csv
1212
import gc
@@ -211,6 +211,10 @@ def test_read_oddinputs(self):
211211
['ab\0c'], None, strict = 1)
212212
self._read_test(['"ab"c'], [['abc']], doublequote = 0)
213213

214+
self.assertRaises(csv.Error, self._read_test,
215+
[b'ab\0c'], None)
216+
217+
214218
def test_read_eol(self):
215219
self._read_test(['a,b'], [['a','b']])
216220
self._read_test(['a,b\n'], [['a','b']])
@@ -375,7 +379,7 @@ def test_bad_dialect(self):
375379

376380
class TestCsvBase(unittest.TestCase):
377381
def readerAssertEqual(self, input, expected_result):
378-
with TemporaryFile("w+") as fileobj:
382+
with TemporaryFile("w+", newline='') as fileobj:
379383
fileobj.write(input)
380384
fileobj.seek(0)
381385
reader = csv.reader(fileobj, dialect = self.dialect)

Modules/_csv.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
270270
*target = NULL;
271271
else if (!IS_BASESTRING(src)) {
272272
PyErr_Format(PyExc_TypeError,
273-
"\"%s\" must be an string", name);
273+
"\"%s\" must be a string", name);
274274
return -1;
275275
}
276276
else {
@@ -793,6 +793,16 @@ Reader_iternext(ReaderObj *self)
793793
"newline inside string");
794794
return NULL;
795795
}
796+
if (!PyUnicode_Check(lineobj))
797+
{
798+
PyErr_Format(error_obj,
799+
"Iterator should return strings, "
800+
"not %.200s "
801+
"(did you open the file in text mode?)",
802+
lineobj->ob_type->tp_name
803+
);
804+
return NULL;
805+
}
796806
++self->line_num;
797807
line = PyUnicode_AsUnicode(lineobj);
798808
linelen = PyUnicode_GetSize(lineobj);

0 commit comments

Comments
 (0)