From 0b4e355b8e7729d1e3a67da8539b3701f4e546ff Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 25 Mar 2015 19:16:15 +0200 Subject: [PATCH] Check that failed writerow() doesn't produce change a file. --- Lib/test/test_csv.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 7e2485f954f..65449aebe40 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -124,12 +124,19 @@ class Test_Csv(unittest.TestCase): self.assertEqual(fileobj.read(), expect + writer.dialect.lineterminator) + def _write_error_test(self, exc, fields, **kwargs): + with TemporaryFile("w+", newline='') as fileobj: + writer = csv.writer(fileobj, **kwargs) + with self.assertRaises(exc): + writer.writerow(fields) + fileobj.seek(0) + self.assertEqual(fileobj.read(), '') + def test_write_arg_valid(self): - self.assertRaises(csv.Error, self._write_test, None, '') + self._write_error_test(csv.Error, None) self._write_test((), '') self._write_test([None], '""') - self.assertRaises(csv.Error, self._write_test, - [None], None, quoting = csv.QUOTE_NONE) + self._write_error_test(csv.Error, [None], quoting = csv.QUOTE_NONE) # Check that exceptions are passed up the chain class BadList: def __len__(self): @@ -137,11 +144,11 @@ class Test_Csv(unittest.TestCase): def __getitem__(self, i): if i > 2: raise OSError - self.assertRaises(OSError, self._write_test, BadList(), '') + self._write_error_test(OSError, BadList()) class BadItem: def __str__(self): raise OSError - self.assertRaises(OSError, self._write_test, [BadItem()], '') + self._write_error_test(OSError, [BadItem()]) def test_write_bigfield(self): # This exercises the buffer realloc functionality @@ -151,10 +158,8 @@ class Test_Csv(unittest.TestCase): def test_write_quoting(self): self._write_test(['a',1,'p,q'], 'a,1,"p,q"') - self.assertRaises(csv.Error, - self._write_test, - ['a',1,'p,q'], 'a,1,p,q', - quoting = csv.QUOTE_NONE) + self._write_error_test(csv.Error, ['a',1,'p,q'], + quoting = csv.QUOTE_NONE) self._write_test(['a',1,'p,q'], 'a,1,"p,q"', quoting = csv.QUOTE_MINIMAL) self._write_test(['a',1,'p,q'], '"a",1,"p,q"', @@ -167,10 +172,8 @@ class Test_Csv(unittest.TestCase): def test_write_escape(self): self._write_test(['a',1,'p,q'], 'a,1,"p,q"', escapechar='\\') - self.assertRaises(csv.Error, - self._write_test, - ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', - escapechar=None, doublequote=False) + self._write_error_test(csv.Error, ['a',1,'p,"q"'], + escapechar=None, doublequote=False) self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', escapechar='\\', doublequote = False) self._write_test(['"'], '""""', @@ -1063,7 +1066,6 @@ class TestUnicode(unittest.TestCase): self.assertEqual(fileobj.read(), expected) - def test_main(): mod = sys.modules[__name__] support.run_unittest(