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