Kaydet (Commit) 837d760c authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Check that failed writerow() doesn't produce change a file.

üst 53b542fe
...@@ -130,12 +130,24 @@ class Test_Csv(unittest.TestCase): ...@@ -130,12 +130,24 @@ class Test_Csv(unittest.TestCase):
fileobj.close() fileobj.close()
os.unlink(name) os.unlink(name)
def _write_error_test(self, exc, fields, **kwargs):
fd, name = tempfile.mkstemp()
fileobj = os.fdopen(fd, "w+b")
try:
writer = csv.writer(fileobj, **kwargs)
with self.assertRaises(exc):
writer.writerow(fields)
fileobj.seek(0)
self.assertEqual(fileobj.read(), '')
finally:
fileobj.close()
os.unlink(name)
def test_write_arg_valid(self): 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((), '')
self._write_test([None], '""') self._write_test([None], '""')
self.assertRaises(csv.Error, self._write_test, self._write_error_test(csv.Error, [None], quoting = csv.QUOTE_NONE)
[None], None, quoting = csv.QUOTE_NONE)
# Check that exceptions are passed up the chain # Check that exceptions are passed up the chain
class BadList: class BadList:
def __len__(self): def __len__(self):
...@@ -143,11 +155,11 @@ class Test_Csv(unittest.TestCase): ...@@ -143,11 +155,11 @@ class Test_Csv(unittest.TestCase):
def __getitem__(self, i): def __getitem__(self, i):
if i > 2: if i > 2:
raise IOError raise IOError
self.assertRaises(IOError, self._write_test, BadList(), '') self._write_error_test(IOError, BadList())
class BadItem: class BadItem:
def __str__(self): def __str__(self):
raise IOError raise IOError
self.assertRaises(IOError, self._write_test, [BadItem()], '') self._write_error_test(IOError, [BadItem()])
def test_write_bigfield(self): def test_write_bigfield(self):
# This exercises the buffer realloc functionality # This exercises the buffer realloc functionality
...@@ -157,10 +169,8 @@ class Test_Csv(unittest.TestCase): ...@@ -157,10 +169,8 @@ class Test_Csv(unittest.TestCase):
def test_write_quoting(self): def test_write_quoting(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"') self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
self.assertRaises(csv.Error, self._write_error_test(csv.Error, ['a',1,'p,q'],
self._write_test, quoting = csv.QUOTE_NONE)
['a',1,'p,q'], 'a,1,p,q',
quoting = csv.QUOTE_NONE)
self._write_test(['a',1,'p,q'], 'a,1,"p,q"', self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
quoting = csv.QUOTE_MINIMAL) quoting = csv.QUOTE_MINIMAL)
self._write_test(['a',1,'p,q'], '"a",1,"p,q"', self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
...@@ -173,10 +183,8 @@ class Test_Csv(unittest.TestCase): ...@@ -173,10 +183,8 @@ class Test_Csv(unittest.TestCase):
def test_write_escape(self): def test_write_escape(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"', self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
escapechar='\\') escapechar='\\')
self.assertRaises(csv.Error, self._write_error_test(csv.Error, ['a',1,'p,"q"'],
self._write_test, escapechar=None, doublequote=False)
['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
escapechar=None, doublequote=False)
self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
escapechar='\\', doublequote = False) escapechar='\\', doublequote = False)
self._write_test(['"'], '""""', self._write_test(['"'], '""""',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment