File: test_dialect.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (156 lines) | stat: -rw-r--r-- 6,494 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class AppTestDialect(object):
    spaceconfig = dict(usemodules=['_csv'])

    def test_register_dialect(self):
        import _csv

        attrs = [('delimiter', ','),
                 ('doublequote', True),
                 ('escapechar', None),
                 ('lineterminator', '\r\n'),
                 ('quotechar', '"'),
                 ('quoting', _csv.QUOTE_MINIMAL),
                 ('skipinitialspace', False),
                 ('strict', False),
                 ]

        for changeattr, newvalue in [('delimiter', ':'),
                                     ('doublequote', False),
                                     ('escapechar', '/'),
                                     ('lineterminator', '---\n'),
                                     ('quotechar', '%'),
                                     ('quoting', _csv.QUOTE_NONNUMERIC),
                                     ('skipinitialspace', True),
                                     ('strict', True)]:
            kwargs = {changeattr: newvalue}
            _csv.register_dialect('foo1', **kwargs)
            d = _csv.get_dialect('foo1')
            assert d.__class__.__name__ == 'Dialect'
            for attr, default in attrs:
                if attr == changeattr:
                    expected = newvalue
                else:
                    expected = default
                assert getattr(d, attr) == expected

    def test_register_dialect_base_1(self):
        import _csv
        _csv.register_dialect('foo1', escapechar='!')
        _csv.register_dialect('foo2', 'foo1', strict=True)
        d1 = _csv.get_dialect('foo1')
        assert d1.escapechar == '!'
        assert d1.strict == False
        d2 = _csv.get_dialect('foo2')
        assert d2.escapechar == '!'
        assert d2.strict == True

    def test_register_dialect_base_2(self):
        import _csv
        class Foo1:
            escapechar = '?'
        _csv.register_dialect('foo2', Foo1, strict=True)
        d2 = _csv.get_dialect('foo2')
        assert d2.escapechar == '?'
        assert d2.strict == True

    def test_typeerror(self):
        import _csv
        attempts = [("delimiter", '', 123),
                    ("escapechar", Ellipsis, 'foo', 0),
                    ("lineterminator", -132),
                    ("quotechar", '', 25),
                    ("quoting", 4, '', '\x00'),
                    ]
        for attempt in attempts:
            name = attempt[0]
            for value in attempt[1:]:
                kwargs = {name: value}
                exc_info = raises(TypeError, _csv.register_dialect, 'foo1', **kwargs)
                assert name in exc_info.value.args[0]

        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', lineterminator=4)
        assert exc_info.value.args[0] == '"lineterminator" must be a string'

    def test_typeerror_delimiter_message(self):
        import _csv
        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=None)
        assert str(exc_info.value) == '"delimiter" must be string, not NoneType'

    def test_typeerror_empty_escapechar(self):
        import _csv
        _csv.register_dialect('foo1_escapechar', escapechar=None) # works
        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', escapechar='')
        assert str(exc_info.value) == '"escapechar" must be a 1-character string'
        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', escapechar=b'')
        print(str(exc_info.value))
        assert str(exc_info.value) == '"escapechar" must be string or None, not bytes'

    def test_typeerror_empty_quotechar_but_quoting(self):
        import _csv
        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', quotechar='', quoting=_csv.QUOTE_ALL)
        assert str(exc_info.value) == '"quotechar" must be a 1-character string'
        exc_info = raises(TypeError, _csv.reader, [], quoting=_csv.QUOTE_NONE, quotechar='')
        print(str(exc_info.value))
        assert str(exc_info.value) == '"quotechar" must be a 1-character string'

    def test_bool_arg(self):
        # boolean arguments take *any* object and use its truth-value
        import _csv
        _csv.register_dialect('foo1', doublequote=[])
        assert _csv.get_dialect('foo1').doublequote == False
        _csv.register_dialect('foo1', skipinitialspace=2)
        assert _csv.get_dialect('foo1').skipinitialspace == True
        _csv.register_dialect('foo1', strict=_csv)    # :-/
        assert _csv.get_dialect('foo1').strict == True

    def test_delimiter(self):
        import _csv

        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=":::")
        assert exc_info.value.args[0] == '"delimiter" must be a 1-character string'

        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter="")
        assert exc_info.value.args[0] == '"delimiter" must be a 1-character string'

        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=b",")
        assert exc_info.value.args[0] == '"delimiter" must be string, not bytes'

        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=4)
        assert exc_info.value.args[0] == '"delimiter" must be string, not int'

    def test_quotechar(self):
        import _csv

        exc_info = raises(TypeError, _csv.register_dialect, 'foo1', quotechar=4)
        assert exc_info.value.args[0] == '"quotechar" must be string or None, not int'

    def test_line_terminator(self):
        # lineterminator can be the empty string
        import _csv
        _csv.register_dialect('foo1', lineterminator='')
        assert _csv.get_dialect('foo1').lineterminator == ''

    def test_unregister_dialect(self):
        import _csv
        _csv.register_dialect('foo1')
        _csv.unregister_dialect('foo1')
        raises(_csv.Error, _csv.get_dialect, 'foo1')
        raises(_csv.Error, _csv.unregister_dialect, 'foo1')

    def test_list_dialects(self):
        import _csv
        lst = _csv.list_dialects()
        assert type(lst) is list
        assert 'neverseen' not in lst
        _csv.register_dialect('neverseen')
        lst = _csv.list_dialects()
        assert 'neverseen' in lst
        _csv.unregister_dialect('neverseen')
        lst = _csv.list_dialects()
        assert 'neverseen' not in lst

    def test_pickle_dialect(self):
        import _csv
        import copy
        _csv.register_dialect('foo')
        raises(TypeError, copy.copy, _csv.get_dialect('foo'))