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