Guido van Rossum | d77d699 | 2007-07-16 23:10:57 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 2 | # Copyright (C) 2001,2002 Python Software Foundation |
| 3 | # csv package unit tests |
| 4 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 5 | import io |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 6 | import sys |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 7 | import os |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 8 | import unittest |
R. David Murray | 8b7d4aa | 2009-04-04 01:38:38 +0000 | [diff] [blame] | 9 | from io import StringIO |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 10 | from tempfile import TemporaryFile |
Skip Montanaro | 594adac | 2003-04-10 17:16:15 +0000 | [diff] [blame] | 11 | import csv |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 12 | import gc |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import support |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 14 | |
| 15 | class Test_Csv(unittest.TestCase): |
| 16 | """ |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 17 | Test the underlying C csv parser in ways that are not appropriate |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 18 | from the high level interface. Further tests of this nature are done |
| 19 | in TestDialectRegistry. |
| 20 | """ |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 21 | def _test_arg_valid(self, ctor, arg): |
| 22 | self.assertRaises(TypeError, ctor) |
| 23 | self.assertRaises(TypeError, ctor, None) |
| 24 | self.assertRaises(TypeError, ctor, arg, bad_attr = 0) |
| 25 | self.assertRaises(TypeError, ctor, arg, delimiter = 0) |
| 26 | self.assertRaises(TypeError, ctor, arg, delimiter = 'XX') |
| 27 | self.assertRaises(csv.Error, ctor, arg, 'foo') |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 28 | self.assertRaises(TypeError, ctor, arg, delimiter=None) |
| 29 | self.assertRaises(TypeError, ctor, arg, delimiter=1) |
| 30 | self.assertRaises(TypeError, ctor, arg, quotechar=1) |
| 31 | self.assertRaises(TypeError, ctor, arg, lineterminator=None) |
| 32 | self.assertRaises(TypeError, ctor, arg, lineterminator=1) |
| 33 | self.assertRaises(TypeError, ctor, arg, quoting=None) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 34 | self.assertRaises(TypeError, ctor, arg, |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 35 | quoting=csv.QUOTE_ALL, quotechar='') |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 36 | self.assertRaises(TypeError, ctor, arg, |
Andrew McNamara | 5d45a8d | 2005-01-12 08:16:17 +0000 | [diff] [blame] | 37 | quoting=csv.QUOTE_ALL, quotechar=None) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 38 | |
| 39 | def test_reader_arg_valid(self): |
| 40 | self._test_arg_valid(csv.reader, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 41 | |
| 42 | def test_writer_arg_valid(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 43 | self._test_arg_valid(csv.writer, StringIO()) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 44 | |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 45 | def _test_default_attrs(self, ctor, *args): |
| 46 | obj = ctor(*args) |
| 47 | # Check defaults |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 48 | self.assertEqual(obj.dialect.delimiter, ',') |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 49 | self.assertEqual(obj.dialect.doublequote, True) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 50 | self.assertEqual(obj.dialect.escapechar, None) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 51 | self.assertEqual(obj.dialect.lineterminator, "\r\n") |
| 52 | self.assertEqual(obj.dialect.quotechar, '"') |
| 53 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) |
| 54 | self.assertEqual(obj.dialect.skipinitialspace, False) |
| 55 | self.assertEqual(obj.dialect.strict, False) |
| 56 | # Try deleting or changing attributes (they are read-only) |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 57 | self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter') |
| 58 | self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':') |
Barry Warsaw | b180c06 | 2005-04-20 19:41:36 +0000 | [diff] [blame] | 59 | self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting') |
| 60 | self.assertRaises(AttributeError, setattr, obj.dialect, |
| 61 | 'quoting', None) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 62 | |
| 63 | def test_reader_attrs(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 64 | self._test_default_attrs(csv.reader, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 65 | |
| 66 | def test_writer_attrs(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 67 | self._test_default_attrs(csv.writer, StringIO()) |
| 68 | |
| 69 | def _test_kw_attrs(self, ctor, *args): |
| 70 | # Now try with alternate options |
| 71 | kwargs = dict(delimiter=':', doublequote=False, escapechar='\\', |
| 72 | lineterminator='\r', quotechar='*', |
| 73 | quoting=csv.QUOTE_NONE, skipinitialspace=True, |
| 74 | strict=True) |
| 75 | obj = ctor(*args, **kwargs) |
| 76 | self.assertEqual(obj.dialect.delimiter, ':') |
| 77 | self.assertEqual(obj.dialect.doublequote, False) |
| 78 | self.assertEqual(obj.dialect.escapechar, '\\') |
| 79 | self.assertEqual(obj.dialect.lineterminator, "\r") |
| 80 | self.assertEqual(obj.dialect.quotechar, '*') |
| 81 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE) |
| 82 | self.assertEqual(obj.dialect.skipinitialspace, True) |
| 83 | self.assertEqual(obj.dialect.strict, True) |
| 84 | |
| 85 | def test_reader_kw_attrs(self): |
| 86 | self._test_kw_attrs(csv.reader, []) |
| 87 | |
| 88 | def test_writer_kw_attrs(self): |
| 89 | self._test_kw_attrs(csv.writer, StringIO()) |
| 90 | |
| 91 | def _test_dialect_attrs(self, ctor, *args): |
| 92 | # Now try with dialect-derived options |
| 93 | class dialect: |
| 94 | delimiter='-' |
| 95 | doublequote=False |
| 96 | escapechar='^' |
| 97 | lineterminator='$' |
| 98 | quotechar='#' |
| 99 | quoting=csv.QUOTE_ALL |
| 100 | skipinitialspace=True |
| 101 | strict=False |
| 102 | args = args + (dialect,) |
| 103 | obj = ctor(*args) |
| 104 | self.assertEqual(obj.dialect.delimiter, '-') |
| 105 | self.assertEqual(obj.dialect.doublequote, False) |
| 106 | self.assertEqual(obj.dialect.escapechar, '^') |
| 107 | self.assertEqual(obj.dialect.lineterminator, "$") |
| 108 | self.assertEqual(obj.dialect.quotechar, '#') |
| 109 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL) |
| 110 | self.assertEqual(obj.dialect.skipinitialspace, True) |
| 111 | self.assertEqual(obj.dialect.strict, False) |
| 112 | |
| 113 | def test_reader_dialect_attrs(self): |
| 114 | self._test_dialect_attrs(csv.reader, []) |
| 115 | |
| 116 | def test_writer_dialect_attrs(self): |
| 117 | self._test_dialect_attrs(csv.writer, StringIO()) |
| 118 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 119 | |
| 120 | def _write_test(self, fields, expect, **kwargs): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 121 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 122 | writer = csv.writer(fileobj, **kwargs) |
| 123 | writer.writerow(fields) |
| 124 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 125 | self.assertEqual(fileobj.read(), |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 126 | expect + writer.dialect.lineterminator) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 127 | |
| 128 | def test_write_arg_valid(self): |
| 129 | self.assertRaises(csv.Error, self._write_test, None, '') |
| 130 | self._write_test((), '') |
| 131 | self._write_test([None], '""') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 132 | self.assertRaises(csv.Error, self._write_test, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 133 | [None], None, quoting = csv.QUOTE_NONE) |
| 134 | # Check that exceptions are passed up the chain |
| 135 | class BadList: |
| 136 | def __len__(self): |
| 137 | return 10; |
| 138 | def __getitem__(self, i): |
| 139 | if i > 2: |
| 140 | raise IOError |
| 141 | self.assertRaises(IOError, self._write_test, BadList(), '') |
| 142 | class BadItem: |
| 143 | def __str__(self): |
| 144 | raise IOError |
| 145 | self.assertRaises(IOError, self._write_test, [BadItem()], '') |
| 146 | |
| 147 | def test_write_bigfield(self): |
| 148 | # This exercises the buffer realloc functionality |
| 149 | bigstring = 'X' * 50000 |
| 150 | self._write_test([bigstring,bigstring], '%s,%s' % \ |
| 151 | (bigstring, bigstring)) |
| 152 | |
| 153 | def test_write_quoting(self): |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 154 | self._write_test(['a',1,'p,q'], 'a,1,"p,q"') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 155 | self.assertRaises(csv.Error, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 156 | self._write_test, |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 157 | ['a',1,'p,q'], 'a,1,p,q', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 158 | quoting = csv.QUOTE_NONE) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 159 | self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 160 | quoting = csv.QUOTE_MINIMAL) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 161 | self._write_test(['a',1,'p,q'], '"a",1,"p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 162 | quoting = csv.QUOTE_NONNUMERIC) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 163 | self._write_test(['a',1,'p,q'], '"a","1","p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 164 | quoting = csv.QUOTE_ALL) |
R. David Murray | cde68cf | 2009-04-01 22:37:58 +0000 | [diff] [blame] | 165 | self._write_test(['a\nb',1], '"a\nb","1"', |
| 166 | quoting = csv.QUOTE_ALL) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 167 | |
| 168 | def test_write_escape(self): |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 169 | self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 170 | escapechar='\\') |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 171 | self.assertRaises(csv.Error, |
| 172 | self._write_test, |
| 173 | ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 174 | escapechar=None, doublequote=False) |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 175 | self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
| 176 | escapechar='\\', doublequote = False) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 177 | self._write_test(['"'], '""""', |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 178 | escapechar='\\', quoting = csv.QUOTE_MINIMAL) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 179 | self._write_test(['"'], '\\"', |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 180 | escapechar='\\', quoting = csv.QUOTE_MINIMAL, |
| 181 | doublequote = False) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 182 | self._write_test(['"'], '\\"', |
Andrew McNamara | c89f284 | 2005-01-12 07:44:42 +0000 | [diff] [blame] | 183 | escapechar='\\', quoting = csv.QUOTE_NONE) |
| 184 | self._write_test(['a',1,'p,q'], 'a,1,p\\,q', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 185 | 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 Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 194 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 195 | writer = csv.writer(fileobj) |
| 196 | self.assertRaises(TypeError, writer.writerows, None) |
| 197 | writer.writerows([['a','b'],['c','d']]) |
| 198 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 199 | self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n") |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 200 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 201 | 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'Arc | 10c476d | 2007-11-19 21:20:21 +0000 | [diff] [blame] | 216 | self.assertRaises(csv.Error, self._read_test, |
| 217 | [b'ab\0c'], None) |
| 218 | |
| 219 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 220 | 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 McNamara | 36a7691 | 2005-01-10 01:04:40 +0000 | [diff] [blame] | 230 | self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 231 | self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') |
| 232 | self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') |
Andrew McNamara | 36a7691 | 2005-01-10 01:04:40 +0000 | [diff] [blame] | 233 | self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 234 | self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') |
| 235 | self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') |
| 236 | |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 237 | 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 McNamara | 0f0599d | 2005-01-12 09:45:18 +0000 | [diff] [blame] | 243 | # will this fail where locale uses comma for decimals? |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 244 | 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] | 245 | quoting=csv.QUOTE_NONNUMERIC) |
R. David Murray | cde68cf | 2009-04-01 22:37:58 +0000 | [diff] [blame] | 246 | self._read_test(['"a\nb", 7'], [['a\nb', ' 7']]) |
Tim Peters | 608c2ff | 2005-01-13 17:37:38 +0000 | [diff] [blame] | 247 | self.assertRaises(ValueError, self._read_test, |
Andrew McNamara | 0f0599d | 2005-01-12 09:45:18 +0000 | [diff] [blame] | 248 | ['abc,3'], [[]], |
| 249 | quoting=csv.QUOTE_NONNUMERIC) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 250 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 251 | def test_read_bigfield(self): |
Andrew McNamara | e4d05c4 | 2005-01-11 07:32:02 +0000 | [diff] [blame] | 252 | # This exercises the buffer realloc functionality and field size |
| 253 | # limits. |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 254 | limit = csv.field_size_limit() |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 255 | try: |
| 256 | size = 50000 |
| 257 | bigstring = 'X' * size |
| 258 | bigline = '%s,%s' % (bigstring, bigstring) |
| 259 | self._read_test([bigline], [[bigstring, bigstring]]) |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 260 | csv.field_size_limit(size) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 261 | self._read_test([bigline], [[bigstring, bigstring]]) |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 262 | self.assertEqual(csv.field_size_limit(), size) |
| 263 | csv.field_size_limit(size-1) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 264 | self.assertRaises(csv.Error, self._read_test, [bigline], []) |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 265 | self.assertRaises(TypeError, csv.field_size_limit, None) |
| 266 | self.assertRaises(TypeError, csv.field_size_limit, 1, None) |
Andrew McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 267 | finally: |
Andrew McNamara | 31d8896 | 2005-01-12 03:45:10 +0000 | [diff] [blame] | 268 | csv.field_size_limit(limit) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 269 | |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 270 | def test_read_linenum(self): |
| 271 | r = csv.reader(['line,1', 'line,2', 'line,3']) |
| 272 | self.assertEqual(r.line_num, 0) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 273 | next(r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 274 | self.assertEqual(r.line_num, 1) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 275 | next(r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 276 | self.assertEqual(r.line_num, 2) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 277 | next(r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 278 | self.assertEqual(r.line_num, 3) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 279 | self.assertRaises(StopIteration, next, r) |
Andrew McNamara | 7f2053e | 2005-01-12 11:17:16 +0000 | [diff] [blame] | 280 | self.assertEqual(r.line_num, 3) |
| 281 | |
R. David Murray | cde68cf | 2009-04-01 22:37:58 +0000 | [diff] [blame] | 282 | 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 292 | class 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 McNamara | af1e312 | 2005-01-12 01:55:21 +0000 | [diff] [blame] | 303 | 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 309 | |
| 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: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 318 | self.assertTrue(csv.get_dialect(name).delimiter, '\t') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 319 | 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 McNamara | 8662597 | 2005-01-11 01:28:33 +0000 | [diff] [blame] | 325 | def test_register_kwargs(self): |
| 326 | name = 'fedcba' |
| 327 | csv.register_dialect(name, delimiter=';') |
| 328 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 329 | self.assertTrue(csv.get_dialect(name).delimiter, '\t') |
| 330 | self.assertTrue(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z']) |
Andrew McNamara | 8662597 | 2005-01-11 01:28:33 +0000 | [diff] [blame] | 331 | finally: |
| 332 | csv.unregister_dialect(name) |
| 333 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 334 | 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 Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 345 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 346 | fileobj.write("abc def\nc1ccccc1 benzene\n") |
| 347 | fileobj.seek(0) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 348 | reader = csv.reader(fileobj, dialect=space()) |
| 349 | self.assertEqual(next(reader), ["abc", "def"]) |
| 350 | self.assertEqual(next(reader), ["c1ccccc1", "benzene"]) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 351 | |
| 352 | def compare_dialect_123(self, expected, *writeargs, **kwwriteargs): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 353 | |
| 354 | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
| 355 | |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 356 | writer = csv.writer(fileobj, *writeargs, **kwwriteargs) |
| 357 | writer.writerow([1,2,3]) |
| 358 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 359 | self.assertEqual(fileobj.read(), expected) |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 360 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 361 | 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 Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 368 | class testUni(csv.excel): |
| 369 | delimiter = "\u039B" |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 370 | |
| 371 | csv.register_dialect('testC', testC) |
| 372 | try: |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 373 | 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 381 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 382 | finally: |
| 383 | csv.unregister_dialect('testC') |
| 384 | |
| 385 | def test_bad_dialect(self): |
| 386 | # Unknown parameter |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 387 | self.assertRaises(TypeError, csv.reader, [], bad_attr = 0) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 388 | # 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 | |
| 393 | class TestCsvBase(unittest.TestCase): |
| 394 | def readerAssertEqual(self, input, expected_result): |
Amaury Forgeot d'Arc | 10c476d | 2007-11-19 21:20:21 +0000 | [diff] [blame] | 395 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 396 | 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 401 | |
| 402 | def writerAssertEqual(self, input, expected_result): |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 403 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 404 | writer = csv.writer(fileobj, dialect = self.dialect) |
| 405 | writer.writerows(input) |
| 406 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 407 | self.assertEqual(fileobj.read(), expected_result) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 408 | |
| 409 | class 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 Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 446 | # Excel would never write a field containing '"a"b', but when |
| 447 | # reading one, it will return 'ab'. |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 448 | 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 Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 454 | # Excel would never write a field containing '"a" "b"', but when |
| 455 | # reading one, it will return 'a "b"'. |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 456 | 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 Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 462 | self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', |
| 463 | [['1', '2', '3', |
| 464 | 'I think, therefore I am', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 465 | '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 Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 469 | [['1', '2', '3', |
| 470 | '"I see," said the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 471 | 'as he picked up his hammer and saw']]) |
| 472 | |
| 473 | def test_quoted_nl(self): |
| 474 | input = '''\ |
| 475 | 1,2,3,"""I see,"" |
| 476 | said the blind man","as he picked up his |
| 477 | hammer and saw" |
| 478 | 9,8,7,6''' |
| 479 | self.readerAssertEqual(input, |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 480 | [['1', '2', '3', |
| 481 | '"I see,"\nsaid the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 482 | '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 | |
| 506 | class EscapedExcel(csv.excel): |
| 507 | quoting = csv.QUOTE_NONE |
| 508 | escapechar = '\\' |
| 509 | |
| 510 | class 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 | |
| 519 | class QuotedEscapedExcel(csv.excel): |
| 520 | quoting = csv.QUOTE_NONNUMERIC |
| 521 | escapechar = '\\' |
| 522 | |
| 523 | class 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 532 | class 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 536 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 537 | writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
| 538 | writer.writerow({"f1": 10, "f3": "abc"}) |
| 539 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 540 | self.assertEqual(fileobj.read(), "10,,abc\r\n") |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 541 | |
| 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 Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 547 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 548 | fileobj.write("1,2,abc\r\n") |
| 549 | fileobj.seek(0) |
| 550 | reader = csv.DictReader(fileobj, |
| 551 | fieldnames=["f1", "f2", "f3"]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 552 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 553 | |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 554 | def test_read_dict_no_fieldnames(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 555 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 556 | fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
| 557 | fileobj.seek(0) |
| 558 | reader = csv.DictReader(fileobj) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 559 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
Skip Montanaro | af8fcfa | 2008-08-09 19:44:22 +0000 | [diff] [blame] | 560 | 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 Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 583 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 584 | def test_read_long(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 585 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 586 | fileobj.write("1,2,abc,4,5,6\r\n") |
| 587 | fileobj.seek(0) |
| 588 | reader = csv.DictReader(fileobj, |
| 589 | fieldnames=["f1", "f2"]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 590 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 591 | None: ["abc", "4", "5", "6"]}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 592 | |
| 593 | def test_read_long_with_rest(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 594 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 595 | fileobj.write("1,2,abc,4,5,6\r\n") |
| 596 | fileobj.seek(0) |
| 597 | reader = csv.DictReader(fileobj, |
| 598 | fieldnames=["f1", "f2"], restkey="_rest") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 599 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 600 | "_rest": ["abc", "4", "5", "6"]}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 601 | |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 602 | def test_read_long_with_rest_no_fieldnames(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 603 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 604 | 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 Montanaro | af8fcfa | 2008-08-09 19:44:22 +0000 | [diff] [blame] | 607 | self.assertEqual(reader.fieldnames, ["f1", "f2"]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 608 | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 609 | "_rest": ["abc", "4", "5", "6"]}) |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 610 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 611 | def test_read_short(self): |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 612 | with TemporaryFile("w+") as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 613 | 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 Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 618 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 619 | "4": '4', "5": '5', "6": '6'}) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 620 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 621 | "4": 'DEFAULT', "5": 'DEFAULT', |
| 622 | "6": 'DEFAULT'}) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 623 | |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 624 | 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 Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 633 | self.assertEqual(next(reader), {"i1": '2147483648', |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 634 | "float": '43.0e12', |
| 635 | "i2": '17', |
| 636 | "s1": 'abc', |
| 637 | "s2": 'def'}) |
| 638 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 639 | 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 Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 643 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 644 | "4": '4', "5": '5', "6": '6'}) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 645 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 646 | "4": '4', "5": '5', "6": '6'}) |
| 647 | |
Skip Montanaro | 3f7a948 | 2003-09-06 19:52:12 +0000 | [diff] [blame] | 648 | 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 Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 652 | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
Skip Montanaro | 3f7a948 | 2003-09-06 19:52:12 +0000 | [diff] [blame] | 653 | "4": '4', "5": '5', "6": '6'}) |
| 654 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 655 | class 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 Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 660 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 661 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 662 | 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 666 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 667 | |
| 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 672 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 673 | 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 677 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 678 | |
| 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 683 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 684 | 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 688 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 689 | |
| 690 | def test_char_write(self): |
| 691 | import array, string |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 692 | a = array.array('u', string.ascii_letters) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 693 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 694 | with TemporaryFile("w+", newline='') as fileobj: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 695 | writer = csv.writer(fileobj, dialect="excel") |
| 696 | writer.writerow(a) |
| 697 | expected = ",".join(a)+"\r\n" |
| 698 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 699 | self.assertEqual(fileobj.read(), expected) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 700 | |
| 701 | class 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 715 | 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 Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 759 | class TestSniffer(unittest.TestCase): |
| 760 | sample1 = """\ |
| 761 | Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes |
| 762 | Shark City, Glendale Heights, IL, 12/28/02, Prezence |
| 763 | Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow |
| 764 | Stonecutters 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 Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame^] | 770 | 'Stonecutters ''Seafood'' and Chop House':'Lemont':'IL':'12/19/02':'Week Back' |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 771 | """ |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 772 | header = '''\ |
| 773 | "venue","city","state","date","performers" |
| 774 | ''' |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 775 | sample3 = '''\ |
| 776 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 777 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 778 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 779 | ''' |
| 780 | |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 781 | sample4 = '''\ |
| 782 | 2147483648;43.0e12;17;abc;def |
| 783 | 147483648;43.0e2;17;abc;def |
| 784 | 47483648;43.0;170;abc;def |
| 785 | ''' |
| 786 | |
Skip Montanaro | 91bb70c | 2005-12-28 15:37:25 +0000 | [diff] [blame] | 787 | sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n" |
Skip Montanaro | 39b29be | 2005-12-30 05:09:48 +0000 | [diff] [blame] | 788 | sample6 = "a|b|c\r\nd|e|f\r\n" |
| 789 | sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n" |
Skip Montanaro | 91bb70c | 2005-12-28 15:37:25 +0000 | [diff] [blame] | 790 | |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 791 | 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 Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 808 | def test_delimiters(self): |
| 809 | sniffer = csv.Sniffer() |
| 810 | dialect = sniffer.sniff(self.sample3) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 811 | # 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 814 | self.assertTrue(dialect.delimiter in self.sample3) |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 815 | dialect = sniffer.sniff(self.sample3, delimiters="?,") |
| 816 | self.assertEqual(dialect.delimiter, "?") |
| 817 | dialect = sniffer.sniff(self.sample3, delimiters="/,") |
| 818 | self.assertEqual(dialect.delimiter, "/") |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 819 | dialect = sniffer.sniff(self.sample4) |
| 820 | self.assertEqual(dialect.delimiter, ";") |
Skip Montanaro | 91bb70c | 2005-12-28 15:37:25 +0000 | [diff] [blame] | 821 | dialect = sniffer.sniff(self.sample5) |
| 822 | self.assertEqual(dialect.delimiter, "\t") |
Skip Montanaro | 39b29be | 2005-12-30 05:09:48 +0000 | [diff] [blame] | 823 | 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 Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 828 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame^] | 829 | 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 Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 836 | if not hasattr(sys, "gettotalrefcount"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 837 | if support.verbose: print("*** skipping leakage tests ***") |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 838 | else: |
| 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 Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 848 | for i in range(20): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 849 | 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 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.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 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 | 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 Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 897 | for i in range(20): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 898 | 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 Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 909 | class 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 918 | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 919 | fileobj.write(",".join(self.names) + "\r\n") |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 920 | 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 Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 927 | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
| 928 | writer = csv.writer(fileobj) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 929 | writer.writerow(self.names) |
| 930 | expected = ",".join(self.names)+"\r\n" |
| 931 | fileobj.seek(0) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 932 | self.assertEqual(fileobj.read(), expected) |
Guido van Rossum | 4626458 | 2007-08-06 19:32:18 +0000 | [diff] [blame] | 933 | |
| 934 | |
Skip Montanaro | 1a56665 | 2003-05-06 15:56:05 +0000 | [diff] [blame] | 935 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 936 | def test_main(): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 937 | mod = sys.modules[__name__] |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 938 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 939 | *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
| 940 | ) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 941 | |
| 942 | if __name__ == '__main__': |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 943 | test_main() |