File: list.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (309 lines) | stat: -rw-r--r-- 7,226 bytes parent folder | download | duplicates (5)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

cimport cython

def f(obj1, obj2, obj3, obj4, obj5):
    """
    >>> f(1, 2, 3, 4, 5)
    []
    """
    obj1 = []
    return obj1

def g(obj1, obj2, obj3, obj4, obj5):
    """
    >>> g(1, 2, 3, 4, 5)
    [2]
    """
    obj1 = [obj2]
    return obj1

def h(obj1, obj2, obj3, obj4, obj5):
    """
    >>> h(1, 2, 3, 4, 5)
    [2, 3]
    """
    obj1 = [obj2, obj3]
    return obj1

def j(obj1, obj2, obj3, obj4, obj5):
    """
    >>> j(1, 2, 3, 4, 5)
    [2, 3, 4]
    """
    obj1 = [obj2, obj3, obj4]
    return obj1

def k(obj1, obj2, obj3, obj4, obj5):
    """
    >>> k(1, 2, 3, 4, 5)
    [17, 42, 88]
    """
    obj1 = [17, 42, 88]
    return obj1

@cython.test_fail_if_path_exists("//SimpleCallNode")
def test_list_call(ob):
    """
    >>> def f():
    ...     yield 1
    ...     yield 2
    ...
    >>> list(f())
    [1, 2]
    """
    return list(ob)

def test_list_sort():
    """
    >>> test_list_sort()
    [1, 2, 3, 4]
    """
    cdef list l1
    l1 = [2,3,1,4]
    l1.sort()
    return l1

def test_list_sort_reversed():
    cdef list l1
    l1 = [2,3,1,4]
    l1.sort(reversed=True)
    return l1

def test_list_reverse():
    """
    >>> test_list_reverse()
    [1, 2, 3, 4]
    """
    cdef list l1
    l1 = [4,3,2,1]
    l1.reverse()
    return l1


@cython.test_assert_path_exists(
    '//SimpleCallNode//AttributeNode[@entry.cname = "__Pyx_PyList_Append"]',
)
def test_list_append():
    """
    >>> test_list_append()
    [1, 2, 3, 4]
    """
    cdef list l1 = [1,2]
    l1.append(3)
    l1.append(4)
    return l1


@cython.test_assert_path_exists(
    '//SimpleCallNode//NameNode[@entry.cname = "__Pyx_PyList_Append"]',
)
def test_list_append_unbound():
    """
    >>> test_list_append_unbound()
    [1, 2, 3, 4]
    """
    cdef list l1 = [1,2]
    list.append(l1, 3)
    list.append(l1, 4)
    return l1


@cython.test_assert_path_exists(
    '//SimpleCallNode//NameNode[@entry.cname = "__Pyx_PyList_Append"]',
)
def test_list_append_unbound_assigned():
    """
    >>> test_list_append_unbound_assigned()
    [1, 2, 3, 4]
    """
    append = list.append
    cdef list l1 = [1,2]
    append(l1, 3)
    append(l1, 4)
    return l1


def test_list_append_insert():
    """
    >>> test_list_append_insert()
    ['first', 'second']
    """
    cdef list l = []
    l.append("second")
    l.insert(0, "first")
    return l

def test_list_pop():
    """
    >>> test_list_pop()
    (2, [1])
    """
    cdef list l1
    l1 = [1,2]
    two = l1.pop()
    return two, l1

def test_list_pop0():
    """
    >>> test_list_pop0()
    (1, [2])
    """
    cdef list l1
    l1 = [1,2]
    one = l1.pop(0)
    return one, l1

def test_list_pop_all():
    """
    >>> test_list_pop_all()
    True
    """
    cdef list l1
    l1 = [1,2]
    i = 0
    try:
        l1.pop()
        i = 1
        l1.pop(-1)
        i = 2
        l1.pop(0)
        i = 3
    except IndexError:
        return i == 2
    return False


@cython.test_assert_path_exists(
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_ListComp_Append"]',
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_PyList_Append"]',
)
def test_list_extend(seq=None, x=4):
    """
    >>> test_list_extend()
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    >>> test_list_extend([])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    >>> test_list_extend([1])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]
    >>> test_list_extend([1, 2])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2]
    """
    cdef list l = [1,2,3]
    l.extend([])
    l.extend(())
    l.extend(set())  # not currently optimised (not worth the trouble)
    assert l == [1,2,3]
    assert len(l) == 3
    l.extend([4,x+1,6])
    l.extend([7,8,9,10,11,12,13,14,15,16])
    if seq is not None:
        l.extend(seq)
    return l


@cython.test_assert_path_exists(
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_ListComp_Append"]',
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_PyList_Append"]',
)
def test_list_extend_unbound(seq=None, x=4):
    """
    >>> test_list_extend_unbound()
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    >>> test_list_extend_unbound([])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    >>> test_list_extend_unbound([1])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]
    >>> test_list_extend_unbound([1, 2])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2]
    """
    cdef list l = [1,2,3]
    list.extend(l, [])
    list.extend(l, ())
    try:
        list.extend((), ())
    except TypeError:
        pass
    else:
        assert False, "TypeError not raised!"
    list.extend(l, set())  # not currently optimised (not worth the trouble)
    assert l == [1,2,3]
    assert len(l) == 3
    list.extend(l, [4,x+1,6])
    list.extend(l, [7,8,9,10,11,12,13,14,15,16])
    if seq is not None:
        list.extend(l, seq)
    return l

@cython.test_assert_path_exists(
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_ListComp_Append"]',
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_PyList_Append"]',
)
def test_list_extend_sideeffect(seq=None, exc=False):
    """
    >>> test_list_extend_sideeffect()
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], [4, 6, 7, 8])
    >>> test_list_extend_sideeffect([])
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], [4, 6, 7, 8])
    >>> test_list_extend_sideeffect([], exc=True)
    ([1, 2, 3, 10, 11, 12, 13, 14, 15, 16], [4, 7, 8])
    >>> test_list_extend_sideeffect([1])
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], [4, 6, 7, 8])
    >>> test_list_extend_sideeffect([1], exc=True)
    ([1, 2, 3, 10, 11, 12, 13, 14, 15, 16, 1], [4, 7, 8])
    >>> test_list_extend_sideeffect([1, 2])
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2], [4, 6, 7, 8])
    """
    calls = []
    def sideeffect(value):
        calls.append(value)
        return value
    def fail(value):
        if exc:
            raise TypeError("HUHU")
        return value

    cdef list l = [1,2,3]
    l.extend([])
    l.extend(())
    l.extend(set())  # not currently optimised (not worth the trouble)
    assert l == [1,2,3]
    assert len(l) == 3

    # Must first build all items, then append them in order.
    # If building one value fails, none of them must be appended.
    try:
        l.extend([sideeffect(4), fail(5), sideeffect(6)])
    except TypeError as e:
        assert exc
        assert "HUHU" in str(e)
    else:
        assert not exc

    try:
        l.extend([sideeffect(7), sideeffect(8), fail(9)])
    except TypeError as e:
        assert exc
        assert "HUHU" in str(e)
    else:
        assert not exc

    l.extend([10,11,12,13,14,15,16])
    if seq is not None:
        l.extend(seq)
    return l, calls


def test_none_list_extend(list l):
    """
    >>> test_none_list_extend([])
    [1, 2, 3]
    >>> test_none_list_extend([0, 0, 0])
    [0, 0, 0, 1, 2, 3]
    >>> test_none_list_extend(None)
    123
    """
    try:
        l.extend([1,2,3])
    except AttributeError:
        return 123
    return l