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