Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_csv.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | ee2c80eeca2a |
| children |
| rev | line source |
|---|---|
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
1 # Copyright (C) 2001,2002 Python Software Foundation |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
2 # csv package unit tests |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
3 |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
4 import io |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
5 import sys |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
6 import os |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
7 import unittest |
|
52789
fb7b7727a731
Update documentation for csv module to reflect changes in code,
R. David Murray <rdmurray@bitdance.com>
parents:
52688
diff
changeset
|
8 from io import StringIO |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
9 from tempfile import TemporaryFile |
|
28570
bfc7a9fa34d6
hoist contents of csv submodule up to the package level
Skip Montanaro <skip@pobox.com>
parents:
28348
diff
changeset
|
10 import csv |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
11 import gc |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43918
diff
changeset
|
12 from test import support |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
13 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
14 class Test_Csv(unittest.TestCase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
15 """ |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
16 Test the underlying C csv parser in ways that are not appropriate |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
17 from the high level interface. Further tests of this nature are done |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
18 in TestDialectRegistry. |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
19 """ |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
20 def _test_arg_valid(self, ctor, arg): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
21 self.assertRaises(TypeError, ctor) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
22 self.assertRaises(TypeError, ctor, None) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
23 self.assertRaises(TypeError, ctor, arg, bad_attr = 0) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
24 self.assertRaises(TypeError, ctor, arg, delimiter = 0) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
25 self.assertRaises(TypeError, ctor, arg, delimiter = 'XX') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
26 self.assertRaises(csv.Error, ctor, arg, 'foo') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
27 self.assertRaises(TypeError, ctor, arg, delimiter=None) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
28 self.assertRaises(TypeError, ctor, arg, delimiter=1) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
29 self.assertRaises(TypeError, ctor, arg, quotechar=1) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
30 self.assertRaises(TypeError, ctor, arg, lineterminator=None) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
31 self.assertRaises(TypeError, ctor, arg, lineterminator=1) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
32 self.assertRaises(TypeError, ctor, arg, quoting=None) |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
33 self.assertRaises(TypeError, ctor, arg, |
|
34404
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
34 quoting=csv.QUOTE_ALL, quotechar='') |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
35 self.assertRaises(TypeError, ctor, arg, |
|
34408
72fd373cd0f9
Fix logic problem in quoting=csv.QUOTE_ALL, quotechar=None check, add test.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34407
diff
changeset
|
36 quoting=csv.QUOTE_ALL, quotechar=None) |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
37 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
38 def test_reader_arg_valid(self): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
39 self._test_arg_valid(csv.reader, []) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
40 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
41 def test_writer_arg_valid(self): |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
42 self._test_arg_valid(csv.writer, StringIO()) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
43 |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
44 def _test_default_attrs(self, ctor, *args): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
45 obj = ctor(*args) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
46 # Check defaults |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
47 self.assertEqual(obj.dialect.delimiter, ',') |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
48 self.assertEqual(obj.dialect.doublequote, True) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
49 self.assertEqual(obj.dialect.escapechar, None) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
50 self.assertEqual(obj.dialect.lineterminator, "\r\n") |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
51 self.assertEqual(obj.dialect.quotechar, '"') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
52 self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
53 self.assertEqual(obj.dialect.skipinitialspace, False) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
54 self.assertEqual(obj.dialect.strict, False) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
55 # Try deleting or changing attributes (they are read-only) |
|
41317
b897b77a004f
Make readonly members defined in C throw an AttributeError on modification. This brings them into sync with Python-level attributes. Fixes bug #1687163.
Collin Winter <collinw@gmail.com>
parents:
40775
diff
changeset
|
56 self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter') |
|
b897b77a004f
Make readonly members defined in C throw an AttributeError on modification. This brings them into sync with Python-level attributes. Fixes bug #1687163.
Collin Winter <collinw@gmail.com>
parents:
40775
diff
changeset
|
57 self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':') |
|
34921
4c8e5243ca42
Fix tests dependent on the exception raised by non-settable descriptors.
Barry Warsaw <barry@python.org>
parents:
34419
diff
changeset
|
58 self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting') |
|
4c8e5243ca42
Fix tests dependent on the exception raised by non-settable descriptors.
Barry Warsaw <barry@python.org>
parents:
34419
diff
changeset
|
59 self.assertRaises(AttributeError, setattr, obj.dialect, |
|
4c8e5243ca42
Fix tests dependent on the exception raised by non-settable descriptors.
Barry Warsaw <barry@python.org>
parents:
34419
diff
changeset
|
60 'quoting', None) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
61 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
62 def test_reader_attrs(self): |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
63 self._test_default_attrs(csv.reader, []) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
64 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
65 def test_writer_attrs(self): |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
66 self._test_default_attrs(csv.writer, StringIO()) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
67 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
68 def _test_kw_attrs(self, ctor, *args): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
69 # Now try with alternate options |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
70 kwargs = dict(delimiter=':', doublequote=False, escapechar='\\', |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
71 lineterminator='\r', quotechar='*', |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
72 quoting=csv.QUOTE_NONE, skipinitialspace=True, |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
73 strict=True) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
74 obj = ctor(*args, **kwargs) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
75 self.assertEqual(obj.dialect.delimiter, ':') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
76 self.assertEqual(obj.dialect.doublequote, False) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
77 self.assertEqual(obj.dialect.escapechar, '\\') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
78 self.assertEqual(obj.dialect.lineterminator, "\r") |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
79 self.assertEqual(obj.dialect.quotechar, '*') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
80 self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
81 self.assertEqual(obj.dialect.skipinitialspace, True) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
82 self.assertEqual(obj.dialect.strict, True) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
83 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
84 def test_reader_kw_attrs(self): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
85 self._test_kw_attrs(csv.reader, []) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
86 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
87 def test_writer_kw_attrs(self): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
88 self._test_kw_attrs(csv.writer, StringIO()) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
89 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
90 def _test_dialect_attrs(self, ctor, *args): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
91 # Now try with dialect-derived options |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
92 class dialect: |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
93 delimiter='-' |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
94 doublequote=False |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
95 escapechar='^' |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
96 lineterminator='$' |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
97 quotechar='#' |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
98 quoting=csv.QUOTE_ALL |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
99 skipinitialspace=True |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
100 strict=False |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
101 args = args + (dialect,) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
102 obj = ctor(*args) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
103 self.assertEqual(obj.dialect.delimiter, '-') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
104 self.assertEqual(obj.dialect.doublequote, False) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
105 self.assertEqual(obj.dialect.escapechar, '^') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
106 self.assertEqual(obj.dialect.lineterminator, "$") |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
107 self.assertEqual(obj.dialect.quotechar, '#') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
108 self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
109 self.assertEqual(obj.dialect.skipinitialspace, True) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
110 self.assertEqual(obj.dialect.strict, False) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
111 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
112 def test_reader_dialect_attrs(self): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
113 self._test_dialect_attrs(csv.reader, []) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
114 |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
115 def test_writer_dialect_attrs(self): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
116 self._test_dialect_attrs(csv.writer, StringIO()) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
117 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
118 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
119 def _write_test(self, fields, expect, **kwargs): |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
120 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
121 writer = csv.writer(fileobj, **kwargs) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
122 writer.writerow(fields) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
123 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
124 self.assertEqual(fileobj.read(), |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
125 expect + writer.dialect.lineterminator) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
126 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
127 def test_write_arg_valid(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
128 self.assertRaises(csv.Error, self._write_test, None, '') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
129 self._write_test((), '') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
130 self._write_test([None], '""') |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
131 self.assertRaises(csv.Error, self._write_test, |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
132 [None], None, quoting = csv.QUOTE_NONE) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
133 # Check that exceptions are passed up the chain |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
134 class BadList: |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
135 def __len__(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
136 return 10; |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
137 def __getitem__(self, i): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
138 if i > 2: |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80180
diff
changeset
|
139 raise OSError |
|
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80180
diff
changeset
|
140 self.assertRaises(OSError, self._write_test, BadList(), '') |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
141 class BadItem: |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
142 def __str__(self): |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80180
diff
changeset
|
143 raise OSError |
|
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80180
diff
changeset
|
144 self.assertRaises(OSError, self._write_test, [BadItem()], '') |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
145 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
146 def test_write_bigfield(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
147 # This exercises the buffer realloc functionality |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
148 bigstring = 'X' * 50000 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
149 self._write_test([bigstring,bigstring], '%s,%s' % \ |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
150 (bigstring, bigstring)) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
151 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
152 def test_write_quoting(self): |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
153 self._write_test(['a',1,'p,q'], 'a,1,"p,q"') |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
154 self.assertRaises(csv.Error, |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
155 self._write_test, |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
156 ['a',1,'p,q'], 'a,1,p,q', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
157 quoting = csv.QUOTE_NONE) |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
158 self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
159 quoting = csv.QUOTE_MINIMAL) |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
160 self._write_test(['a',1,'p,q'], '"a",1,"p,q"', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
161 quoting = csv.QUOTE_NONNUMERIC) |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
162 self._write_test(['a',1,'p,q'], '"a","1","p,q"', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
163 quoting = csv.QUOTE_ALL) |
|
52688
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
164 self._write_test(['a\nb',1], '"a\nb","1"', |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
165 quoting = csv.QUOTE_ALL) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
166 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
167 def test_write_escape(self): |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
168 self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
169 escapechar='\\') |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
170 self.assertRaises(csv.Error, |
|
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
171 self._write_test, |
|
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
172 ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
173 escapechar=None, doublequote=False) |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
174 self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
|
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
175 escapechar='\\', doublequote = False) |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
176 self._write_test(['"'], '""""', |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
177 escapechar='\\', quoting = csv.QUOTE_MINIMAL) |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
178 self._write_test(['"'], '\\"', |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
179 escapechar='\\', quoting = csv.QUOTE_MINIMAL, |
|
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
180 doublequote = False) |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
181 self._write_test(['"'], '\\"', |
|
34407
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
182 escapechar='\\', quoting = csv.QUOTE_NONE) |
|
c88ab292bf05
When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34406
diff
changeset
|
183 self._write_test(['a',1,'p,q'], 'a,1,p\\,q', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
184 escapechar='\\', quoting = csv.QUOTE_NONE) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
185 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
186 def test_writerows(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
187 class BrokenFile: |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
188 def write(self, buf): |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80180
diff
changeset
|
189 raise OSError |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
190 writer = csv.writer(BrokenFile()) |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80180
diff
changeset
|
191 self.assertRaises(OSError, writer.writerows, [['a']]) |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
192 |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
193 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
194 writer = csv.writer(fileobj) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
195 self.assertRaises(TypeError, writer.writerows, None) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
196 writer.writerows([['a','b'],['c','d']]) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
197 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
198 self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n") |
|
32538
4fab4a5cce2f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32258
diff
changeset
|
199 |
|
80180
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
200 @support.cpython_only |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
201 def test_writerows_legacy_strings(self): |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
202 import _testcapi |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
203 |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
204 c = _testcapi.unicode_legacy_string('a') |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
205 with TemporaryFile("w+", newline='') as fileobj: |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
206 writer = csv.writer(fileobj) |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
207 writer.writerows([[c]]) |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
208 fileobj.seek(0) |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
209 self.assertEqual(fileobj.read(), "a\r\n") |
|
0b5f00ccf907
Issue #16145: Support legacy strings in the _csv module.
Stefan Krah <skrah@bytereef.org>
parents:
79157
diff
changeset
|
210 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
211 def _read_test(self, input, expect, **kwargs): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
212 reader = csv.reader(input, **kwargs) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
213 result = list(reader) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
214 self.assertEqual(result, expect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
215 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
216 def test_read_oddinputs(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
217 self._read_test([], []) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
218 self._read_test([''], [[]]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
219 self.assertRaises(csv.Error, self._read_test, |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
220 ['"ab"c'], None, strict = 1) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
221 # cannot handle null bytes for the moment |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
222 self.assertRaises(csv.Error, self._read_test, |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
223 ['ab\0c'], None, strict = 1) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
224 self._read_test(['"ab"c'], [['abc']], doublequote = 0) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
225 |
|
43918
146ba97ffe1a
Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42931
diff
changeset
|
226 self.assertRaises(csv.Error, self._read_test, |
|
146ba97ffe1a
Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42931
diff
changeset
|
227 [b'ab\0c'], None) |
|
146ba97ffe1a
Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42931
diff
changeset
|
228 |
|
146ba97ffe1a
Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42931
diff
changeset
|
229 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
230 def test_read_eol(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
231 self._read_test(['a,b'], [['a','b']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
232 self._read_test(['a,b\n'], [['a','b']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
233 self._read_test(['a,b\r\n'], [['a','b']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
234 self._read_test(['a,b\r'], [['a','b']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
235 self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], []) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
236 self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], []) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
237 self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], []) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
238 |
|
79156
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
239 def test_read_eof(self): |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
240 self._read_test(['a,"'], [['a', '']]) |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
241 self._read_test(['"a'], [['a']]) |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
242 self._read_test(['^'], [['\n']], escapechar='^') |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
243 self.assertRaises(csv.Error, self._read_test, ['a,"'], [], strict=True) |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
244 self.assertRaises(csv.Error, self._read_test, ['"a'], [], strict=True) |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
245 self.assertRaises(csv.Error, self._read_test, |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
246 ['^'], [], escapechar='^', strict=True) |
|
e9c005676d6e
Issue #16013: Fix CSV Reader parsing issue with ending quote characters. Patch by Serhiy Storchaka.
Senthil Kumaran <senthil@uthcode.com>
parents:
68257
diff
changeset
|
247 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
248 def test_read_escape(self): |
|
34380
ba6e17271c62
Fix parsing of csv files with escapes (escape character previously would be
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34358
diff
changeset
|
249 self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\') |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
250 self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
251 self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') |
|
34380
ba6e17271c62
Fix parsing of csv files with escapes (escape character previously would be
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34358
diff
changeset
|
252 self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\') |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
253 self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
254 self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
255 |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
256 def test_read_quoting(self): |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
257 self._read_test(['1,",3,",5'], [['1', ',3,', '5']]) |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
258 self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
259 quotechar=None, escapechar='\\') |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
260 self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
|
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
261 quoting=csv.QUOTE_NONE, escapechar='\\') |
|
34409
1d866c02a8ca
When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34408
diff
changeset
|
262 # will this fail where locale uses comma for decimals? |
|
34410
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
263 self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]], |
|
34409
1d866c02a8ca
When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34408
diff
changeset
|
264 quoting=csv.QUOTE_NONNUMERIC) |
|
52688
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
265 self._read_test(['"a\nb", 7'], [['a\nb', ' 7']]) |
|
34419
652b85d35a75
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34410
diff
changeset
|
266 self.assertRaises(ValueError, self._read_test, |
|
34409
1d866c02a8ca
When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34408
diff
changeset
|
267 ['abc,3'], [[]], |
|
1d866c02a8ca
When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34408
diff
changeset
|
268 quoting=csv.QUOTE_NONNUMERIC) |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
269 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
270 def test_read_bigfield(self): |
|
34396
7059fb8b9eb3
Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34393
diff
changeset
|
271 # This exercises the buffer realloc functionality and field size |
|
7059fb8b9eb3
Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34393
diff
changeset
|
272 # limits. |
|
34406
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
273 limit = csv.field_size_limit() |
|
34404
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
274 try: |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
275 size = 50000 |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
276 bigstring = 'X' * size |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
277 bigline = '%s,%s' % (bigstring, bigstring) |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
278 self._read_test([bigline], [[bigstring, bigstring]]) |
|
34406
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
279 csv.field_size_limit(size) |
|
34404
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
280 self._read_test([bigline], [[bigstring, bigstring]]) |
|
34406
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
281 self.assertEqual(csv.field_size_limit(), size) |
|
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
282 csv.field_size_limit(size-1) |
|
34404
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
283 self.assertRaises(csv.Error, self._read_test, [bigline], []) |
|
34406
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
284 self.assertRaises(TypeError, csv.field_size_limit, None) |
|
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
285 self.assertRaises(TypeError, csv.field_size_limit, 1, None) |
|
34404
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
286 finally: |
|
34406
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34404
diff
changeset
|
287 csv.field_size_limit(limit) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
288 |
|
34410
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
289 def test_read_linenum(self): |
|
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
290 r = csv.reader(['line,1', 'line,2', 'line,3']) |
|
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
291 self.assertEqual(r.line_num, 0) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
292 next(r) |
|
34410
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
293 self.assertEqual(r.line_num, 1) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
294 next(r) |
|
34410
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
295 self.assertEqual(r.line_num, 2) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
296 next(r) |
|
34410
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
297 self.assertEqual(r.line_num, 3) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
298 self.assertRaises(StopIteration, next, r) |
|
34410
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
299 self.assertEqual(r.line_num, 3) |
|
a70dd8767b07
Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34409
diff
changeset
|
300 |
|
52688
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
301 def test_roundtrip_quoteed_newlines(self): |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
302 with TemporaryFile("w+", newline='') as fileobj: |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
303 writer = csv.writer(fileobj) |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
304 self.assertRaises(TypeError, writer.writerows, None) |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
305 rows = [['a\nb','b'],['c','x\r\nd']] |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
306 writer.writerows(rows) |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
307 fileobj.seek(0) |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
308 for i, row in enumerate(csv.reader(fileobj)): |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
309 self.assertEqual(row, rows[i]) |
|
1e1dadbbdd01
Merged revisions 70997 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
48670
diff
changeset
|
310 |
|
82815
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
311 def test_roundtrip_escaped_unquoted_newlines(self): |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
312 with TemporaryFile("w+", newline='') as fileobj: |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
313 writer = csv.writer(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\") |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
314 rows = [['a\nb','b'],['c','x\r\nd']] |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
315 writer.writerows(rows) |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
316 fileobj.seek(0) |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
317 for i, row in enumerate(csv.reader(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\")): |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
318 self.assertEqual(row,rows[i]) |
|
940748853712
#15927: Fix cvs.reader parsing of escaped \r\n with quoting off.
R David Murray <rdmurray@bitdance.com>
parents:
81039
diff
changeset
|
319 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
320 class TestDialectRegistry(unittest.TestCase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
321 def test_registry_badargs(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
322 self.assertRaises(TypeError, csv.list_dialects, None) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
323 self.assertRaises(TypeError, csv.get_dialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
324 self.assertRaises(csv.Error, csv.get_dialect, None) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
325 self.assertRaises(csv.Error, csv.get_dialect, "nonesuch") |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
326 self.assertRaises(TypeError, csv.unregister_dialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
327 self.assertRaises(csv.Error, csv.unregister_dialect, None) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
328 self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch") |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
329 self.assertRaises(TypeError, csv.register_dialect, None) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
330 self.assertRaises(TypeError, csv.register_dialect, None, None) |
|
34404
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
331 self.assertRaises(TypeError, csv.register_dialect, "nonesuch", 0, 0) |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
332 self.assertRaises(TypeError, csv.register_dialect, "nonesuch", |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
333 badargument=None) |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
334 self.assertRaises(TypeError, csv.register_dialect, "nonesuch", |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
335 quoting=None) |
|
a8599c788218
Improve test coverage fractionally.
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
336 self.assertRaises(TypeError, csv.register_dialect, []) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
337 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
338 def test_registry(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
339 class myexceltsv(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
340 delimiter = "\t" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
341 name = "myexceltsv" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
342 expected_dialects = csv.list_dialects() + [name] |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
343 expected_dialects.sort() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
344 csv.register_dialect(name, myexceltsv) |
|
66612
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
345 self.addCleanup(csv.unregister_dialect, name) |
|
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
346 self.assertEqual(csv.get_dialect(name).delimiter, '\t') |
|
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
347 got_dialects = sorted(csv.list_dialects()) |
|
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
348 self.assertEqual(expected_dialects, got_dialects) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
349 |
|
34391
0a1608a5eb05
Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34380
diff
changeset
|
350 def test_register_kwargs(self): |
|
0a1608a5eb05
Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34380
diff
changeset
|
351 name = 'fedcba' |
|
0a1608a5eb05
Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34380
diff
changeset
|
352 csv.register_dialect(name, delimiter=';') |
|
66612
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
353 self.addCleanup(csv.unregister_dialect, name) |
|
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
354 self.assertEqual(csv.get_dialect(name).delimiter, ';') |
|
5f2412843bb9
Merged revisions 86940 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
56368
diff
changeset
|
355 self.assertEqual([['X', 'Y', 'Z']], list(csv.reader(['X;Y;Z'], name))) |
|
34391
0a1608a5eb05
Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34380
diff
changeset
|
356 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
357 def test_incomplete_dialect(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
358 class myexceltsv(csv.Dialect): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
359 delimiter = "\t" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
360 self.assertRaises(csv.Error, myexceltsv) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
361 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
362 def test_space_dialect(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
363 class space(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
364 delimiter = " " |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
365 quoting = csv.QUOTE_NONE |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
366 escapechar = "\\" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
367 |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
368 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
369 fileobj.write("abc def\nc1ccccc1 benzene\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
370 fileobj.seek(0) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
371 reader = csv.reader(fileobj, dialect=space()) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
372 self.assertEqual(next(reader), ["abc", "def"]) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
373 self.assertEqual(next(reader), ["c1ccccc1", "benzene"]) |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
374 |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
375 def compare_dialect_123(self, expected, *writeargs, **kwwriteargs): |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
376 |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
377 with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
378 |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
379 writer = csv.writer(fileobj, *writeargs, **kwwriteargs) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
380 writer.writerow([1,2,3]) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
381 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
382 self.assertEqual(fileobj.read(), expected) |
|
32538
4fab4a5cce2f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32258
diff
changeset
|
383 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
384 def test_dialect_apply(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
385 class testA(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
386 delimiter = "\t" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
387 class testB(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
388 delimiter = ":" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
389 class testC(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
390 delimiter = "|" |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
391 class testUni(csv.excel): |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
392 delimiter = "\u039B" |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
393 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
394 csv.register_dialect('testC', testC) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
395 try: |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
396 self.compare_dialect_123("1,2,3\r\n") |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
397 self.compare_dialect_123("1\t2\t3\r\n", testA) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
398 self.compare_dialect_123("1:2:3\r\n", dialect=testB()) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
399 self.compare_dialect_123("1|2|3\r\n", dialect='testC') |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
400 self.compare_dialect_123("1;2;3\r\n", dialect=testA, |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
401 delimiter=';') |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
402 self.compare_dialect_123("1\u039B2\u039B3\r\n", |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
403 dialect=testUni) |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
404 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
405 finally: |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
406 csv.unregister_dialect('testC') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
407 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
408 def test_bad_dialect(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
409 # Unknown parameter |
|
34340
be4929e5179b
Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents:
32538
diff
changeset
|
410 self.assertRaises(TypeError, csv.reader, [], bad_attr = 0) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
411 # Bad values |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
412 self.assertRaises(TypeError, csv.reader, [], delimiter = None) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
413 self.assertRaises(TypeError, csv.reader, [], quoting = -1) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
414 self.assertRaises(TypeError, csv.reader, [], quoting = 100) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
415 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
416 class TestCsvBase(unittest.TestCase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
417 def readerAssertEqual(self, input, expected_result): |
|
43918
146ba97ffe1a
Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42931
diff
changeset
|
418 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
419 fileobj.write(input) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
420 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
421 reader = csv.reader(fileobj, dialect = self.dialect) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
422 fields = list(reader) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
423 self.assertEqual(fields, expected_result) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
424 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
425 def writerAssertEqual(self, input, expected_result): |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
426 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
427 writer = csv.writer(fileobj, dialect = self.dialect) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
428 writer.writerows(input) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
429 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
430 self.assertEqual(fileobj.read(), expected_result) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
431 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
432 class TestDialectExcel(TestCsvBase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
433 dialect = 'excel' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
434 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
435 def test_single(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
436 self.readerAssertEqual('abc', [['abc']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
437 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
438 def test_simple(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
439 self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
440 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
441 def test_blankline(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
442 self.readerAssertEqual('', []) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
443 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
444 def test_empty_fields(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
445 self.readerAssertEqual(',', [['', '']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
446 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
447 def test_singlequoted(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
448 self.readerAssertEqual('""', [['']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
449 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
450 def test_singlequoted_left_empty(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
451 self.readerAssertEqual('"",', [['','']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
452 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
453 def test_singlequoted_right_empty(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
454 self.readerAssertEqual(',""', [['','']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
455 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
456 def test_single_quoted_quote(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
457 self.readerAssertEqual('""""', [['"']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
458 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
459 def test_quoted_quotes(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
460 self.readerAssertEqual('""""""', [['""']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
461 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
462 def test_inline_quote(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
463 self.readerAssertEqual('a""b', [['a""b']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
464 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
465 def test_inline_quotes(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
466 self.readerAssertEqual('a"b"c', [['a"b"c']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
467 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
468 def test_quotes_and_more(self): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
469 # Excel would never write a field containing '"a"b', but when |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
470 # reading one, it will return 'ab'. |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
471 self.readerAssertEqual('"a"b', [['ab']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
472 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
473 def test_lone_quote(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
474 self.readerAssertEqual('a"b', [['a"b']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
475 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
476 def test_quote_and_quote(self): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
477 # Excel would never write a field containing '"a" "b"', but when |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
478 # reading one, it will return 'a "b"'. |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
479 self.readerAssertEqual('"a" "b"', [['a "b"']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
480 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
481 def test_space_and_quote(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
482 self.readerAssertEqual(' "a"', [[' "a"']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
483 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
484 def test_quoted(self): |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
485 self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', |
|
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
486 [['1', '2', '3', |
|
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
487 'I think, therefore I am', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
488 '5', '6']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
489 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
490 def test_quoted_quote(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
491 self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"', |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
492 [['1', '2', '3', |
|
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
493 '"I see," said the blind man', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
494 'as he picked up his hammer and saw']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
495 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
496 def test_quoted_nl(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
497 input = '''\ |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
498 1,2,3,"""I see,"" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
499 said the blind man","as he picked up his |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
500 hammer and saw" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
501 9,8,7,6''' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
502 self.readerAssertEqual(input, |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
503 [['1', '2', '3', |
|
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28570
diff
changeset
|
504 '"I see,"\nsaid the blind man', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
505 'as he picked up his\nhammer and saw'], |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
506 ['9','8','7','6']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
507 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
508 def test_dubious_quote(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
509 self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
510 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
511 def test_null(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
512 self.writerAssertEqual([], '') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
513 |
|
59613
77c71f258df7
Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents:
59081
diff
changeset
|
514 def test_single_writer(self): |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
515 self.writerAssertEqual([['abc']], 'abc\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
516 |
|
59613
77c71f258df7
Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents:
59081
diff
changeset
|
517 def test_simple_writer(self): |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
518 self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
519 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
520 def test_quotes(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
521 self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
522 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
523 def test_quote_fieldsep(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
524 self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
525 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
526 def test_newlines(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
527 self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
528 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
529 class EscapedExcel(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
530 quoting = csv.QUOTE_NONE |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
531 escapechar = '\\' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
532 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
533 class TestEscapedExcel(TestCsvBase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
534 dialect = EscapedExcel() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
535 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
536 def test_escape_fieldsep(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
537 self.writerAssertEqual([['abc,def']], 'abc\\,def\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
538 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
539 def test_read_escape_fieldsep(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
540 self.readerAssertEqual('abc\\,def\r\n', [['abc,def']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
541 |
|
65655
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
542 class TestDialectUnix(TestCsvBase): |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
543 dialect = 'unix' |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
544 |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
545 def test_simple_writer(self): |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
546 self.writerAssertEqual([[1, 'abc def', 'abc']], '"1","abc def","abc"\n') |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
547 |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
548 def test_simple_reader(self): |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
549 self.readerAssertEqual('"1","abc def","abc"\n', [['1', 'abc def', 'abc']]) |
|
9e4a2809e55c
#5975: add unix_dialect to csv module.
Georg Brandl <georg@python.org>
parents:
65368
diff
changeset
|
550 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
551 class QuotedEscapedExcel(csv.excel): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
552 quoting = csv.QUOTE_NONNUMERIC |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
553 escapechar = '\\' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
554 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
555 class TestQuotedEscapedExcel(TestCsvBase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
556 dialect = QuotedEscapedExcel() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
557 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
558 def test_write_escape_fieldsep(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
559 self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
560 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
561 def test_read_escape_fieldsep(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
562 self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']]) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
563 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
564 class TestDictFields(unittest.TestCase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
565 ### "long" means the row is longer than the number of fieldnames |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
566 ### "short" means there are fewer elements in the row than fieldnames |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
567 def test_write_simple_dict(self): |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
568 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
569 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
|
59081
77de7b1bcd59
Merged revisions 78384 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58415
diff
changeset
|
570 writer.writeheader() |
|
77de7b1bcd59
Merged revisions 78384 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58415
diff
changeset
|
571 fileobj.seek(0) |
|
77de7b1bcd59
Merged revisions 78384 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58415
diff
changeset
|
572 self.assertEqual(fileobj.readline(), "f1,f2,f3\r\n") |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
573 writer.writerow({"f1": 10, "f3": "abc"}) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
574 fileobj.seek(0) |
|
59081
77de7b1bcd59
Merged revisions 78384 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58415
diff
changeset
|
575 fileobj.readline() # header |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
576 self.assertEqual(fileobj.read(), "10,,abc\r\n") |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
577 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
578 def test_write_no_fields(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
579 fileobj = StringIO() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
580 self.assertRaises(TypeError, csv.DictWriter, fileobj) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
581 |
|
87272
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
582 def test_write_fields_not_in_fieldnames(self): |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
583 with TemporaryFile("w+", newline='') as fileobj: |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
584 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
585 # Of special note is the non-string key (issue 19449) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
586 with self.assertRaises(ValueError) as cx: |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
587 writer.writerow({"f4": 10, "f2": "spam", 1: "abc"}) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
588 exception = str(cx.exception) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
589 self.assertIn("fieldnames", exception) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
590 self.assertIn("'f4'", exception) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
591 self.assertNotIn("'f2'", exception) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
592 self.assertIn("1", exception) |
|
6e5afeada7ca
#19449: Handle non-string keys when generating 'fieldnames' error.
R David Murray <rdmurray@bitdance.com>
parents:
86895
diff
changeset
|
593 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
594 def test_read_dict_fields(self): |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
595 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
596 fileobj.write("1,2,abc\r\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
597 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
598 reader = csv.DictReader(fileobj, |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
599 fieldnames=["f1", "f2", "f3"]) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
600 self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
601 |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
602 def test_read_dict_no_fieldnames(self): |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
603 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
604 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
605 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
606 reader = csv.DictReader(fileobj) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
607 self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
|
48670
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
608 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
609 |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
610 # Two test cases to make sure existing ways of implicitly setting |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
611 # fieldnames continue to work. Both arise from discussion in issue3436. |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
612 def test_read_dict_fieldnames_from_file(self): |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
613 with TemporaryFile("w+") as fileobj: |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
614 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
615 fileobj.seek(0) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
616 reader = csv.DictReader(fileobj, |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
617 fieldnames=next(csv.reader(fileobj))) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
618 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
619 self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
620 |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
621 def test_read_dict_fieldnames_chain(self): |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
622 import itertools |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
623 with TemporaryFile("w+") as fileobj: |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
624 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
625 fileobj.seek(0) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
626 reader = csv.DictReader(fileobj) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
627 first = next(reader) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
628 for row in itertools.chain([first], reader): |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
629 self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) |
|
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
630 self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'}) |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
631 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
632 def test_read_long(self): |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
633 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
634 fileobj.write("1,2,abc,4,5,6\r\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
635 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
636 reader = csv.DictReader(fileobj, |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
637 fieldnames=["f1", "f2"]) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
638 self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
639 None: ["abc", "4", "5", "6"]}) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
640 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
641 def test_read_long_with_rest(self): |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
642 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
643 fileobj.write("1,2,abc,4,5,6\r\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
644 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
645 reader = csv.DictReader(fileobj, |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
646 fieldnames=["f1", "f2"], restkey="_rest") |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
647 self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
648 "_rest": ["abc", "4", "5", "6"]}) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
649 |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
650 def test_read_long_with_rest_no_fieldnames(self): |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
651 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
652 fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
653 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
654 reader = csv.DictReader(fileobj, restkey="_rest") |
|
48670
1a11717a154b
Merged revisions 65605 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
655 self.assertEqual(reader.fieldnames, ["f1", "f2"]) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
656 self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
657 "_rest": ["abc", "4", "5", "6"]}) |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
658 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
659 def test_read_short(self): |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
660 with TemporaryFile("w+") as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
661 fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
662 fileobj.seek(0) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
663 reader = csv.DictReader(fileobj, |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
664 fieldnames="1 2 3 4 5 6".split(), |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
665 restval="DEFAULT") |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
666 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
667 "4": '4', "5": '5', "6": '6'}) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
668 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
669 "4": 'DEFAULT', "5": 'DEFAULT', |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
670 "6": 'DEFAULT'}) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
671 |
|
29410
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
672 def test_read_multi(self): |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
673 sample = [ |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
674 '2147483648,43.0e12,17,abc,def\r\n', |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
675 '147483648,43.0e2,17,abc,def\r\n', |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
676 '47483648,43.0,170,abc,def\r\n' |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
677 ] |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
678 |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
679 reader = csv.DictReader(sample, |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
680 fieldnames="i1 float i2 s1 s2".split()) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
681 self.assertEqual(next(reader), {"i1": '2147483648', |
|
29410
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
682 "float": '43.0e12', |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
683 "i2": '17', |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
684 "s1": 'abc', |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
685 "s2": 'def'}) |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
686 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
687 def test_read_with_blanks(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
688 reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n", |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
689 "1,2,abc,4,5,6\r\n"], |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
690 fieldnames="1 2 3 4 5 6".split()) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
691 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
692 "4": '4', "5": '5', "6": '6'}) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
693 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
694 "4": '4', "5": '5', "6": '6'}) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
695 |
|
30356
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
29410
diff
changeset
|
696 def test_read_semi_sep(self): |
|
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
29410
diff
changeset
|
697 reader = csv.DictReader(["1;2;abc;4;5;6\r\n"], |
|
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
29410
diff
changeset
|
698 fieldnames="1 2 3 4 5 6".split(), |
|
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
29410
diff
changeset
|
699 delimiter=';') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41317
diff
changeset
|
700 self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
|
30356
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
29410
diff
changeset
|
701 "4": '4', "5": '5', "6": '6'}) |
|
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
29410
diff
changeset
|
702 |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
703 class TestArrayWrites(unittest.TestCase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
704 def test_int_write(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
705 import array |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
706 contents = [(20-i) for i in range(20)] |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
707 a = array.array('i', contents) |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
708 |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
709 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
710 writer = csv.writer(fileobj, dialect="excel") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
711 writer.writerow(a) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
712 expected = ",".join([str(i) for i in a])+"\r\n" |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
713 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
714 self.assertEqual(fileobj.read(), expected) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
715 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
716 def test_double_write(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
717 import array |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
718 contents = [(20-i)*0.1 for i in range(20)] |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
719 a = array.array('d', contents) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
720 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
721 writer = csv.writer(fileobj, dialect="excel") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
722 writer.writerow(a) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
723 expected = ",".join([str(i) for i in a])+"\r\n" |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
724 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
725 self.assertEqual(fileobj.read(), expected) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
726 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
727 def test_float_write(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
728 import array |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
729 contents = [(20-i)*0.1 for i in range(20)] |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
730 a = array.array('f', contents) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
731 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
732 writer = csv.writer(fileobj, dialect="excel") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
733 writer.writerow(a) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
734 expected = ",".join([str(i) for i in a])+"\r\n" |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
735 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
736 self.assertEqual(fileobj.read(), expected) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
737 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
738 def test_char_write(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
739 import array, string |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
42501
diff
changeset
|
740 a = array.array('u', string.ascii_letters) |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
741 |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
742 with TemporaryFile("w+", newline='') as fileobj: |
|
32258
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
743 writer = csv.writer(fileobj, dialect="excel") |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
744 writer.writerow(a) |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
745 expected = ",".join(a)+"\r\n" |
|
3dfe81ac0e7c
Rewrote to use temporary files instead of StringIO objects in most places.
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
746 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
747 self.assertEqual(fileobj.read(), expected) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
748 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
749 class TestDialectValidity(unittest.TestCase): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
750 def test_quoting(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
751 class mydialect(csv.Dialect): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
752 delimiter = ";" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
753 escapechar = '\\' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
754 doublequote = False |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
755 skipinitialspace = True |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
756 lineterminator = '\r\n' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
757 quoting = csv.QUOTE_NONE |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
758 d = mydialect() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
759 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
760 mydialect.quoting = None |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
761 self.assertRaises(csv.Error, mydialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
762 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
763 mydialect.doublequote = True |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
764 mydialect.quoting = csv.QUOTE_ALL |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
765 mydialect.quotechar = '"' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
766 d = mydialect() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
767 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
768 mydialect.quotechar = "''" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
769 self.assertRaises(csv.Error, mydialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
770 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
771 mydialect.quotechar = 4 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
772 self.assertRaises(csv.Error, mydialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
773 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
774 def test_delimiter(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
775 class mydialect(csv.Dialect): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
776 delimiter = ";" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
777 escapechar = '\\' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
778 doublequote = False |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
779 skipinitialspace = True |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
780 lineterminator = '\r\n' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
781 quoting = csv.QUOTE_NONE |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
782 d = mydialect() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
783 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
784 mydialect.delimiter = ":::" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
785 self.assertRaises(csv.Error, mydialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
786 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
787 mydialect.delimiter = 4 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
788 self.assertRaises(csv.Error, mydialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
789 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
790 def test_lineterminator(self): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
791 class mydialect(csv.Dialect): |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
792 delimiter = ";" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
793 escapechar = '\\' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
794 doublequote = False |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
795 skipinitialspace = True |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
796 lineterminator = '\r\n' |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
797 quoting = csv.QUOTE_NONE |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
798 d = mydialect() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
799 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
800 mydialect.lineterminator = ":::" |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
801 d = mydialect() |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
802 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
803 mydialect.lineterminator = 4 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
804 self.assertRaises(csv.Error, mydialect) |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
805 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
806 |
| 28827 | 807 class TestSniffer(unittest.TestCase): |
| 808 sample1 = """\ | |
| 809 Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes | |
| 810 Shark City, Glendale Heights, IL, 12/28/02, Prezence | |
| 811 Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow | |
| 812 Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back | |
| 813 """ | |
| 814 sample2 = """\ | |
| 815 'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes' | |
| 816 'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence' | |
| 817 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow' | |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
818 'Stonecutters ''Seafood'' and Chop House':'Lemont':'IL':'12/19/02':'Week Back' |
| 28827 | 819 """ |
|
84386
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
820 header1 = '''\ |
| 28827 | 821 "venue","city","state","date","performers" |
| 822 ''' | |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
823 sample3 = '''\ |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
824 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
825 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
826 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
827 ''' |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
828 |
|
29410
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
829 sample4 = '''\ |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
830 2147483648;43.0e12;17;abc;def |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
831 147483648;43.0e2;17;abc;def |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
832 47483648;43.0;170;abc;def |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
833 ''' |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
834 |
|
36146
22dbf5818b0b
Fix for problem with Sniffer class. If your delimiter is whitespace and the
Skip Montanaro <skip@pobox.com>
parents:
34921
diff
changeset
|
835 sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n" |
|
36157
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
836 sample6 = "a|b|c\r\nd|e|f\r\n" |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
837 sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n" |
|
36146
22dbf5818b0b
Fix for problem with Sniffer class. If your delimiter is whitespace and the
Skip Montanaro <skip@pobox.com>
parents:
34921
diff
changeset
|
838 |
|
84386
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
839 # Issue 18155: Use a delimiter that is a special char to regex: |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
840 |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
841 header2 = '''\ |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
842 "venue"+"city"+"state"+"date"+"performers" |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
843 ''' |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
844 sample8 = """\ |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
845 Harry's+ Arlington Heights+ IL+ 2/1/03+ Kimi Hayes |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
846 Shark City+ Glendale Heights+ IL+ 12/28/02+ Prezence |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
847 Tommy's Place+ Blue Island+ IL+ 12/28/02+ Blue Sunday/White Crow |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
848 Stonecutters Seafood and Chop House+ Lemont+ IL+ 12/19/02+ Week Back |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
849 """ |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
850 sample9 = """\ |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
851 'Harry''s'+ Arlington Heights'+ 'IL'+ '2/1/03'+ 'Kimi Hayes' |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
852 'Shark City'+ Glendale Heights'+' IL'+ '12/28/02'+ 'Prezence' |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
853 'Tommy''s Place'+ Blue Island'+ 'IL'+ '12/28/02'+ 'Blue Sunday/White Crow' |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
854 'Stonecutters ''Seafood'' and Chop House'+ 'Lemont'+ 'IL'+ '12/19/02'+ 'Week Back' |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
855 """ |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
856 |
| 28827 | 857 def test_has_header(self): |
| 858 sniffer = csv.Sniffer() | |
| 859 self.assertEqual(sniffer.has_header(self.sample1), False) | |
|
84386
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
860 self.assertEqual(sniffer.has_header(self.header1 + self.sample1), |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
861 True) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
862 |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
863 def test_has_header_regex_special_delimiter(self): |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
864 sniffer = csv.Sniffer() |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
865 self.assertEqual(sniffer.has_header(self.sample8), False) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
866 self.assertEqual(sniffer.has_header(self.header2 + self.sample8), |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
867 True) |
| 28827 | 868 |
| 869 def test_sniff(self): | |
| 870 sniffer = csv.Sniffer() | |
| 871 dialect = sniffer.sniff(self.sample1) | |
| 872 self.assertEqual(dialect.delimiter, ",") | |
| 873 self.assertEqual(dialect.quotechar, '"') | |
| 874 self.assertEqual(dialect.skipinitialspace, True) | |
| 875 | |
| 876 dialect = sniffer.sniff(self.sample2) | |
| 877 self.assertEqual(dialect.delimiter, ":") | |
| 878 self.assertEqual(dialect.quotechar, "'") | |
| 879 self.assertEqual(dialect.skipinitialspace, False) | |
| 880 | |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
881 def test_delimiters(self): |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
882 sniffer = csv.Sniffer() |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
883 dialect = sniffer.sniff(self.sample3) |
| 38464 | 884 # given that all three lines in sample3 are equal, |
| 885 # I think that any character could have been 'guessed' as the | |
| 886 # delimiter, depending on dictionary order | |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
56368
diff
changeset
|
887 self.assertIn(dialect.delimiter, self.sample3) |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
888 dialect = sniffer.sniff(self.sample3, delimiters="?,") |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
889 self.assertEqual(dialect.delimiter, "?") |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
890 dialect = sniffer.sniff(self.sample3, delimiters="/,") |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
891 self.assertEqual(dialect.delimiter, "/") |
|
29410
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
892 dialect = sniffer.sniff(self.sample4) |
|
f2df9c9f0fcb
add a couple test cases which involve longs and floats in 'e' format.
Skip Montanaro <skip@pobox.com>
parents:
29169
diff
changeset
|
893 self.assertEqual(dialect.delimiter, ";") |
|
36146
22dbf5818b0b
Fix for problem with Sniffer class. If your delimiter is whitespace and the
Skip Montanaro <skip@pobox.com>
parents:
34921
diff
changeset
|
894 dialect = sniffer.sniff(self.sample5) |
|
22dbf5818b0b
Fix for problem with Sniffer class. If your delimiter is whitespace and the
Skip Montanaro <skip@pobox.com>
parents:
34921
diff
changeset
|
895 self.assertEqual(dialect.delimiter, "\t") |
|
36157
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
896 dialect = sniffer.sniff(self.sample6) |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
897 self.assertEqual(dialect.delimiter, "|") |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
898 dialect = sniffer.sniff(self.sample7) |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
899 self.assertEqual(dialect.delimiter, "|") |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
900 self.assertEqual(dialect.quotechar, "'") |
|
84386
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
901 dialect = sniffer.sniff(self.sample8) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
902 self.assertEqual(dialect.delimiter, '+') |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
903 dialect = sniffer.sniff(self.sample9) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
904 self.assertEqual(dialect.delimiter, '+') |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
905 self.assertEqual(dialect.quotechar, "'") |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28991
diff
changeset
|
906 |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
907 def test_doublequote(self): |
|
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
908 sniffer = csv.Sniffer() |
|
84386
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
909 dialect = sniffer.sniff(self.header1) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
910 self.assertFalse(dialect.doublequote) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
911 dialect = sniffer.sniff(self.header2) |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
912 self.assertFalse(dialect.doublequote) |
|
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
913 dialect = sniffer.sniff(self.sample2) |
|
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
914 self.assertTrue(dialect.doublequote) |
|
84386
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
915 dialect = sniffer.sniff(self.sample8) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
916 self.assertFalse(dialect.doublequote) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
917 dialect = sniffer.sniff(self.sample9) |
|
68ff68f9a0d5
#18155: Regex-escape delimiter, in case it is a regex special char.
R David Murray <rdmurray@bitdance.com>
parents:
80180
diff
changeset
|
918 self.assertTrue(dialect.doublequote) |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
919 |
|
86895
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
920 class NUL: |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
921 def write(s, *args): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
922 pass |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
923 writelines = write |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
924 |
|
86895
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
925 @unittest.skipUnless(hasattr(sys, "gettotalrefcount"), |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
926 'requires sys.gettotalrefcount()') |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
927 class TestLeaks(unittest.TestCase): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
928 def test_create_read(self): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
929 delta = 0 |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
930 lastrc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
931 for i in range(20): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
932 gc.collect() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
933 self.assertEqual(gc.garbage, []) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
934 rc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
935 csv.reader(["a,b,c\r\n"]) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
936 csv.reader(["a,b,c\r\n"]) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
937 csv.reader(["a,b,c\r\n"]) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
938 delta = rc-lastrc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
939 lastrc = rc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
940 # if csv.reader() leaks, last delta should be 3 or more |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
941 self.assertEqual(delta < 3, True) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
942 |
|
86895
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
943 def test_create_write(self): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
944 delta = 0 |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
945 lastrc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
946 s = NUL() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
947 for i in range(20): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
948 gc.collect() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
949 self.assertEqual(gc.garbage, []) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
950 rc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
951 csv.writer(s) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
952 csv.writer(s) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
953 csv.writer(s) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
954 delta = rc-lastrc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
955 lastrc = rc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
956 # if csv.writer() leaks, last delta should be 3 or more |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
957 self.assertEqual(delta < 3, True) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
958 |
|
86895
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
959 def test_read(self): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
960 delta = 0 |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
961 rows = ["a,b,c\r\n"]*5 |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
962 lastrc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
963 for i in range(20): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
964 gc.collect() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
965 self.assertEqual(gc.garbage, []) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
966 rc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
967 rdr = csv.reader(rows) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
968 for row in rdr: |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
969 pass |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
970 delta = rc-lastrc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
971 lastrc = rc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
972 # if reader leaks during read, delta should be 5 or more |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
973 self.assertEqual(delta < 5, True) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
974 |
|
86895
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
975 def test_write(self): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
976 delta = 0 |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
977 rows = [[1,2,3]]*5 |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
978 s = NUL() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
979 lastrc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
980 for i in range(20): |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
981 gc.collect() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
982 self.assertEqual(gc.garbage, []) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
983 rc = sys.gettotalrefcount() |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
984 writer = csv.writer(s) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
985 for row in rows: |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
986 writer.writerow(row) |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
987 delta = rc-lastrc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
988 lastrc = rc |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
989 # if writer leaks during write, last delta should be 5 or more |
|
1feeeb8992f8
Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84386
diff
changeset
|
990 self.assertEqual(delta < 5, True) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
991 |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
992 class TestUnicode(unittest.TestCase): |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
993 |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
994 names = ["Martin von Löwis", |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
995 "Marc André Lemburg", |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
996 "Guido van Rossum", |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
997 "François Pinard"] |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
998 |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
999 def test_unicode_read(self): |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1000 import io |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
1001 with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1002 fileobj.write(",".join(self.names) + "\r\n") |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1003 fileobj.seek(0) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1004 reader = csv.reader(fileobj) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1005 self.assertEqual(list(reader), [self.names]) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1006 |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1007 |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1008 def test_unicode_write(self): |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1009 import io |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
1010 with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
1011 writer = csv.writer(fileobj) |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1012 writer.writerow(self.names) |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1013 expected = ",".join(self.names)+"\r\n" |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1014 fileobj.seek(0) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42585
diff
changeset
|
1015 self.assertEqual(fileobj.read(), expected) |
|
42462
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1016 |
|
8b45f2297dbd
SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents:
42293
diff
changeset
|
1017 |
|
28991
ce6c3cc422ab
add not-yet-supported Unicode test just so it doesn't get lost.
Skip Montanaro <skip@pobox.com>
parents:
28917
diff
changeset
|
1018 |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28827
diff
changeset
|
1019 def test_main(): |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
1020 mod = sys.modules[__name__] |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43918
diff
changeset
|
1021 support.run_unittest( |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28827
diff
changeset
|
1022 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28827
diff
changeset
|
1023 ) |
|
28339
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
1024 |
|
cc35ed2b26a8
new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
1025 if __name__ == '__main__': |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28827
diff
changeset
|
1026 test_main() |
