Guido van Rossum | d77d699 | 2007-07-16 23:10:57 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 2 | # Copyright (C) 2001,2002 Python Software Foundation |
| 3 | # csv package unit tests |
| 4 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 5 | import io |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 6 | import sys |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 7 | import os |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 8 | import unittest |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 9 | from io import StringIO |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 10 | from tempfile import TemporaryFile |
Skip Montanaro | 594adac | 2003-04-10 17:16:15 +0000 | [diff] [blame] | 11 | import csv |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 12 | import gc |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 13 | from test import test_support |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 14 | |
| 15 | class Test_Csv(unittest.TestCase): |
| 16 | """ |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 17 | Test the underlying C csv parser in ways that are not appropriate |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 18 | from the high level interface. Further tests of this nature are done |
| 19 | in TestDialectRegistry. |
| 20 | """ |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 21 | def _test_arg_valid(self, ctor, arg): |
| 22 | self.assertRaises(TypeError, ctor) |
| 23 | self.assertRaises(TypeError, ctor, None) |
| 24 | self.assertRaises(TypeError, ctor, arg, bad_attr = 0) |
| 25 | self.assertRaises(TypeError, ctor, arg, delimiter = 0) |
| 26 | self.assertRaises(TypeError, ctor, arg, delimiter = 'XX') |
| 27 | self.assertRaises(csv.Error, ctor, arg, 'foo') |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 28 | self.assertRaises(TypeError, ctor, arg, delimiter=None) |
| 29 | self.assertRaises(TypeError, ctor, arg, delimiter=1) |
| 30 | self.assertRaises(TypeError, ctor, arg, quotechar=1) |
| 31 | self.assertRaises(TypeError, ctor, arg, lineterminator=None) |
| 32 | self.assertRaises(TypeError, ctor, arg, lineterminator=1) |
| 33 | self.assertRaises(TypeError, ctor, arg, quoting=None) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 34 | self.assertRaises(TypeError, ctor, arg, |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 35 | quoting=csv.QUOTE_ALL, quotechar='') |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 36 | self.assertRaises(TypeError, ctor, arg, |
Andrew McNamara | 5d45a8d | 2005-01-12 08:16:17 +0000 | [diff] [blame] | 37 | quoting=csv.QUOTE_ALL, quotechar=None) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 38 | |
| 39 | def test_reader_arg_valid(self): |
| 40 | self._test_arg_valid(csv.reader, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 41 | |
| 42 | def test_writer_arg_valid(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 43 | self._test_arg_valid(csv.writer, StringIO()) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 44 | |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 45 | def _test_default_attrs(self, ctor, *args): |
| 46 | obj = ctor(*args) |
| 47 | # Check defaults |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 48 | self.assertEqual(obj.dialect.delimiter, ',') |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 49 | self.assertEqual(obj.dialect.doublequote, True) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 50 | self.assertEqual(obj.dialect.escapechar, None) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 51 | self.assertEqual(obj.dialect.lineterminator, "\r\n") |
| 52 | self.assertEqual(obj.dialect.quotechar, '"') |
| 53 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) |
| 54 | self.assertEqual(obj.dialect.skipinitialspace, False) |
| 55 | self.assertEqual(obj.dialect.strict, False) |
| 56 | # Try deleting or changing attributes (they are read-only) |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 57 | self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter') |
| 58 | self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':') |
Barry Warsaw | b180c06 | 2005-04-20 19:41:36 +0000 | [diff] [blame] | 59 | self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting') |
| 60 | self.assertRaises(AttributeError, setattr, obj.dialect, |
| 61 | 'quoting', None) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 62 | |
| 63 | def test_reader_attrs(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 64 | self._test_default_attrs(csv.reader, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 65 | |
| 66 | def test_writer_attrs(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 67 | self._test_default_attrs(csv.writer, StringIO()) |
| 68 | |
| 69 | def _test_kw_attrs(self, ctor, *args): |
| 70 | # Now try with alternate options |
| 71 | kwargs = dict(delimiter=':', doublequote=False, escapechar='\\', |
| 72 | lineterminator='\r', quotechar='*', |
| 73 | quoting=csv.QUOTE_NONE, skipinitialspace=True, |
| 74 | strict=True) |
| 75 | obj = ctor(*args, **kwargs) |
| 76 | self.assertEqual(obj.dialect.delimiter, ':') |
| 77 | self.assertEqual(obj.dialect.doublequote, False) |
| 78 | self.assertEqual(obj.dialect.escapechar, '\\') |
| 79 | self.assertEqual(obj.dialect.lineterminator, "\r") |
| 80 | self.assertEqual(obj.dialect.quotechar, '*') |
| 81 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE) |
| 82 | self.assertEqual(obj.dialect.skipinitialspace, True) |
| 83 | self.assertEqual(obj.dialect.strict, True) |
| 84 | |
| 85 | def test_reader_kw_attrs(self): |
| 86 | self._test_kw_attrs(csv.reader, []) |
| 87 | |
| 88 | def test_writer_kw_attrs(self): |
| 89 | self._test_kw_attrs(csv.writer, StringIO()) |
| 90 | |
| 91 | def _test_dialect_attrs(self, ctor, *args): |
| 92 | # Now try with dialect-derived options |
| 93 | class dialect: |
| 94 | delimiter='-' |
| 95 | doublequote=False |
| 96 | escapechar='^' |
| 97 | lineterminator='$' |
| 98 | quotechar='#' |
| 99 | quoting=csv.QUOTE_ALL |
| 100 | skipinitialspace=True |
| 101 | strict=False |
| 102 | args = args + (dialect,) |
| 103 | obj = ctor(*args) |
| 104 | self.assertEqual(obj.dialect.delimiter, '-') |
| 105 | self.assertEqual(obj.dialect.doublequote, False) |
| 106 | self.assertEqual(obj.dialect.escapechar, '^') |
| 107 | self.assertEqual(obj.dialect.lineterminator, "$") |
| 108 | self.assertEqual(obj.dialect.quotechar, '#') |
| 109 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL) |
| 110 | self.assertEqual(obj.dialect.skipinitialspace, True) |
| 111 | self.assertEqual(obj.dialect.strict, False) |
| 112 | |
| 113 | def test_reader_dialect_attrs(self): |
| 114 | self._test_dialect_attrs(csv.reader, []) |
| 115 | |
| 116 | def test_writer_dialect_attrs(self): |
| 117 | self._test_dialect_attrs(csv.writer, StringIO()) |
| 118 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 119 | |
| 120 | def _write_test(self, fields, expect, **kwargs): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 121 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 122 | writer = csv.writer(fileobj, **kwargs) |
| 123 | writer.writerow(fields) |
| 124 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 125 | self.assertEqual(fileobj.read(), |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 126 | expect + writer.dialect.lineterminator) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 127 | |
| 128 | def test_write_arg_valid(self): |
| 129 | self.assertRaises(csv.Error, self._write_test, None, '') |
| 130 | self._write_test((), '') |
| 131 | self._write_test([None], '""') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 132 | self.assertRaises(csv.Error, self._write_test, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 133 | [None], None, quoting = csv.QUOTE_NONE) |
| 134 | # Check that exceptions are passed up the chain |
| 135 | class BadList: |
| 136 | def __len__(self): |
| 137 | return 10; |
| 138 | def __getitem__(self, i): |
| 139 | if i > 2: |
| 140 | raise IOError |
| 141 | self.assertRaises(IOError, self._write_test, BadList(), '') |
| 142 | class BadItem: |
| 143 | def __str__(self): |
| 144 | raise IOError |
| 145 | self.assertRaises(IOError, self._write_test, [BadItem()], '') |
| 146 | |
| 147 | def test_write_bigfield(self): |
| 148 | # This exercises the buffer realloc functionality |
| 149 | bigstring = 'X' * 50000 |
| 150 | self._write_test([bigstring,bigstring], '%s,%s' % \ |
| 151 | (bigstring, bigstring)) |
| 152 | |
| 153 | def test_write_quoting(self): |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 154 | self._write_test(['a',1,'p,q'], 'a,1,"p,q"') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 155 | self.assertRaises(csv.Error, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 156 | self._write_test, |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 157 | ['a',1,'p,q'], 'a,1,p,q', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 158 | quoting = csv.QUOTE_NONE) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 159 | self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 160 | quoting = csv.QUOTE_MINIMAL) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 161 | self._write_test(['a',1,'p,q'], '"a",1,"p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 162 | quoting = csv.QUOTE_NONNUMERIC) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 163 | self._write_test(['a',1,'p,q'], '"a","1","p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 164 | quoting = csv.QUOTE_ALL) |
| 165 | |
| 166 | def test_write_escape(self): |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 167 | self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 168 | escapechar='\\') |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 169 | self.assertRaises(csv.Error, |
| 170 | self._write_test, |
| 171 | ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 172 | escapechar=None, doublequote=False) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 173 | self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
| 174 | escapechar='\\', doublequote = False) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 175 | self._write_test(['"'], '""""', |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 176 | escapechar='\\', quoting = csv.QUOTE_MINIMAL) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 177 | self._write_test(['"'], '\\"', |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 178 | escapechar='\\', quoting = csv.QUOTE_MINIMAL, |
| 179 | doublequote = False) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 180 | self._write_test(['"'], '\\"', |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 181 | escapechar='\\', quoting = csv.QUOTE_NONE) |
| 182 | self._write_test(['a',1,'p,q'], 'a,1,p\\,q', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 183 | escapechar='\\', quoting = csv.QUOTE_NONE) |
| 184 | |
| 185 | def test_writerows(self): |
| 186 | class BrokenFile: |
| 187 | def write(self, buf): |
| 188 | raise IOError |
| 189 | writer = csv.writer(BrokenFile()) |
| 190 | self.assertRaises(IOError, writer.writerows, [['a']]) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 192 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 193 | writer = csv.writer(fileobj) |
| 194 | self.assertRaises(TypeError, writer.writerows, None) |
| 195 | writer.writerows([['a','b'],['c','d']]) |
| 196 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 197 | self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n") |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 198 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 199 | def _read_test(self, input, expect, **kwargs): |
| 200 | reader = csv.reader(input, **kwargs) |
| 201 | result = list(reader) |
| 202 | self.assertEqual(result, expect) |
| 203 | |
| 204 | def test_read_oddinputs(self): |
| 205 | self._read_test([], []) |
| 206 | self._read_test([''], [[]]) |
| 207 | self.assertRaises(csv.Error, self._read_test, |
| 208 | ['"ab"c'], None, strict = 1) |
| 209 | # cannot handle null bytes for the moment |
| 210 | self.assertRaises(csv.Error, self._read_test, |
| 211 | ['ab\0c'], None, strict = 1) |
| 212 | self._read_test(['"ab"c'], [['abc']], doublequote = 0) |
| 213 | |
| 214 | def test_read_eol(self): |
| 215 | self._read_test(['a,b'], [['a','b']]) |
| 216 | self._read_test(['a,b\n'], [['a','b']]) |
| 217 | self._read_test(['a,b\r\n'], [['a','b']]) |
| 218 | self._read_test(['a,b\r'], [['a','b']]) |
| 219 | self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], []) |
| 220 | self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], []) |
| 221 | self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], []) |
| 222 | |
| 223 | def test_read_escape(self): |
Andrew McNamara | 36a7691 | 2005-01-10 01:04:40 +0000 | [diff] [blame] | 224 | self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 225 | self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') |
| 226 | self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') |
Andrew McNamara | 36a7691 | 2005-01-10 01:04:40 +0000 | [diff] [blame] | 227 | self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 228 | self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') |
| 229 | self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') |
| 230 | |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 231 | def test_read_quoting(self): |
| 232 | self._read_test(['1,",3,",5'], [['1', ',3,', '5']]) |
| 233 | self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
| 234 | quotechar=None, escapechar='\\') |
| 235 | self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
| 236 | quoting=csv.QUOTE_NONE, escapechar='\\') |
Andrew McNamara | 0f0599d | 2005-01-12 09:45:18 +0000 | [diff] [blame] | 237 | # will this fail where locale uses comma for decimals? |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 238 | self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]], |
Andrew McNamara | 0f0599d | 2005-01-12 09:45:18 +0000 | [diff] [blame] | 239 | quoting=csv.QUOTE_NONNUMERIC) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 240 | self.assertRaises(ValueError, self._read_test, |
Andrew McNamara | 0f0599d | 2005-01-12 09:45:18 +0000 | [diff] [blame] | 241 | ['abc,3'], [[]], |
| 242 | quoting=csv.QUOTE_NONNUMERIC) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 243 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 244 | def test_read_bigfield(self): |
Andrew McNamara | e4d05c4 | 2005-01-11 07:32:02 +0000 | [diff] [blame] | 245 | # This exercises the buffer realloc functionality and field size |
| 246 | # limits. |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 247 | limit = csv.field_size_limit() |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 248 | try: |
| 249 | size = 50000 |
| 250 | bigstring = 'X' * size |
| 251 | bigline = '%s,%s' % (bigstring, bigstring) |
| 252 | self._read_test([bigline], [[bigstring, bigstring]]) |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 253 | csv.field_size_limit(size) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 254 | self._read_test([bigline], [[bigstring, bigstring]]) |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 255 | self.assertEqual(csv.field_size_limit(), size) |
| 256 | csv.field_size_limit(size-1) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 257 | self.assertRaises(csv.Error, self._read_test, [bigline], []) |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 258 | self.assertRaises(TypeError, csv.field_size_limit, None) |
| 259 | self.assertRaises(TypeError, csv.field_size_limit, 1, None) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 260 | finally: |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 261 | csv.field_size_limit(limit) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 262 | |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 263 | def test_read_linenum(self): |
| 264 | r = csv.reader(['line,1', 'line,2', 'line,3']) |
| 265 | self.assertEqual(r.line_num, 0) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 266 | next(r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 267 | self.assertEqual(r.line_num, 1) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 268 | next(r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 269 | self.assertEqual(r.line_num, 2) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 270 | next(r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 271 | self.assertEqual(r.line_num, 3) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 272 | self.assertRaises(StopIteration, next, r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 273 | self.assertEqual(r.line_num, 3) |
| 274 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 275 | class TestDialectRegistry(unittest.TestCase): |
| 276 | def test_registry_badargs(self): |
| 277 | self.assertRaises(TypeError, csv.list_dialects, None) |
| 278 | self.assertRaises(TypeError, csv.get_dialect) |
| 279 | self.assertRaises(csv.Error, csv.get_dialect, None) |
| 280 | self.assertRaises(csv.Error, csv.get_dialect, "nonesuch") |
| 281 | self.assertRaises(TypeError, csv.unregister_dialect) |
| 282 | self.assertRaises(csv.Error, csv.unregister_dialect, None) |
| 283 | self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch") |
| 284 | self.assertRaises(TypeError, csv.register_dialect, None) |
| 285 | self.assertRaises(TypeError, csv.register_dialect, None, None) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 286 | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", 0, 0) |
| 287 | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", |
| 288 | badargument=None) |
| 289 | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", |
| 290 | quoting=None) |
| 291 | self.assertRaises(TypeError, csv.register_dialect, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 292 | |
| 293 | def test_registry(self): |
| 294 | class myexceltsv(csv.excel): |
| 295 | delimiter = "\t" |
| 296 | name = "myexceltsv" |
| 297 | expected_dialects = csv.list_dialects() + [name] |
| 298 | expected_dialects.sort() |
| 299 | csv.register_dialect(name, myexceltsv) |
| 300 | try: |
Andrew McNamara | 8662597 | 2005-01-11 01:28:33 +0000 | [diff] [blame] | 301 | self.failUnless(csv.get_dialect(name).delimiter, '\t') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 302 | got_dialects = csv.list_dialects() |
| 303 | got_dialects.sort() |
| 304 | self.assertEqual(expected_dialects, got_dialects) |
| 305 | finally: |
| 306 | csv.unregister_dialect(name) |
| 307 | |
Andrew McNamara | 8662597 | 2005-01-11 01:28:33 +0000 | [diff] [blame] | 308 | def test_register_kwargs(self): |
| 309 | name = 'fedcba' |
| 310 | csv.register_dialect(name, delimiter=';') |
| 311 | try: |
| 312 | self.failUnless(csv.get_dialect(name).delimiter, '\t') |
| 313 | self.failUnless(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z']) |
| 314 | finally: |
| 315 | csv.unregister_dialect(name) |
| 316 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 317 | def test_incomplete_dialect(self): |
| 318 | class myexceltsv(csv.Dialect): |
| 319 | delimiter = "\t" |
| 320 | self.assertRaises(csv.Error, myexceltsv) |
| 321 | |
| 322 | def test_space_dialect(self): |
| 323 | class space(csv.excel): |
| 324 | delimiter = " " |
| 325 | quoting = csv.QUOTE_NONE |
| 326 | escapechar = "\\" |
| 327 | |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 328 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 329 | fileobj.write("abc def\nc1ccccc1 benzene\n") |
| 330 | fileobj.seek(0) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 331 | reader = csv.reader(fileobj, dialect=space()) |
| 332 | self.assertEqual(next(reader), ["abc", "def"]) |
| 333 | self.assertEqual(next(reader), ["c1ccccc1", "benzene"]) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 334 | |
| 335 | def compare_dialect_123(self, expected, *writeargs, **kwwriteargs): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 336 | |
| 337 | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
| 338 | |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 339 | writer = csv.writer(fileobj, *writeargs, **kwwriteargs) |
| 340 | writer.writerow([1,2,3]) |
| 341 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 342 | self.assertEqual(fileobj.read(), expected) |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 343 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 344 | def test_dialect_apply(self): |
| 345 | class testA(csv.excel): |
| 346 | delimiter = "\t" |
| 347 | class testB(csv.excel): |
| 348 | delimiter = ":" |
| 349 | class testC(csv.excel): |
| 350 | delimiter = "|" |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 351 | class testUni(csv.excel): |
| 352 | delimiter = "\u039B" |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 353 | |
| 354 | csv.register_dialect('testC', testC) |
| 355 | try: |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 356 | self.compare_dialect_123("1,2,3\r\n") |
| 357 | self.compare_dialect_123("1\t2\t3\r\n", testA) |
| 358 | self.compare_dialect_123("1:2:3\r\n", dialect=testB()) |
| 359 | self.compare_dialect_123("1|2|3\r\n", dialect='testC') |
| 360 | self.compare_dialect_123("1;2;3\r\n", dialect=testA, |
| 361 | delimiter=';') |
| 362 | self.compare_dialect_123("1\u039B2\u039B3\r\n", |
| 363 | dialect=testUni) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 364 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 365 | finally: |
| 366 | csv.unregister_dialect('testC') |
| 367 | |
| 368 | def test_bad_dialect(self): |
| 369 | # Unknown parameter |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 370 | self.assertRaises(TypeError, csv.reader, [], bad_attr = 0) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 371 | # Bad values |
| 372 | self.assertRaises(TypeError, csv.reader, [], delimiter = None) |
| 373 | self.assertRaises(TypeError, csv.reader, [], quoting = -1) |
| 374 | self.assertRaises(TypeError, csv.reader, [], quoting = 100) |
| 375 | |
| 376 | class TestCsvBase(unittest.TestCase): |
| 377 | def readerAssertEqual(self, input, expected_result): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 378 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 379 | fileobj.write(input) |
| 380 | fileobj.seek(0) |
| 381 | reader = csv.reader(fileobj, dialect = self.dialect) |
| 382 | fields = list(reader) |
| 383 | self.assertEqual(fields, expected_result) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 384 | |
| 385 | def writerAssertEqual(self, input, expected_result): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 386 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 387 | writer = csv.writer(fileobj, dialect = self.dialect) |
| 388 | writer.writerows(input) |
| 389 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 390 | self.assertEqual(fileobj.read(), expected_result) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 391 | |
| 392 | class TestDialectExcel(TestCsvBase): |
| 393 | dialect = 'excel' |
| 394 | |
| 395 | def test_single(self): |
| 396 | self.readerAssertEqual('abc', [['abc']]) |
| 397 | |
| 398 | def test_simple(self): |
| 399 | self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']]) |
| 400 | |
| 401 | def test_blankline(self): |
| 402 | self.readerAssertEqual('', []) |
| 403 | |
| 404 | def test_empty_fields(self): |
| 405 | self.readerAssertEqual(',', [['', '']]) |
| 406 | |
| 407 | def test_singlequoted(self): |
| 408 | self.readerAssertEqual('""', [['']]) |
| 409 | |
| 410 | def test_singlequoted_left_empty(self): |
| 411 | self.readerAssertEqual('"",', [['','']]) |
| 412 | |
| 413 | def test_singlequoted_right_empty(self): |
| 414 | self.readerAssertEqual(',""', [['','']]) |
| 415 | |
| 416 | def test_single_quoted_quote(self): |
| 417 | self.readerAssertEqual('""""', [['"']]) |
| 418 | |
| 419 | def test_quoted_quotes(self): |
| 420 | self.readerAssertEqual('""""""', [['""']]) |
| 421 | |
| 422 | def test_inline_quote(self): |
| 423 | self.readerAssertEqual('a""b', [['a""b']]) |
| 424 | |
| 425 | def test_inline_quotes(self): |
| 426 | self.readerAssertEqual('a"b"c', [['a"b"c']]) |
| 427 | |
| 428 | def test_quotes_and_more(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 429 | # Excel would never write a field containing '"a"b', but when |
| 430 | # reading one, it will return 'ab'. |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 431 | self.readerAssertEqual('"a"b', [['ab']]) |
| 432 | |
| 433 | def test_lone_quote(self): |
| 434 | self.readerAssertEqual('a"b', [['a"b']]) |
| 435 | |
| 436 | def test_quote_and_quote(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 437 | # Excel would never write a field containing '"a" "b"', but when |
| 438 | # reading one, it will return 'a "b"'. |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 439 | self.readerAssertEqual('"a" "b"', [['a "b"']]) |
| 440 | |
| 441 | def test_space_and_quote(self): |
| 442 | self.readerAssertEqual(' "a"', [[' "a"']]) |
| 443 | |
| 444 | def test_quoted(self): |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 445 | self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', |
| 446 | [['1', '2', '3', |
| 447 | 'I think, therefore I am', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 448 | '5', '6']]) |
| 449 | |
| 450 | def test_quoted_quote(self): |
| 451 | self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"', |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 452 | [['1', '2', '3', |
| 453 | '"I see," said the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 454 | 'as he picked up his hammer and saw']]) |
| 455 | |
| 456 | def test_quoted_nl(self): |
| 457 | input = '''\ |
| 458 | 1,2,3,"""I see,"" |
| 459 | said the blind man","as he picked up his |
| 460 | hammer and saw" |
| 461 | 9,8,7,6''' |
| 462 | self.readerAssertEqual(input, |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 463 | [['1', '2', '3', |
| 464 | '"I see,"\nsaid the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 465 | 'as he picked up his\nhammer and saw'], |
| 466 | ['9','8','7','6']]) |
| 467 | |
| 468 | def test_dubious_quote(self): |
| 469 | self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']]) |
| 470 | |
| 471 | def test_null(self): |
| 472 | self.writerAssertEqual([], '') |
| 473 | |
| 474 | def test_single(self): |
| 475 | self.writerAssertEqual([['abc']], 'abc\r\n') |
| 476 | |
| 477 | def test_simple(self): |
| 478 | self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n') |
| 479 | |
| 480 | def test_quotes(self): |
| 481 | self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n') |
| 482 | |
| 483 | def test_quote_fieldsep(self): |
| 484 | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
| 485 | |
| 486 | def test_newlines(self): |
| 487 | self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n') |
| 488 | |
| 489 | class EscapedExcel(csv.excel): |
| 490 | quoting = csv.QUOTE_NONE |
| 491 | escapechar = '\\' |
| 492 | |
| 493 | class TestEscapedExcel(TestCsvBase): |
| 494 | dialect = EscapedExcel() |
| 495 | |
| 496 | def test_escape_fieldsep(self): |
| 497 | self.writerAssertEqual([['abc,def']], 'abc\\,def\r\n') |
| 498 | |
| 499 | def test_read_escape_fieldsep(self): |
| 500 | self.readerAssertEqual('abc\\,def\r\n', [['abc,def']]) |
| 501 | |
| 502 | class QuotedEscapedExcel(csv.excel): |
| 503 | quoting = csv.QUOTE_NONNUMERIC |
| 504 | escapechar = '\\' |
| 505 | |
| 506 | class TestQuotedEscapedExcel(TestCsvBase): |
| 507 | dialect = QuotedEscapedExcel() |
| 508 | |
| 509 | def test_write_escape_fieldsep(self): |
| 510 | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
| 511 | |
| 512 | def test_read_escape_fieldsep(self): |
| 513 | self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']]) |
| 514 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 515 | class TestDictFields(unittest.TestCase): |
| 516 | ### "long" means the row is longer than the number of fieldnames |
| 517 | ### "short" means there are fewer elements in the row than fieldnames |
| 518 | def test_write_simple_dict(self): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 519 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 520 | writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
| 521 | writer.writerow({"f1": 10, "f3": "abc"}) |
| 522 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 523 | self.assertEqual(fileobj.read(), "10,,abc\r\n") |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 524 | |
| 525 | def test_write_no_fields(self): |
| 526 | fileobj = StringIO() |
| 527 | self.assertRaises(TypeError, csv.DictWriter, fileobj) |
| 528 | |
| 529 | def test_read_dict_fields(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 530 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 531 | fileobj.write("1,2,abc\r\n") |
| 532 | fileobj.seek(0) |
| 533 | reader = csv.DictReader(fileobj, |
| 534 | fieldnames=["f1", "f2", "f3"]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 535 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 536 | |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 537 | def test_read_dict_no_fieldnames(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 538 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 539 | fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
| 540 | fileobj.seek(0) |
| 541 | reader = csv.DictReader(fileobj) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 542 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 543 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 544 | def test_read_long(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 545 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 546 | fileobj.write("1,2,abc,4,5,6\r\n") |
| 547 | fileobj.seek(0) |
| 548 | reader = csv.DictReader(fileobj, |
| 549 | fieldnames=["f1", "f2"]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 550 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 551 | None: ["abc", "4", "5", "6"]}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 552 | |
| 553 | def test_read_long_with_rest(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 554 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 555 | fileobj.write("1,2,abc,4,5,6\r\n") |
| 556 | fileobj.seek(0) |
| 557 | reader = csv.DictReader(fileobj, |
| 558 | fieldnames=["f1", "f2"], restkey="_rest") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 559 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 560 | "_rest": ["abc", "4", "5", "6"]}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 561 | |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 562 | def test_read_long_with_rest_no_fieldnames(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 563 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 564 | fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n") |
| 565 | fileobj.seek(0) |
| 566 | reader = csv.DictReader(fileobj, restkey="_rest") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 567 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 568 | "_rest": ["abc", "4", "5", "6"]}) |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 569 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 570 | def test_read_short(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 571 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 572 | fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n") |
| 573 | fileobj.seek(0) |
| 574 | reader = csv.DictReader(fileobj, |
| 575 | fieldnames="1 2 3 4 5 6".split(), |
| 576 | restval="DEFAULT") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 577 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 578 | "4": '4', "5": '5', "6": '6'}) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 579 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 580 | "4": 'DEFAULT', "5": 'DEFAULT', |
| 581 | "6": 'DEFAULT'}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 582 | |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 583 | def test_read_multi(self): |
| 584 | sample = [ |
| 585 | '2147483648,43.0e12,17,abc,def\r\n', |
| 586 | '147483648,43.0e2,17,abc,def\r\n', |
| 587 | '47483648,43.0,170,abc,def\r\n' |
| 588 | ] |
| 589 | |
| 590 | reader = csv.DictReader(sample, |
| 591 | fieldnames="i1 float i2 s1 s2".split()) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 592 | self.assertEqual(next(reader), {"i1": '2147483648', |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 593 | "float": '43.0e12', |
| 594 | "i2": '17', |
| 595 | "s1": 'abc', |
| 596 | "s2": 'def'}) |
| 597 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 598 | def test_read_with_blanks(self): |
| 599 | reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n", |
| 600 | "1,2,abc,4,5,6\r\n"], |
| 601 | fieldnames="1 2 3 4 5 6".split()) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 602 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 603 | "4": '4', "5": '5', "6": '6'}) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 604 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 605 | "4": '4', "5": '5', "6": '6'}) |
| 606 | |
Skip Montanaro | 3f7a948 | 2003-09-06 19:52:12 +0000 | [diff] [blame] | 607 | def test_read_semi_sep(self): |
| 608 | reader = csv.DictReader(["1;2;abc;4;5;6\r\n"], |
| 609 | fieldnames="1 2 3 4 5 6".split(), |
| 610 | delimiter=';') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 611 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | 3f7a948 | 2003-09-06 19:52:12 +0000 | [diff] [blame] | 612 | "4": '4', "5": '5', "6": '6'}) |
| 613 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 614 | class TestArrayWrites(unittest.TestCase): |
| 615 | def test_int_write(self): |
| 616 | import array |
| 617 | contents = [(20-i) for i in range(20)] |
| 618 | a = array.array('i', contents) |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 620 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 621 | writer = csv.writer(fileobj, dialect="excel") |
| 622 | writer.writerow(a) |
| 623 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 624 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 625 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 626 | |
| 627 | def test_double_write(self): |
| 628 | import array |
| 629 | contents = [(20-i)*0.1 for i in range(20)] |
| 630 | a = array.array('d', contents) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 631 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 632 | writer = csv.writer(fileobj, dialect="excel") |
| 633 | writer.writerow(a) |
| 634 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 635 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 636 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 637 | |
| 638 | def test_float_write(self): |
| 639 | import array |
| 640 | contents = [(20-i)*0.1 for i in range(20)] |
| 641 | a = array.array('f', contents) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 642 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 643 | writer = csv.writer(fileobj, dialect="excel") |
| 644 | writer.writerow(a) |
| 645 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 646 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 647 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 648 | |
| 649 | def test_char_write(self): |
| 650 | import array, string |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 651 | a = array.array('u', string.ascii_letters) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 652 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 653 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 654 | writer = csv.writer(fileobj, dialect="excel") |
| 655 | writer.writerow(a) |
| 656 | expected = ",".join(a)+"\r\n" |
| 657 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 658 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 659 | |
| 660 | class TestDialectValidity(unittest.TestCase): |
| 661 | def test_quoting(self): |
| 662 | class mydialect(csv.Dialect): |
| 663 | delimiter = ";" |
| 664 | escapechar = '\\' |
| 665 | doublequote = False |
| 666 | skipinitialspace = True |
| 667 | lineterminator = '\r\n' |
| 668 | quoting = csv.QUOTE_NONE |
| 669 | d = mydialect() |
| 670 | |
| 671 | mydialect.quoting = None |
| 672 | self.assertRaises(csv.Error, mydialect) |
| 673 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 674 | mydialect.doublequote = True |
| 675 | mydialect.quoting = csv.QUOTE_ALL |
| 676 | mydialect.quotechar = '"' |
| 677 | d = mydialect() |
| 678 | |
| 679 | mydialect.quotechar = "''" |
| 680 | self.assertRaises(csv.Error, mydialect) |
| 681 | |
| 682 | mydialect.quotechar = 4 |
| 683 | self.assertRaises(csv.Error, mydialect) |
| 684 | |
| 685 | def test_delimiter(self): |
| 686 | class mydialect(csv.Dialect): |
| 687 | delimiter = ";" |
| 688 | escapechar = '\\' |
| 689 | doublequote = False |
| 690 | skipinitialspace = True |
| 691 | lineterminator = '\r\n' |
| 692 | quoting = csv.QUOTE_NONE |
| 693 | d = mydialect() |
| 694 | |
| 695 | mydialect.delimiter = ":::" |
| 696 | self.assertRaises(csv.Error, mydialect) |
| 697 | |
| 698 | mydialect.delimiter = 4 |
| 699 | self.assertRaises(csv.Error, mydialect) |
| 700 | |
| 701 | def test_lineterminator(self): |
| 702 | class mydialect(csv.Dialect): |
| 703 | delimiter = ";" |
| 704 | escapechar = '\\' |
| 705 | doublequote = False |
| 706 | skipinitialspace = True |
| 707 | lineterminator = '\r\n' |
| 708 | quoting = csv.QUOTE_NONE |
| 709 | d = mydialect() |
| 710 | |
| 711 | mydialect.lineterminator = ":::" |
| 712 | d = mydialect() |
| 713 | |
| 714 | mydialect.lineterminator = 4 |
| 715 | self.assertRaises(csv.Error, mydialect) |
| 716 | |
| 717 | |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 718 | class TestSniffer(unittest.TestCase): |
| 719 | sample1 = """\ |
| 720 | Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes |
| 721 | Shark City, Glendale Heights, IL, 12/28/02, Prezence |
| 722 | Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow |
| 723 | Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back |
| 724 | """ |
| 725 | sample2 = """\ |
| 726 | 'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes' |
| 727 | 'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence' |
| 728 | 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow' |
| 729 | 'Stonecutters Seafood and Chop House':'Lemont':'IL':'12/19/02':'Week Back' |
| 730 | """ |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 731 | header = '''\ |
| 732 | "venue","city","state","date","performers" |
| 733 | ''' |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 734 | sample3 = '''\ |
| 735 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 736 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 737 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 738 | ''' |
| 739 | |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 740 | sample4 = '''\ |
| 741 | 2147483648;43.0e12;17;abc;def |
| 742 | 147483648;43.0e2;17;abc;def |
| 743 | 47483648;43.0;170;abc;def |
| 744 | ''' |
| 745 | |
Skip Montanaro | 91bb70c | 2005-12-28 15:37:25 +0000 | [diff] [blame] | 746 | sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n" |
Skip Montanaro | 39b29be | 2005-12-30 05:09:48 +0000 | [diff] [blame] | 747 | sample6 = "a|b|c\r\nd|e|f\r\n" |
| 748 | sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n" |
Skip Montanaro | 91bb70c | 2005-12-28 15:37:25 +0000 | [diff] [blame] | 749 | |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 750 | def test_has_header(self): |
| 751 | sniffer = csv.Sniffer() |
| 752 | self.assertEqual(sniffer.has_header(self.sample1), False) |
| 753 | self.assertEqual(sniffer.has_header(self.header+self.sample1), True) |
| 754 | |
| 755 | def test_sniff(self): |
| 756 | sniffer = csv.Sniffer() |
| 757 | dialect = sniffer.sniff(self.sample1) |
| 758 | self.assertEqual(dialect.delimiter, ",") |
| 759 | self.assertEqual(dialect.quotechar, '"') |
| 760 | self.assertEqual(dialect.skipinitialspace, True) |
| 761 | |
| 762 | dialect = sniffer.sniff(self.sample2) |
| 763 | self.assertEqual(dialect.delimiter, ":") |
| 764 | self.assertEqual(dialect.quotechar, "'") |
| 765 | self.assertEqual(dialect.skipinitialspace, False) |
| 766 | |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 767 | def test_delimiters(self): |
| 768 | sniffer = csv.Sniffer() |
| 769 | dialect = sniffer.sniff(self.sample3) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 770 | # given that all three lines in sample3 are equal, |
| 771 | # I think that any character could have been 'guessed' as the |
| 772 | # delimiter, depending on dictionary order |
| 773 | self.assert_(dialect.delimiter in self.sample3) |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 774 | dialect = sniffer.sniff(self.sample3, delimiters="?,") |
| 775 | self.assertEqual(dialect.delimiter, "?") |
| 776 | dialect = sniffer.sniff(self.sample3, delimiters="/,") |
| 777 | self.assertEqual(dialect.delimiter, "/") |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 778 | dialect = sniffer.sniff(self.sample4) |
| 779 | self.assertEqual(dialect.delimiter, ";") |
Skip Montanaro | 91bb70c | 2005-12-28 15:37:25 +0000 | [diff] [blame] | 780 | dialect = sniffer.sniff(self.sample5) |
| 781 | self.assertEqual(dialect.delimiter, "\t") |
Skip Montanaro | 39b29be | 2005-12-30 05:09:48 +0000 | [diff] [blame] | 782 | dialect = sniffer.sniff(self.sample6) |
| 783 | self.assertEqual(dialect.delimiter, "|") |
| 784 | dialect = sniffer.sniff(self.sample7) |
| 785 | self.assertEqual(dialect.delimiter, "|") |
| 786 | self.assertEqual(dialect.quotechar, "'") |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 787 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 788 | if not hasattr(sys, "gettotalrefcount"): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 789 | if test_support.verbose: print("*** skipping leakage tests ***") |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 790 | else: |
| 791 | class NUL: |
| 792 | def write(s, *args): |
| 793 | pass |
| 794 | writelines = write |
| 795 | |
| 796 | class TestLeaks(unittest.TestCase): |
| 797 | def test_create_read(self): |
| 798 | delta = 0 |
| 799 | lastrc = sys.gettotalrefcount() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 800 | for i in range(20): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 801 | gc.collect() |
| 802 | self.assertEqual(gc.garbage, []) |
| 803 | rc = sys.gettotalrefcount() |
| 804 | csv.reader(["a,b,c\r\n"]) |
| 805 | csv.reader(["a,b,c\r\n"]) |
| 806 | csv.reader(["a,b,c\r\n"]) |
| 807 | delta = rc-lastrc |
| 808 | lastrc = rc |
| 809 | # if csv.reader() leaks, last delta should be 3 or more |
| 810 | self.assertEqual(delta < 3, True) |
| 811 | |
| 812 | def test_create_write(self): |
| 813 | delta = 0 |
| 814 | lastrc = sys.gettotalrefcount() |
| 815 | s = NUL() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 816 | for i in range(20): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 817 | gc.collect() |
| 818 | self.assertEqual(gc.garbage, []) |
| 819 | rc = sys.gettotalrefcount() |
| 820 | csv.writer(s) |
| 821 | csv.writer(s) |
| 822 | csv.writer(s) |
| 823 | delta = rc-lastrc |
| 824 | lastrc = rc |
| 825 | # if csv.writer() leaks, last delta should be 3 or more |
| 826 | self.assertEqual(delta < 3, True) |
| 827 | |
| 828 | def test_read(self): |
| 829 | delta = 0 |
| 830 | rows = ["a,b,c\r\n"]*5 |
| 831 | lastrc = sys.gettotalrefcount() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 832 | for i in range(20): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 833 | gc.collect() |
| 834 | self.assertEqual(gc.garbage, []) |
| 835 | rc = sys.gettotalrefcount() |
| 836 | rdr = csv.reader(rows) |
| 837 | for row in rdr: |
| 838 | pass |
| 839 | delta = rc-lastrc |
| 840 | lastrc = rc |
| 841 | # if reader leaks during read, delta should be 5 or more |
| 842 | self.assertEqual(delta < 5, True) |
| 843 | |
| 844 | def test_write(self): |
| 845 | delta = 0 |
| 846 | rows = [[1,2,3]]*5 |
| 847 | s = NUL() |
| 848 | lastrc = sys.gettotalrefcount() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 849 | for i in range(20): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 850 | gc.collect() |
| 851 | self.assertEqual(gc.garbage, []) |
| 852 | rc = sys.gettotalrefcount() |
| 853 | writer = csv.writer(s) |
| 854 | for row in rows: |
| 855 | writer.writerow(row) |
| 856 | delta = rc-lastrc |
| 857 | lastrc = rc |
| 858 | # if writer leaks during write, last delta should be 5 or more |
| 859 | self.assertEqual(delta < 5, True) |
| 860 | |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 861 | class TestUnicode(unittest.TestCase): |
| 862 | |
| 863 | names = ["Martin von Löwis", |
| 864 | "Marc André Lemburg", |
| 865 | "Guido van Rossum", |
| 866 | "François Pinard"] |
| 867 | |
| 868 | def test_unicode_read(self): |
| 869 | import io |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 870 | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 871 | fileobj.write(",".join(self.names) + "\r\n") |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 872 | fileobj.seek(0) |
| 873 | reader = csv.reader(fileobj) |
| 874 | self.assertEqual(list(reader), [self.names]) |
| 875 | |
| 876 | |
| 877 | def test_unicode_write(self): |
| 878 | import io |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 879 | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
| 880 | writer = csv.writer(fileobj) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 881 | writer.writerow(self.names) |
| 882 | expected = ",".join(self.names)+"\r\n" |
| 883 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame^] | 884 | self.assertEqual(fileobj.read(), expected) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 885 | |
| 886 | |
Skip Montanaro | 1a56665 | 2003-05-06 15:56:05 +0000 | [diff] [blame] | 887 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 888 | def test_main(): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 889 | mod = sys.modules[__name__] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 890 | test_support.run_unittest( |
| 891 | *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
| 892 | ) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 893 | |
| 894 | if __name__ == '__main__': |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 895 | test_main() |