Lines Matching full:csv
2 # csv package unit tests
9 import csv
25 Test the underlying C csv parser in ways that are not appropriate
35 self.assertRaises(csv.Error, ctor, arg, 'foo')
43 quoting=csv.QUOTE_ALL, quotechar='')
45 quoting=csv.QUOTE_ALL, quotechar=None)
48 self._test_arg_valid(csv.reader, [])
49 self.assertRaises(OSError, csv.reader, BadIterable())
52 self._test_arg_valid(csv.writer, StringIO())
57 self.assertRaises(OSError, csv.writer, BadWriter())
67 self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
78 self._test_default_attrs(csv.reader, [])
81 self._test_default_attrs(csv.writer, StringIO())
87 quoting=csv.QUOTE_NONE, skipinitialspace=True,
95 self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE)
100 self._test_kw_attrs(csv.reader, [])
103 self._test_kw_attrs(csv.writer, StringIO())
113 quoting=csv.QUOTE_ALL
123 self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
128 self._test_dialect_attrs(csv.reader, [])
131 self._test_dialect_attrs(csv.writer, StringIO())
136 writer = csv.writer(fileobj, **kwargs)
144 writer = csv.writer(fileobj, **kwargs)
151 self._write_error_test(csv.Error, None)
154 self._write_error_test(csv.Error, [None], quoting = csv.QUOTE_NONE)
177 self._write_error_test(csv.Error, ['a',1,'p,q'],
178 quoting = csv.QUOTE_NONE)
180 quoting = csv.QUOTE_MINIMAL)
182 quoting = csv.QUOTE_NONNUMERIC)
184 quoting = csv.QUOTE_ALL)
186 quoting = csv.QUOTE_ALL)
191 self._write_error_test(csv.Error, ['a',1,'p,"q"'],
196 escapechar='\\', quoting = csv.QUOTE_MINIMAL)
198 escapechar='\\', quoting = csv.QUOTE_MINIMAL,
201 escapechar='\\', quoting = csv.QUOTE_NONE)
203 escapechar='\\', quoting = csv.QUOTE_NONE)
210 self._write_error_test(csv.Error, iter([None]), quoting=csv.QUOTE_NONE)
217 writer = csv.writer(BrokenFile())
221 writer = csv.writer(fileobj)
229 writer = csv.writer(fileobj)
235 writer = csv.writer(fileobj)
241 writer = csv.writer(fileobj)
248 writer = csv.writer(fileobj)
258 writer = csv.writer(fileobj)
264 reader = csv.reader(input, **kwargs)
271 self.assertRaises(csv.Error, self._read_test,
274 self.assertRaises(csv.Error, self._read_test,
278 self.assertRaises(csv.Error, self._read_test,
287 self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], [])
288 self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], [])
289 self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], [])
295 self.assertRaises(csv.Error, self._read_test, ['a,"'], [], strict=True)
296 self.assertRaises(csv.Error, self._read_test, ['"a'], [], strict=True)
297 self.assertRaises(csv.Error, self._read_test,
313 quoting=csv.QUOTE_NONE, escapechar='\\')
316 quoting=csv.QUOTE_NONNUMERIC)
320 quoting=csv.QUOTE_NONNUMERIC)
325 limit = csv.field_size_limit()
331 csv.field_size_limit(size)
333 self.assertEqual(csv.field_size_limit(), size)
334 csv.field_size_limit(size-1)
335 self.assertRaises(csv.Error, self._read_test, [bigline], [])
336 self.assertRaises(TypeError, csv.field_size_limit, None)
337 self.assertRaises(TypeError, csv.field_size_limit, 1, None)
339 csv.field_size_limit(limit)
342 r = csv.reader(['line,1', 'line,2', 'line,3'])
355 writer = csv.writer(fileobj)
359 for i, row in enumerate(csv.reader(fileobj)):
364 writer = csv.writer(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\")
368 for i, row in enumerate(csv.reader(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\")):
373 self.assertRaises(TypeError, csv.list_dialects, None)
374 self.assertRaises(TypeError, csv.get_dialect)
375 self.assertRaises(csv.Error, csv.get_dialect, None)
376 self.assertRaises(csv.Error, csv.get_dialect, "nonesuch")
377 self.assertRaises(TypeError, csv.unregister_dialect)
378 self.assertRaises(csv.Error, csv.unregister_dialect, None)
379 self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch")
380 self.assertRaises(TypeError, csv.register_dialect, None)
381 self.assertRaises(TypeError, csv.register_dialect, None, None)
382 self.assertRaises(TypeError, csv.register_dialect, "nonesuch", 0, 0)
383 self.assertRaises(TypeError, csv.register_dialect, "nonesuch",
385 self.assertRaises(TypeError, csv.register_dialect, "nonesuch",
387 self.assertRaises(TypeError, csv.register_dialect, [])
390 class myexceltsv(csv.excel):
393 expected_dialects = csv.list_dialects() + [name]
395 csv.register_dialect(name, myexceltsv)
396 self.addCleanup(csv.unregister_dialect, name)
397 self.assertEqual(csv.get_dialect(name).delimiter, '\t')
398 got_dialects = sorted(csv.list_dialects())
403 csv.register_dialect(name, delimiter=';')
404 self.addCleanup(csv.unregister_dialect, name)
405 self.assertEqual(csv.get_dialect(name).delimiter, ';')
406 self.assertEqual([['X', 'Y', 'Z']], list(csv.reader(['X;Y;Z'], name)))
409 class myexceltsv(csv.Dialect):
411 self.assertRaises(csv.Error, myexceltsv)
414 class space(csv.excel):
416 quoting = csv.QUOTE_NONE
422 reader = csv.reader(fileobj, dialect=space())
430 writer = csv.writer(fileobj, *writeargs, **kwwriteargs)
436 class testA(csv.excel):
438 class testB(csv.excel):
440 class testC(csv.excel):
442 class testUni(csv.excel):
445 csv.register_dialect('testC', testC)
457 csv.unregister_dialect('testC')
461 self.assertRaises(TypeError, csv.reader, [], bad_attr = 0)
463 self.assertRaises(TypeError, csv.reader, [], delimiter = None)
464 self.assertRaises(TypeError, csv.reader, [], quoting = -1)
465 self.assertRaises(TypeError, csv.reader, [], quoting = 100)
468 for name in csv.list_dialects():
469 dialect = csv.get_dialect(name)
473 for name in csv.list_dialects():
474 dialect = csv.get_dialect(name)
483 reader = csv.reader(fileobj, dialect = self.dialect)
489 writer = csv.writer(fileobj, dialect = self.dialect)
591 class EscapedExcel(csv.excel):
592 quoting = csv.QUOTE_NONE
613 class QuotedEscapedExcel(csv.excel):
614 quoting = csv.QUOTE_NONNUMERIC
631 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
637 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
648 writer = csv.DictWriter(fileobj, fieldnames=["f1", "f2", "f3"])
658 self.assertRaises(TypeError, csv.DictWriter, fileobj)
662 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
674 self.assertRaises(ValueError, csv.DictWriter, fileobj, ['f1', 'f2'],
679 writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="raise")
681 self.assertRaises(ValueError, csv.DictWriter.writerow, writer, dictrow)
685 writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="ignore")
687 csv.DictWriter.writerow(writer, dictrow)
694 reader = csv.DictReader(fileobj,
702 reader = csv.DictReader(fileobj)
712 reader = csv.DictReader(fileobj,
713 fieldnames=next(csv.reader(fileobj)))
722 reader = csv.DictReader(fileobj)
732 reader = csv.DictReader(fileobj,
741 reader = csv.DictReader(fileobj,
750 reader = csv.DictReader(fileobj, restkey="_rest")
759 reader = csv.DictReader(fileobj,
775 reader = csv.DictReader(sample,
784 reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n",
793 reader = csv.DictReader(["1;2;abc;4;5;6\r\n"],
806 writer = csv.writer(fileobj, dialect="excel")
817 writer = csv.writer(fileobj, dialect="excel")
828 writer = csv.writer(fileobj, dialect="excel")
839 writer = csv.writer(fileobj, dialect="excel")
847 class mydialect(csv.Dialect):
853 quoting = csv.QUOTE_NONE
855 self.assertEqual(d.quoting, csv.QUOTE_NONE)
858 self.assertRaises(csv.Error, mydialect)
861 mydialect.quoting = csv.QUOTE_ALL
864 self.assertEqual(d.quoting, csv.QUOTE_ALL)
869 with self.assertRaises(csv.Error) as cm:
875 with self.assertRaises(csv.Error) as cm:
881 class mydialect(csv.Dialect):
887 quoting = csv.QUOTE_NONE
892 with self.assertRaises(csv.Error) as cm:
898 with self.assertRaises(csv.Error) as cm:
904 with self.assertRaises(csv.Error) as cm:
910 with self.assertRaises(csv.Error) as cm:
916 class mydialect(csv.Dialect):
922 quoting = csv.QUOTE_NONE
931 with self.assertRaises(csv.Error) as cm:
938 class mydialect(csv.Dialect):
945 self.assertRaises(csv.Error, create_invalid, field_name, "")
946 self.assertRaises(csv.Error, create_invalid, field_name, "abc")
947 self.assertRaises(csv.Error, create_invalid, field_name, b'x')
948 self.assertRaises(csv.Error, create_invalid, field_name, 5)
1002 sniffer = csv.Sniffer()
1007 sniffer = csv.Sniffer()
1012 sniffer = csv.Sniffer()
1022 sniffer = csv.Sniffer()
1034 sniffer = csv.Sniffer()
1060 sniffer = csv.Sniffer()
1087 csv.reader(["a,b,c\r\n"])
1088 csv.reader(["a,b,c\r\n"])
1089 csv.reader(["a,b,c\r\n"])
1092 # if csv.reader() leaks, last delta should be 3 or more
1103 csv.writer(s)
1104 csv.writer(s)
1105 csv.writer(s)
1108 # if csv.writer() leaks, last delta should be 3 or more
1119 rdr = csv.reader(rows)
1136 writer = csv.writer(s)
1155 reader = csv.reader(fileobj)
1161 writer = csv.writer(fileobj)
1173 dw = csv.DictWriter(fileobject, keys)
1176 dr = csv.DictReader(fileobject)
1193 self.assertEqual(list(csv.DictReader(data)),
1201 self.assertEqual(list(csv.DictReader(data, restkey='OtherInfo')),
1210 self.assertEqual(list(csv.DictReader(data, fieldnames=['fname', 'lname'])),
1222 support.check__all__(self, csv, ('csv', '_csv'), extra=extra)