blob: 75d1a91cefb85f06b35ab634c90f53cee66f98f9 [file] [log] [blame]
Guido van Rossumd77d6992007-07-16 23:10:57 +00001# -*- coding: utf-8 -*-
Skip Montanarob4a04172003-03-20 23:29:12 +00002# Copyright (C) 2001,2002 Python Software Foundation
3# csv package unit tests
4
Guido van Rossumf0c74162007-08-28 03:29:45 +00005import io
Skip Montanarob4a04172003-03-20 23:29:12 +00006import sys
Skip Montanaro58fc5d082004-06-05 17:03:20 +00007import os
Skip Montanarob4a04172003-03-20 23:29:12 +00008import unittest
R. David Murray8b7d4aa2009-04-04 01:38:38 +00009from io import StringIO
Guido van Rossum46264582007-08-06 19:32:18 +000010from tempfile import TemporaryFile
Skip Montanaro594adac2003-04-10 17:16:15 +000011import csv
Skip Montanarob4a04172003-03-20 23:29:12 +000012import gc
Benjamin Petersonee8712c2008-05-20 21:35:26 +000013from test import support
Skip Montanarob4a04172003-03-20 23:29:12 +000014
15class Test_Csv(unittest.TestCase):
16 """
Tim Peters0eadaac2003-04-24 16:02:54 +000017 Test the underlying C csv parser in ways that are not appropriate
Skip Montanarob4a04172003-03-20 23:29:12 +000018 from the high level interface. Further tests of this nature are done
19 in TestDialectRegistry.
20 """
Andrew McNamara1196cf12005-01-07 04:42:45 +000021 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 McNamara1196cf12005-01-07 04:42:45 +000028 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 Peters608c2ff2005-01-13 17:37:38 +000034 self.assertRaises(TypeError, ctor, arg,
Andrew McNamaraaf1e3122005-01-12 01:55:21 +000035 quoting=csv.QUOTE_ALL, quotechar='')
Tim Peters608c2ff2005-01-13 17:37:38 +000036 self.assertRaises(TypeError, ctor, arg,
Andrew McNamara5d45a8d2005-01-12 08:16:17 +000037 quoting=csv.QUOTE_ALL, quotechar=None)
Andrew McNamara1196cf12005-01-07 04:42:45 +000038
39 def test_reader_arg_valid(self):
40 self._test_arg_valid(csv.reader, [])
Skip Montanarob4a04172003-03-20 23:29:12 +000041
42 def test_writer_arg_valid(self):
Andrew McNamara1196cf12005-01-07 04:42:45 +000043 self._test_arg_valid(csv.writer, StringIO())
Skip Montanarob4a04172003-03-20 23:29:12 +000044
Andrew McNamara1196cf12005-01-07 04:42:45 +000045 def _test_default_attrs(self, ctor, *args):
46 obj = ctor(*args)
47 # Check defaults
Skip Montanarob4a04172003-03-20 23:29:12 +000048 self.assertEqual(obj.dialect.delimiter, ',')
Andrew McNamara1196cf12005-01-07 04:42:45 +000049 self.assertEqual(obj.dialect.doublequote, True)
Skip Montanarob4a04172003-03-20 23:29:12 +000050 self.assertEqual(obj.dialect.escapechar, None)
Andrew McNamara1196cf12005-01-07 04:42:45 +000051 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 Winter42dae6a2007-03-28 21:44:53 +000057 self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter')
58 self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':')
Barry Warsawb180c062005-04-20 19:41:36 +000059 self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
60 self.assertRaises(AttributeError, setattr, obj.dialect,
61 'quoting', None)
Skip Montanarob4a04172003-03-20 23:29:12 +000062
63 def test_reader_attrs(self):
Andrew McNamara1196cf12005-01-07 04:42:45 +000064 self._test_default_attrs(csv.reader, [])
Skip Montanarob4a04172003-03-20 23:29:12 +000065
66 def test_writer_attrs(self):
Andrew McNamara1196cf12005-01-07 04:42:45 +000067 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 Montanarob4a04172003-03-20 23:29:12 +0000119
120 def _write_test(self, fields, expect, **kwargs):
Guido van Rossumf0c74162007-08-28 03:29:45 +0000121 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000122 writer = csv.writer(fileobj, **kwargs)
123 writer.writerow(fields)
124 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000125 self.assertEqual(fileobj.read(),
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000126 expect + writer.dialect.lineterminator)
Skip Montanarob4a04172003-03-20 23:29:12 +0000127
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 Peters0eadaac2003-04-24 16:02:54 +0000132 self.assertRaises(csv.Error, self._write_test,
Skip Montanarob4a04172003-03-20 23:29:12 +0000133 [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 McNamarac89f2842005-01-12 07:44:42 +0000154 self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
Tim Peters0eadaac2003-04-24 16:02:54 +0000155 self.assertRaises(csv.Error,
Skip Montanarob4a04172003-03-20 23:29:12 +0000156 self._write_test,
Andrew McNamarac89f2842005-01-12 07:44:42 +0000157 ['a',1,'p,q'], 'a,1,p,q',
Skip Montanarob4a04172003-03-20 23:29:12 +0000158 quoting = csv.QUOTE_NONE)
Andrew McNamarac89f2842005-01-12 07:44:42 +0000159 self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
Skip Montanarob4a04172003-03-20 23:29:12 +0000160 quoting = csv.QUOTE_MINIMAL)
Andrew McNamarac89f2842005-01-12 07:44:42 +0000161 self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
Skip Montanarob4a04172003-03-20 23:29:12 +0000162 quoting = csv.QUOTE_NONNUMERIC)
Andrew McNamarac89f2842005-01-12 07:44:42 +0000163 self._write_test(['a',1,'p,q'], '"a","1","p,q"',
Skip Montanarob4a04172003-03-20 23:29:12 +0000164 quoting = csv.QUOTE_ALL)
R. David Murraycde68cf2009-04-01 22:37:58 +0000165 self._write_test(['a\nb',1], '"a\nb","1"',
166 quoting = csv.QUOTE_ALL)
Skip Montanarob4a04172003-03-20 23:29:12 +0000167
168 def test_write_escape(self):
Andrew McNamarac89f2842005-01-12 07:44:42 +0000169 self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
Skip Montanarob4a04172003-03-20 23:29:12 +0000170 escapechar='\\')
Andrew McNamarac89f2842005-01-12 07:44:42 +0000171 self.assertRaises(csv.Error,
172 self._write_test,
173 ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
Tim Peters608c2ff2005-01-13 17:37:38 +0000174 escapechar=None, doublequote=False)
Andrew McNamarac89f2842005-01-12 07:44:42 +0000175 self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
176 escapechar='\\', doublequote = False)
Tim Peters608c2ff2005-01-13 17:37:38 +0000177 self._write_test(['"'], '""""',
Andrew McNamarac89f2842005-01-12 07:44:42 +0000178 escapechar='\\', quoting = csv.QUOTE_MINIMAL)
Tim Peters608c2ff2005-01-13 17:37:38 +0000179 self._write_test(['"'], '\\"',
Andrew McNamarac89f2842005-01-12 07:44:42 +0000180 escapechar='\\', quoting = csv.QUOTE_MINIMAL,
181 doublequote = False)
Tim Peters608c2ff2005-01-13 17:37:38 +0000182 self._write_test(['"'], '\\"',
Andrew McNamarac89f2842005-01-12 07:44:42 +0000183 escapechar='\\', quoting = csv.QUOTE_NONE)
184 self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
Skip Montanarob4a04172003-03-20 23:29:12 +0000185 escapechar='\\', quoting = csv.QUOTE_NONE)
186
187 def test_writerows(self):
188 class BrokenFile:
189 def write(self, buf):
190 raise IOError
191 writer = csv.writer(BrokenFile())
192 self.assertRaises(IOError, writer.writerows, [['a']])
Guido van Rossum46264582007-08-06 19:32:18 +0000193
Guido van Rossumf0c74162007-08-28 03:29:45 +0000194 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000195 writer = csv.writer(fileobj)
196 self.assertRaises(TypeError, writer.writerows, None)
197 writer.writerows([['a','b'],['c','d']])
198 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000199 self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n")
Tim Peters27f88362004-07-08 04:22:35 +0000200
Skip Montanarob4a04172003-03-20 23:29:12 +0000201 def _read_test(self, input, expect, **kwargs):
202 reader = csv.reader(input, **kwargs)
203 result = list(reader)
204 self.assertEqual(result, expect)
205
206 def test_read_oddinputs(self):
207 self._read_test([], [])
208 self._read_test([''], [[]])
209 self.assertRaises(csv.Error, self._read_test,
210 ['"ab"c'], None, strict = 1)
211 # cannot handle null bytes for the moment
212 self.assertRaises(csv.Error, self._read_test,
213 ['ab\0c'], None, strict = 1)
214 self._read_test(['"ab"c'], [['abc']], doublequote = 0)
215
Amaury Forgeot d'Arc10c476d2007-11-19 21:20:21 +0000216 self.assertRaises(csv.Error, self._read_test,
217 [b'ab\0c'], None)
218
219
Skip Montanarob4a04172003-03-20 23:29:12 +0000220 def test_read_eol(self):
221 self._read_test(['a,b'], [['a','b']])
222 self._read_test(['a,b\n'], [['a','b']])
223 self._read_test(['a,b\r\n'], [['a','b']])
224 self._read_test(['a,b\r'], [['a','b']])
225 self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], [])
226 self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], [])
227 self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], [])
228
229 def test_read_escape(self):
Andrew McNamara36a76912005-01-10 01:04:40 +0000230 self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\')
Skip Montanarob4a04172003-03-20 23:29:12 +0000231 self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\')
232 self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\')
Andrew McNamara36a76912005-01-10 01:04:40 +0000233 self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\')
Skip Montanarob4a04172003-03-20 23:29:12 +0000234 self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\')
235 self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\')
236
Andrew McNamara1196cf12005-01-07 04:42:45 +0000237 def test_read_quoting(self):
238 self._read_test(['1,",3,",5'], [['1', ',3,', '5']])
239 self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
240 quotechar=None, escapechar='\\')
241 self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
242 quoting=csv.QUOTE_NONE, escapechar='\\')
Andrew McNamara0f0599d2005-01-12 09:45:18 +0000243 # will this fail where locale uses comma for decimals?
Andrew McNamara7f2053e2005-01-12 11:17:16 +0000244 self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
Andrew McNamara0f0599d2005-01-12 09:45:18 +0000245 quoting=csv.QUOTE_NONNUMERIC)
R. David Murraycde68cf2009-04-01 22:37:58 +0000246 self._read_test(['"a\nb", 7'], [['a\nb', ' 7']])
Tim Peters608c2ff2005-01-13 17:37:38 +0000247 self.assertRaises(ValueError, self._read_test,
Andrew McNamara0f0599d2005-01-12 09:45:18 +0000248 ['abc,3'], [[]],
249 quoting=csv.QUOTE_NONNUMERIC)
Andrew McNamara1196cf12005-01-07 04:42:45 +0000250
Skip Montanarob4a04172003-03-20 23:29:12 +0000251 def test_read_bigfield(self):
Andrew McNamarae4d05c42005-01-11 07:32:02 +0000252 # This exercises the buffer realloc functionality and field size
253 # limits.
Andrew McNamara31d88962005-01-12 03:45:10 +0000254 limit = csv.field_size_limit()
Andrew McNamaraaf1e3122005-01-12 01:55:21 +0000255 try:
256 size = 50000
257 bigstring = 'X' * size
258 bigline = '%s,%s' % (bigstring, bigstring)
259 self._read_test([bigline], [[bigstring, bigstring]])
Andrew McNamara31d88962005-01-12 03:45:10 +0000260 csv.field_size_limit(size)
Andrew McNamaraaf1e3122005-01-12 01:55:21 +0000261 self._read_test([bigline], [[bigstring, bigstring]])
Andrew McNamara31d88962005-01-12 03:45:10 +0000262 self.assertEqual(csv.field_size_limit(), size)
263 csv.field_size_limit(size-1)
Andrew McNamaraaf1e3122005-01-12 01:55:21 +0000264 self.assertRaises(csv.Error, self._read_test, [bigline], [])
Andrew McNamara31d88962005-01-12 03:45:10 +0000265 self.assertRaises(TypeError, csv.field_size_limit, None)
266 self.assertRaises(TypeError, csv.field_size_limit, 1, None)
Andrew McNamaraaf1e3122005-01-12 01:55:21 +0000267 finally:
Andrew McNamara31d88962005-01-12 03:45:10 +0000268 csv.field_size_limit(limit)
Skip Montanarob4a04172003-03-20 23:29:12 +0000269
Andrew McNamara7f2053e2005-01-12 11:17:16 +0000270 def test_read_linenum(self):
271 r = csv.reader(['line,1', 'line,2', 'line,3'])
272 self.assertEqual(r.line_num, 0)
Georg Brandla18af4e2007-04-21 15:47:16 +0000273 next(r)
Andrew McNamara7f2053e2005-01-12 11:17:16 +0000274 self.assertEqual(r.line_num, 1)
Georg Brandla18af4e2007-04-21 15:47:16 +0000275 next(r)
Andrew McNamara7f2053e2005-01-12 11:17:16 +0000276 self.assertEqual(r.line_num, 2)
Georg Brandla18af4e2007-04-21 15:47:16 +0000277 next(r)
Andrew McNamara7f2053e2005-01-12 11:17:16 +0000278 self.assertEqual(r.line_num, 3)
Georg Brandla18af4e2007-04-21 15:47:16 +0000279 self.assertRaises(StopIteration, next, r)
Andrew McNamara7f2053e2005-01-12 11:17:16 +0000280 self.assertEqual(r.line_num, 3)
281
R. David Murraycde68cf2009-04-01 22:37:58 +0000282 def test_roundtrip_quoteed_newlines(self):
283 with TemporaryFile("w+", newline='') as fileobj:
284 writer = csv.writer(fileobj)
285 self.assertRaises(TypeError, writer.writerows, None)
286 rows = [['a\nb','b'],['c','x\r\nd']]
287 writer.writerows(rows)
288 fileobj.seek(0)
289 for i, row in enumerate(csv.reader(fileobj)):
290 self.assertEqual(row, rows[i])
291
Skip Montanarob4a04172003-03-20 23:29:12 +0000292class TestDialectRegistry(unittest.TestCase):
293 def test_registry_badargs(self):
294 self.assertRaises(TypeError, csv.list_dialects, None)
295 self.assertRaises(TypeError, csv.get_dialect)
296 self.assertRaises(csv.Error, csv.get_dialect, None)
297 self.assertRaises(csv.Error, csv.get_dialect, "nonesuch")
298 self.assertRaises(TypeError, csv.unregister_dialect)
299 self.assertRaises(csv.Error, csv.unregister_dialect, None)
300 self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch")
301 self.assertRaises(TypeError, csv.register_dialect, None)
302 self.assertRaises(TypeError, csv.register_dialect, None, None)
Andrew McNamaraaf1e3122005-01-12 01:55:21 +0000303 self.assertRaises(TypeError, csv.register_dialect, "nonesuch", 0, 0)
304 self.assertRaises(TypeError, csv.register_dialect, "nonesuch",
305 badargument=None)
306 self.assertRaises(TypeError, csv.register_dialect, "nonesuch",
307 quoting=None)
308 self.assertRaises(TypeError, csv.register_dialect, [])
Skip Montanarob4a04172003-03-20 23:29:12 +0000309
310 def test_registry(self):
311 class myexceltsv(csv.excel):
312 delimiter = "\t"
313 name = "myexceltsv"
314 expected_dialects = csv.list_dialects() + [name]
315 expected_dialects.sort()
316 csv.register_dialect(name, myexceltsv)
317 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000318 self.assertTrue(csv.get_dialect(name).delimiter, '\t')
Skip Montanarob4a04172003-03-20 23:29:12 +0000319 got_dialects = csv.list_dialects()
320 got_dialects.sort()
321 self.assertEqual(expected_dialects, got_dialects)
322 finally:
323 csv.unregister_dialect(name)
324
Andrew McNamara86625972005-01-11 01:28:33 +0000325 def test_register_kwargs(self):
326 name = 'fedcba'
327 csv.register_dialect(name, delimiter=';')
328 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000329 self.assertTrue(csv.get_dialect(name).delimiter, '\t')
330 self.assertTrue(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z'])
Andrew McNamara86625972005-01-11 01:28:33 +0000331 finally:
332 csv.unregister_dialect(name)
333
Skip Montanarob4a04172003-03-20 23:29:12 +0000334 def test_incomplete_dialect(self):
335 class myexceltsv(csv.Dialect):
336 delimiter = "\t"
337 self.assertRaises(csv.Error, myexceltsv)
338
339 def test_space_dialect(self):
340 class space(csv.excel):
341 delimiter = " "
342 quoting = csv.QUOTE_NONE
343 escapechar = "\\"
344
Guido van Rossum46264582007-08-06 19:32:18 +0000345 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000346 fileobj.write("abc def\nc1ccccc1 benzene\n")
347 fileobj.seek(0)
Georg Brandla18af4e2007-04-21 15:47:16 +0000348 reader = csv.reader(fileobj, dialect=space())
349 self.assertEqual(next(reader), ["abc", "def"])
350 self.assertEqual(next(reader), ["c1ccccc1", "benzene"])
Guido van Rossum46264582007-08-06 19:32:18 +0000351
352 def compare_dialect_123(self, expected, *writeargs, **kwwriteargs):
Guido van Rossumf0c74162007-08-28 03:29:45 +0000353
354 with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj:
355
Guido van Rossum46264582007-08-06 19:32:18 +0000356 writer = csv.writer(fileobj, *writeargs, **kwwriteargs)
357 writer.writerow([1,2,3])
358 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000359 self.assertEqual(fileobj.read(), expected)
Tim Peters27f88362004-07-08 04:22:35 +0000360
Skip Montanarob4a04172003-03-20 23:29:12 +0000361 def test_dialect_apply(self):
362 class testA(csv.excel):
363 delimiter = "\t"
364 class testB(csv.excel):
365 delimiter = ":"
366 class testC(csv.excel):
367 delimiter = "|"
Guido van Rossum46264582007-08-06 19:32:18 +0000368 class testUni(csv.excel):
369 delimiter = "\u039B"
Skip Montanarob4a04172003-03-20 23:29:12 +0000370
371 csv.register_dialect('testC', testC)
372 try:
Guido van Rossum46264582007-08-06 19:32:18 +0000373 self.compare_dialect_123("1,2,3\r\n")
374 self.compare_dialect_123("1\t2\t3\r\n", testA)
375 self.compare_dialect_123("1:2:3\r\n", dialect=testB())
376 self.compare_dialect_123("1|2|3\r\n", dialect='testC')
377 self.compare_dialect_123("1;2;3\r\n", dialect=testA,
378 delimiter=';')
379 self.compare_dialect_123("1\u039B2\u039B3\r\n",
380 dialect=testUni)
Skip Montanarob4a04172003-03-20 23:29:12 +0000381
Skip Montanarob4a04172003-03-20 23:29:12 +0000382 finally:
383 csv.unregister_dialect('testC')
384
385 def test_bad_dialect(self):
386 # Unknown parameter
Andrew McNamara1196cf12005-01-07 04:42:45 +0000387 self.assertRaises(TypeError, csv.reader, [], bad_attr = 0)
Skip Montanarob4a04172003-03-20 23:29:12 +0000388 # Bad values
389 self.assertRaises(TypeError, csv.reader, [], delimiter = None)
390 self.assertRaises(TypeError, csv.reader, [], quoting = -1)
391 self.assertRaises(TypeError, csv.reader, [], quoting = 100)
392
393class TestCsvBase(unittest.TestCase):
394 def readerAssertEqual(self, input, expected_result):
Amaury Forgeot d'Arc10c476d2007-11-19 21:20:21 +0000395 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000396 fileobj.write(input)
397 fileobj.seek(0)
398 reader = csv.reader(fileobj, dialect = self.dialect)
399 fields = list(reader)
400 self.assertEqual(fields, expected_result)
Skip Montanarob4a04172003-03-20 23:29:12 +0000401
402 def writerAssertEqual(self, input, expected_result):
Guido van Rossumf0c74162007-08-28 03:29:45 +0000403 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000404 writer = csv.writer(fileobj, dialect = self.dialect)
405 writer.writerows(input)
406 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000407 self.assertEqual(fileobj.read(), expected_result)
Skip Montanarob4a04172003-03-20 23:29:12 +0000408
409class TestDialectExcel(TestCsvBase):
410 dialect = 'excel'
411
412 def test_single(self):
413 self.readerAssertEqual('abc', [['abc']])
414
415 def test_simple(self):
416 self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']])
417
418 def test_blankline(self):
419 self.readerAssertEqual('', [])
420
421 def test_empty_fields(self):
422 self.readerAssertEqual(',', [['', '']])
423
424 def test_singlequoted(self):
425 self.readerAssertEqual('""', [['']])
426
427 def test_singlequoted_left_empty(self):
428 self.readerAssertEqual('"",', [['','']])
429
430 def test_singlequoted_right_empty(self):
431 self.readerAssertEqual(',""', [['','']])
432
433 def test_single_quoted_quote(self):
434 self.readerAssertEqual('""""', [['"']])
435
436 def test_quoted_quotes(self):
437 self.readerAssertEqual('""""""', [['""']])
438
439 def test_inline_quote(self):
440 self.readerAssertEqual('a""b', [['a""b']])
441
442 def test_inline_quotes(self):
443 self.readerAssertEqual('a"b"c', [['a"b"c']])
444
445 def test_quotes_and_more(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000446 # Excel would never write a field containing '"a"b', but when
447 # reading one, it will return 'ab'.
Skip Montanarob4a04172003-03-20 23:29:12 +0000448 self.readerAssertEqual('"a"b', [['ab']])
449
450 def test_lone_quote(self):
451 self.readerAssertEqual('a"b', [['a"b']])
452
453 def test_quote_and_quote(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000454 # Excel would never write a field containing '"a" "b"', but when
455 # reading one, it will return 'a "b"'.
Skip Montanarob4a04172003-03-20 23:29:12 +0000456 self.readerAssertEqual('"a" "b"', [['a "b"']])
457
458 def test_space_and_quote(self):
459 self.readerAssertEqual(' "a"', [[' "a"']])
460
461 def test_quoted(self):
Tim Peters0eadaac2003-04-24 16:02:54 +0000462 self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6',
463 [['1', '2', '3',
464 'I think, therefore I am',
Skip Montanarob4a04172003-03-20 23:29:12 +0000465 '5', '6']])
466
467 def test_quoted_quote(self):
468 self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"',
Tim Peters0eadaac2003-04-24 16:02:54 +0000469 [['1', '2', '3',
470 '"I see," said the blind man',
Skip Montanarob4a04172003-03-20 23:29:12 +0000471 'as he picked up his hammer and saw']])
472
473 def test_quoted_nl(self):
474 input = '''\
4751,2,3,"""I see,""
476said the blind man","as he picked up his
477hammer and saw"
4789,8,7,6'''
479 self.readerAssertEqual(input,
Tim Peters0eadaac2003-04-24 16:02:54 +0000480 [['1', '2', '3',
481 '"I see,"\nsaid the blind man',
Skip Montanarob4a04172003-03-20 23:29:12 +0000482 'as he picked up his\nhammer and saw'],
483 ['9','8','7','6']])
484
485 def test_dubious_quote(self):
486 self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']])
487
488 def test_null(self):
489 self.writerAssertEqual([], '')
490
491 def test_single(self):
492 self.writerAssertEqual([['abc']], 'abc\r\n')
493
494 def test_simple(self):
495 self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n')
496
497 def test_quotes(self):
498 self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n')
499
500 def test_quote_fieldsep(self):
501 self.writerAssertEqual([['abc,def']], '"abc,def"\r\n')
502
503 def test_newlines(self):
504 self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n')
505
506class EscapedExcel(csv.excel):
507 quoting = csv.QUOTE_NONE
508 escapechar = '\\'
509
510class TestEscapedExcel(TestCsvBase):
511 dialect = EscapedExcel()
512
513 def test_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
519class QuotedEscapedExcel(csv.excel):
520 quoting = csv.QUOTE_NONNUMERIC
521 escapechar = '\\'
522
523class TestQuotedEscapedExcel(TestCsvBase):
524 dialect = QuotedEscapedExcel()
525
526 def test_write_escape_fieldsep(self):
527 self.writerAssertEqual([['abc,def']], '"abc,def"\r\n')
528
529 def test_read_escape_fieldsep(self):
530 self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']])
531
Skip Montanarob4a04172003-03-20 23:29:12 +0000532class TestDictFields(unittest.TestCase):
533 ### "long" means the row is longer than the number of fieldnames
534 ### "short" means there are fewer elements in the row than fieldnames
535 def test_write_simple_dict(self):
Guido van Rossumf0c74162007-08-28 03:29:45 +0000536 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000537 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
538 writer.writerow({"f1": 10, "f3": "abc"})
539 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000540 self.assertEqual(fileobj.read(), "10,,abc\r\n")
Skip Montanarob4a04172003-03-20 23:29:12 +0000541
542 def test_write_no_fields(self):
543 fileobj = StringIO()
544 self.assertRaises(TypeError, csv.DictWriter, fileobj)
545
546 def test_read_dict_fields(self):
Guido van Rossum46264582007-08-06 19:32:18 +0000547 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000548 fileobj.write("1,2,abc\r\n")
549 fileobj.seek(0)
550 reader = csv.DictReader(fileobj,
551 fieldnames=["f1", "f2", "f3"])
Georg Brandla18af4e2007-04-21 15:47:16 +0000552 self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
Skip Montanarob4a04172003-03-20 23:29:12 +0000553
Skip Montanarodffeed32003-10-03 14:03:01 +0000554 def test_read_dict_no_fieldnames(self):
Guido van Rossum46264582007-08-06 19:32:18 +0000555 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000556 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
557 fileobj.seek(0)
558 reader = csv.DictReader(fileobj)
Georg Brandla18af4e2007-04-21 15:47:16 +0000559 self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
Skip Montanaroaf8fcfa2008-08-09 19:44:22 +0000560 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])
561
562 # Two test cases to make sure existing ways of implicitly setting
563 # fieldnames continue to work. Both arise from discussion in issue3436.
564 def test_read_dict_fieldnames_from_file(self):
565 with TemporaryFile("w+") as fileobj:
566 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
567 fileobj.seek(0)
568 reader = csv.DictReader(fileobj,
569 fieldnames=next(csv.reader(fileobj)))
570 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])
571 self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
572
573 def test_read_dict_fieldnames_chain(self):
574 import itertools
575 with TemporaryFile("w+") as fileobj:
576 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
577 fileobj.seek(0)
578 reader = csv.DictReader(fileobj)
579 first = next(reader)
580 for row in itertools.chain([first], reader):
581 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])
582 self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'})
Skip Montanarodffeed32003-10-03 14:03:01 +0000583
Skip Montanarob4a04172003-03-20 23:29:12 +0000584 def test_read_long(self):
Guido van Rossum46264582007-08-06 19:32:18 +0000585 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000586 fileobj.write("1,2,abc,4,5,6\r\n")
587 fileobj.seek(0)
588 reader = csv.DictReader(fileobj,
589 fieldnames=["f1", "f2"])
Georg Brandla18af4e2007-04-21 15:47:16 +0000590 self.assertEqual(next(reader), {"f1": '1', "f2": '2',
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000591 None: ["abc", "4", "5", "6"]})
Skip Montanarob4a04172003-03-20 23:29:12 +0000592
593 def test_read_long_with_rest(self):
Guido van Rossum46264582007-08-06 19:32:18 +0000594 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000595 fileobj.write("1,2,abc,4,5,6\r\n")
596 fileobj.seek(0)
597 reader = csv.DictReader(fileobj,
598 fieldnames=["f1", "f2"], restkey="_rest")
Georg Brandla18af4e2007-04-21 15:47:16 +0000599 self.assertEqual(next(reader), {"f1": '1', "f2": '2',
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000600 "_rest": ["abc", "4", "5", "6"]})
Skip Montanarob4a04172003-03-20 23:29:12 +0000601
Skip Montanarodffeed32003-10-03 14:03:01 +0000602 def test_read_long_with_rest_no_fieldnames(self):
Guido van Rossum46264582007-08-06 19:32:18 +0000603 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000604 fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n")
605 fileobj.seek(0)
606 reader = csv.DictReader(fileobj, restkey="_rest")
Skip Montanaroaf8fcfa2008-08-09 19:44:22 +0000607 self.assertEqual(reader.fieldnames, ["f1", "f2"])
Georg Brandla18af4e2007-04-21 15:47:16 +0000608 self.assertEqual(next(reader), {"f1": '1', "f2": '2',
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000609 "_rest": ["abc", "4", "5", "6"]})
Skip Montanarodffeed32003-10-03 14:03:01 +0000610
Skip Montanarob4a04172003-03-20 23:29:12 +0000611 def test_read_short(self):
Guido van Rossum46264582007-08-06 19:32:18 +0000612 with TemporaryFile("w+") as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000613 fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n")
614 fileobj.seek(0)
615 reader = csv.DictReader(fileobj,
616 fieldnames="1 2 3 4 5 6".split(),
617 restval="DEFAULT")
Georg Brandla18af4e2007-04-21 15:47:16 +0000618 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000619 "4": '4', "5": '5', "6": '6'})
Georg Brandla18af4e2007-04-21 15:47:16 +0000620 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000621 "4": 'DEFAULT', "5": 'DEFAULT',
622 "6": 'DEFAULT'})
Skip Montanarob4a04172003-03-20 23:29:12 +0000623
Skip Montanaro1546bc42003-06-12 02:40:22 +0000624 def test_read_multi(self):
625 sample = [
626 '2147483648,43.0e12,17,abc,def\r\n',
627 '147483648,43.0e2,17,abc,def\r\n',
628 '47483648,43.0,170,abc,def\r\n'
629 ]
630
631 reader = csv.DictReader(sample,
632 fieldnames="i1 float i2 s1 s2".split())
Georg Brandla18af4e2007-04-21 15:47:16 +0000633 self.assertEqual(next(reader), {"i1": '2147483648',
Skip Montanaro1546bc42003-06-12 02:40:22 +0000634 "float": '43.0e12',
635 "i2": '17',
636 "s1": 'abc',
637 "s2": 'def'})
638
Skip Montanarob4a04172003-03-20 23:29:12 +0000639 def test_read_with_blanks(self):
640 reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n",
641 "1,2,abc,4,5,6\r\n"],
642 fieldnames="1 2 3 4 5 6".split())
Georg Brandla18af4e2007-04-21 15:47:16 +0000643 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
Skip Montanarob4a04172003-03-20 23:29:12 +0000644 "4": '4', "5": '5', "6": '6'})
Georg Brandla18af4e2007-04-21 15:47:16 +0000645 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
Skip Montanarob4a04172003-03-20 23:29:12 +0000646 "4": '4', "5": '5', "6": '6'})
647
Skip Montanaro3f7a9482003-09-06 19:52:12 +0000648 def test_read_semi_sep(self):
649 reader = csv.DictReader(["1;2;abc;4;5;6\r\n"],
650 fieldnames="1 2 3 4 5 6".split(),
651 delimiter=';')
Georg Brandla18af4e2007-04-21 15:47:16 +0000652 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc',
Skip Montanaro3f7a9482003-09-06 19:52:12 +0000653 "4": '4', "5": '5', "6": '6'})
654
Skip Montanarob4a04172003-03-20 23:29:12 +0000655class TestArrayWrites(unittest.TestCase):
656 def test_int_write(self):
657 import array
658 contents = [(20-i) for i in range(20)]
659 a = array.array('i', contents)
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000660
Guido van Rossumf0c74162007-08-28 03:29:45 +0000661 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000662 writer = csv.writer(fileobj, dialect="excel")
663 writer.writerow(a)
664 expected = ",".join([str(i) for i in a])+"\r\n"
665 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000666 self.assertEqual(fileobj.read(), expected)
Skip Montanarob4a04172003-03-20 23:29:12 +0000667
668 def test_double_write(self):
669 import array
670 contents = [(20-i)*0.1 for i in range(20)]
671 a = array.array('d', contents)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000672 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000673 writer = csv.writer(fileobj, dialect="excel")
674 writer.writerow(a)
675 expected = ",".join([str(i) for i in a])+"\r\n"
676 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000677 self.assertEqual(fileobj.read(), expected)
Skip Montanarob4a04172003-03-20 23:29:12 +0000678
679 def test_float_write(self):
680 import array
681 contents = [(20-i)*0.1 for i in range(20)]
682 a = array.array('f', contents)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000683 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000684 writer = csv.writer(fileobj, dialect="excel")
685 writer.writerow(a)
686 expected = ",".join([str(i) for i in a])+"\r\n"
687 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000688 self.assertEqual(fileobj.read(), expected)
Skip Montanarob4a04172003-03-20 23:29:12 +0000689
690 def test_char_write(self):
691 import array, string
Martin v. Löwis967f1e32007-08-14 09:23:10 +0000692 a = array.array('u', string.ascii_letters)
Guido van Rossum46264582007-08-06 19:32:18 +0000693
Guido van Rossumf0c74162007-08-28 03:29:45 +0000694 with TemporaryFile("w+", newline='') as fileobj:
Skip Montanaro58fc5d082004-06-05 17:03:20 +0000695 writer = csv.writer(fileobj, dialect="excel")
696 writer.writerow(a)
697 expected = ",".join(a)+"\r\n"
698 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000699 self.assertEqual(fileobj.read(), expected)
Skip Montanarob4a04172003-03-20 23:29:12 +0000700
701class TestDialectValidity(unittest.TestCase):
702 def test_quoting(self):
703 class mydialect(csv.Dialect):
704 delimiter = ";"
705 escapechar = '\\'
706 doublequote = False
707 skipinitialspace = True
708 lineterminator = '\r\n'
709 quoting = csv.QUOTE_NONE
710 d = mydialect()
711
712 mydialect.quoting = None
713 self.assertRaises(csv.Error, mydialect)
714
Skip Montanarob4a04172003-03-20 23:29:12 +0000715 mydialect.doublequote = True
716 mydialect.quoting = csv.QUOTE_ALL
717 mydialect.quotechar = '"'
718 d = mydialect()
719
720 mydialect.quotechar = "''"
721 self.assertRaises(csv.Error, mydialect)
722
723 mydialect.quotechar = 4
724 self.assertRaises(csv.Error, mydialect)
725
726 def test_delimiter(self):
727 class mydialect(csv.Dialect):
728 delimiter = ";"
729 escapechar = '\\'
730 doublequote = False
731 skipinitialspace = True
732 lineterminator = '\r\n'
733 quoting = csv.QUOTE_NONE
734 d = mydialect()
735
736 mydialect.delimiter = ":::"
737 self.assertRaises(csv.Error, mydialect)
738
739 mydialect.delimiter = 4
740 self.assertRaises(csv.Error, mydialect)
741
742 def test_lineterminator(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.lineterminator = ":::"
753 d = mydialect()
754
755 mydialect.lineterminator = 4
756 self.assertRaises(csv.Error, mydialect)
757
758
Skip Montanaro48816c62003-04-25 14:43:14 +0000759class TestSniffer(unittest.TestCase):
760 sample1 = """\
761Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes
762Shark City, Glendale Heights, IL, 12/28/02, Prezence
763Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow
764Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
765"""
766 sample2 = """\
767'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes'
768'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence'
769'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow'
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +0000770'Stonecutters ''Seafood'' and Chop House':'Lemont':'IL':'12/19/02':'Week Back'
Skip Montanaro48816c62003-04-25 14:43:14 +0000771"""
Skip Montanaro48816c62003-04-25 14:43:14 +0000772 header = '''\
773"venue","city","state","date","performers"
774'''
Skip Montanaro77892372003-05-19 15:33:36 +0000775 sample3 = '''\
77605/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03
77705/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03
77805/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03
779'''
780
Skip Montanaro1546bc42003-06-12 02:40:22 +0000781 sample4 = '''\
7822147483648;43.0e12;17;abc;def
783147483648;43.0e2;17;abc;def
78447483648;43.0;170;abc;def
785'''
786
Skip Montanaro91bb70c2005-12-28 15:37:25 +0000787 sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n"
Skip Montanaro39b29be2005-12-30 05:09:48 +0000788 sample6 = "a|b|c\r\nd|e|f\r\n"
789 sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n"
Skip Montanaro91bb70c2005-12-28 15:37:25 +0000790
Skip Montanaro48816c62003-04-25 14:43:14 +0000791 def test_has_header(self):
792 sniffer = csv.Sniffer()
793 self.assertEqual(sniffer.has_header(self.sample1), False)
794 self.assertEqual(sniffer.has_header(self.header+self.sample1), True)
795
796 def test_sniff(self):
797 sniffer = csv.Sniffer()
798 dialect = sniffer.sniff(self.sample1)
799 self.assertEqual(dialect.delimiter, ",")
800 self.assertEqual(dialect.quotechar, '"')
801 self.assertEqual(dialect.skipinitialspace, True)
802
803 dialect = sniffer.sniff(self.sample2)
804 self.assertEqual(dialect.delimiter, ":")
805 self.assertEqual(dialect.quotechar, "'")
806 self.assertEqual(dialect.skipinitialspace, False)
807
Skip Montanaro77892372003-05-19 15:33:36 +0000808 def test_delimiters(self):
809 sniffer = csv.Sniffer()
810 dialect = sniffer.sniff(self.sample3)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000811 # given that all three lines in sample3 are equal,
812 # I think that any character could have been 'guessed' as the
813 # delimiter, depending on dictionary order
Georg Brandlab91fde2009-08-13 08:51:18 +0000814 self.assertTrue(dialect.delimiter in self.sample3)
Skip Montanaro77892372003-05-19 15:33:36 +0000815 dialect = sniffer.sniff(self.sample3, delimiters="?,")
816 self.assertEqual(dialect.delimiter, "?")
817 dialect = sniffer.sniff(self.sample3, delimiters="/,")
818 self.assertEqual(dialect.delimiter, "/")
Skip Montanaro1546bc42003-06-12 02:40:22 +0000819 dialect = sniffer.sniff(self.sample4)
820 self.assertEqual(dialect.delimiter, ";")
Skip Montanaro91bb70c2005-12-28 15:37:25 +0000821 dialect = sniffer.sniff(self.sample5)
822 self.assertEqual(dialect.delimiter, "\t")
Skip Montanaro39b29be2005-12-30 05:09:48 +0000823 dialect = sniffer.sniff(self.sample6)
824 self.assertEqual(dialect.delimiter, "|")
825 dialect = sniffer.sniff(self.sample7)
826 self.assertEqual(dialect.delimiter, "|")
827 self.assertEqual(dialect.quotechar, "'")
Skip Montanaro77892372003-05-19 15:33:36 +0000828
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +0000829 def test_doublequote(self):
830 sniffer = csv.Sniffer()
831 dialect = sniffer.sniff(self.header)
832 self.assertFalse(dialect.doublequote)
833 dialect = sniffer.sniff(self.sample2)
834 self.assertTrue(dialect.doublequote)
835
Skip Montanarob4a04172003-03-20 23:29:12 +0000836if not hasattr(sys, "gettotalrefcount"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000837 if support.verbose: print("*** skipping leakage tests ***")
Skip Montanarob4a04172003-03-20 23:29:12 +0000838else:
839 class NUL:
840 def write(s, *args):
841 pass
842 writelines = write
843
844 class TestLeaks(unittest.TestCase):
845 def test_create_read(self):
846 delta = 0
847 lastrc = sys.gettotalrefcount()
Guido van Rossum805365e2007-05-07 22:24:25 +0000848 for i in range(20):
Skip Montanarob4a04172003-03-20 23:29:12 +0000849 gc.collect()
850 self.assertEqual(gc.garbage, [])
851 rc = sys.gettotalrefcount()
852 csv.reader(["a,b,c\r\n"])
853 csv.reader(["a,b,c\r\n"])
854 csv.reader(["a,b,c\r\n"])
855 delta = rc-lastrc
856 lastrc = rc
857 # if csv.reader() leaks, last delta should be 3 or more
858 self.assertEqual(delta < 3, True)
859
860 def test_create_write(self):
861 delta = 0
862 lastrc = sys.gettotalrefcount()
863 s = NUL()
Guido van Rossum805365e2007-05-07 22:24:25 +0000864 for i in range(20):
Skip Montanarob4a04172003-03-20 23:29:12 +0000865 gc.collect()
866 self.assertEqual(gc.garbage, [])
867 rc = sys.gettotalrefcount()
868 csv.writer(s)
869 csv.writer(s)
870 csv.writer(s)
871 delta = rc-lastrc
872 lastrc = rc
873 # if csv.writer() leaks, last delta should be 3 or more
874 self.assertEqual(delta < 3, True)
875
876 def test_read(self):
877 delta = 0
878 rows = ["a,b,c\r\n"]*5
879 lastrc = sys.gettotalrefcount()
Guido van Rossum805365e2007-05-07 22:24:25 +0000880 for i in range(20):
Skip Montanarob4a04172003-03-20 23:29:12 +0000881 gc.collect()
882 self.assertEqual(gc.garbage, [])
883 rc = sys.gettotalrefcount()
884 rdr = csv.reader(rows)
885 for row in rdr:
886 pass
887 delta = rc-lastrc
888 lastrc = rc
889 # if reader leaks during read, delta should be 5 or more
890 self.assertEqual(delta < 5, True)
891
892 def test_write(self):
893 delta = 0
894 rows = [[1,2,3]]*5
895 s = NUL()
896 lastrc = sys.gettotalrefcount()
Guido van Rossum805365e2007-05-07 22:24:25 +0000897 for i in range(20):
Skip Montanarob4a04172003-03-20 23:29:12 +0000898 gc.collect()
899 self.assertEqual(gc.garbage, [])
900 rc = sys.gettotalrefcount()
901 writer = csv.writer(s)
902 for row in rows:
903 writer.writerow(row)
904 delta = rc-lastrc
905 lastrc = rc
906 # if writer leaks during write, last delta should be 5 or more
907 self.assertEqual(delta < 5, True)
908
Guido van Rossum46264582007-08-06 19:32:18 +0000909class TestUnicode(unittest.TestCase):
910
911 names = ["Martin von Löwis",
912 "Marc André Lemburg",
913 "Guido van Rossum",
914 "François Pinard"]
915
916 def test_unicode_read(self):
917 import io
Guido van Rossumf0c74162007-08-28 03:29:45 +0000918 with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj:
Guido van Rossum46264582007-08-06 19:32:18 +0000919 fileobj.write(",".join(self.names) + "\r\n")
Guido van Rossum46264582007-08-06 19:32:18 +0000920 fileobj.seek(0)
921 reader = csv.reader(fileobj)
922 self.assertEqual(list(reader), [self.names])
923
924
925 def test_unicode_write(self):
926 import io
Guido van Rossumf0c74162007-08-28 03:29:45 +0000927 with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj:
928 writer = csv.writer(fileobj)
Guido van Rossum46264582007-08-06 19:32:18 +0000929 writer.writerow(self.names)
930 expected = ",".join(self.names)+"\r\n"
931 fileobj.seek(0)
Guido van Rossumf0c74162007-08-28 03:29:45 +0000932 self.assertEqual(fileobj.read(), expected)
Guido van Rossum46264582007-08-06 19:32:18 +0000933
934
Skip Montanaro1a566652003-05-06 15:56:05 +0000935
Walter Dörwald21d3a322003-05-01 17:45:56 +0000936def test_main():
Skip Montanarob4a04172003-03-20 23:29:12 +0000937 mod = sys.modules[__name__]
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000938 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000939 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
940 )
Skip Montanarob4a04172003-03-20 23:29:12 +0000941
942if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000943 test_main()