summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_csv.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-06-22 08:21:59 (GMT)
committerGitHub <noreply@github.com>2020-06-22 08:21:59 (GMT)
commitc88239f864a27f673c0f0a9e62d2488563f9d081 (patch)
treeb60c7378df76eef6f7924956a4cf5883f11cbdd9 /Lib/test/test_csv.py
parentcafe1b6e9d3594a34aba50e872d4198296ffaadf (diff)
downloadcpython-c88239f864a27f673c0f0a9e62d2488563f9d081.zip
cpython-c88239f864a27f673c0f0a9e62d2488563f9d081.tar.gz
cpython-c88239f864a27f673c0f0a9e62d2488563f9d081.tar.bz2
bpo-26407: Do not mask errors in csv. (GH-20536)
Unexpected errors in calling the __iter__ method are no longer masked by TypeError in csv.reader(), csv.writer.writerow() and csv.writer.writerows().
Diffstat (limited to 'Lib/test/test_csv.py')
-rw-r--r--Lib/test/test_csv.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index a16d140..d421be0 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -14,6 +14,12 @@ from itertools import permutations
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
@@ -40,9 +46,15 @@ class Test_Csv(unittest.TestCase):
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)
@@ -141,6 +153,7 @@ class Test_Csv(unittest.TestCase):
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;
@@ -230,6 +243,12 @@ class Test_Csv(unittest.TestCase):
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
@@ -334,7 +353,6 @@ class Test_Csv(unittest.TestCase):
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)