2
# tag: sequence_unpacking
12
cdef class ItCount(object):
15
def __init__(self, values):
16
self.values = iter(values)
22
return next(self.values)
24
def kunterbunt(obj1, obj2, obj3, obj4, obj5):
26
>>> kunterbunt(1, (2,), (3,4,5), (6,(7,(8,9))), 0)
27
(8, 9, (8, 9), (6, (7, (8, 9))), 0)
30
obj1, obj2 = obj2 + obj2
31
obj1, obj2, obj3 = obj3
32
obj1, (obj2, obj3) = obj4
34
return obj1, obj2, obj3, obj4, obj5
36
def unpack_tuple(tuple it):
38
>>> unpack_tuple((1,2,3))
41
>>> a,b,c = None # doctest: +ELLIPSIS
42
Traceback (most recent call last):
44
>>> unpack_tuple(None) # doctest: +ELLIPSIS
45
Traceback (most recent call last):
51
def unpack_list(list it):
53
>>> unpack_list([1,2,3])
56
>>> a,b,c = None # doctest: +ELLIPSIS
57
Traceback (most recent call last):
59
>>> unpack_list(None) # doctest: +ELLIPSIS
60
Traceback (most recent call last):
66
def unpack_to_itself(it):
72
>>> unpack_to_itself([1,2])
74
>>> unpack_to_itself((1,2))
76
>>> unpack_to_itself(_it(2))
78
>>> unpack_to_itself((1,2,3))
79
Traceback (most recent call last):
80
ValueError: too many values to unpack (expected 2)
81
>>> unpack_to_itself(_it(3))
82
Traceback (most recent call last):
83
ValueError: too many values to unpack (expected 2)
88
def unpack_partial(it):
93
... except ValueError: pass
94
... else: print("DID NOT FAIL!")
97
>>> unpack_partial([1,2])
99
>>> unpack_partial((1,2))
101
>>> unpack_partial(_it(2))
104
>>> it = ItCount([1,2])
107
... except ValueError: pass
108
... else: print("DID NOT FAIL!")
113
>>> it = ItCount([1,2])
114
>>> unpack_partial(it)
126
def unpack_fail_assignment(it):
128
>>> it = ItCount([1, 2, 3])
130
>>> try: a, b[0], c = it
131
... except TypeError: pass
136
>>> it = ItCount([1, 2, 3])
137
>>> unpack_fail_assignment(it)
150
def unpack_partial_typed(it):
152
>>> unpack_partial_typed([1, 2, 'abc'])
154
>>> unpack_partial_typed((1, 'abc', 3))
156
>>> unpack_partial_typed(set([1, 'abc', 3]))
159
>>> it = ItCount([1, 'abc', 3])
160
>>> unpack_partial_typed(it)
175
>>> unpack_typed((1, 2.0, [1]))
177
>>> unpack_typed([1, 2.0, [1]])
179
>>> it = ItCount([1, 2.0, [1]])
185
>>> try: unpack_typed((1, None, [1]))
186
... except TypeError: pass
187
>>> try: unpack_typed([1, None, [1]])
188
... except TypeError: pass
189
>>> it = ItCount([1, None, [1]])
190
>>> try: unpack_typed(it)
191
... except TypeError: pass
195
>>> unpack_typed((1, 2.0, (1,)))
196
Traceback (most recent call last):
197
TypeError: Expected list, got tuple
198
>>> it = ItCount([1, 2.0, (1,)])
200
Traceback (most recent call last):
201
TypeError: Expected list, got tuple
211
def failure_too_many(it):
213
>>> try: a,b,c = [1,2,3,4]
214
... except ValueError: pass
215
>>> failure_too_many([1,2,3,4])
216
Traceback (most recent call last):
217
ValueError: too many values to unpack (expected 3)
219
>>> try: a,b,c = [1,2,3,4]
220
... except ValueError: pass
221
>>> failure_too_many((1,2,3,4))
222
Traceback (most recent call last):
223
ValueError: too many values to unpack (expected 3)
225
>>> a,b,c = set([1,2,3,4]) # doctest: +ELLIPSIS
226
Traceback (most recent call last):
227
ValueError: too many values to unpack...
228
>>> failure_too_many(set([1,2,3,4]))
229
Traceback (most recent call last):
230
ValueError: too many values to unpack (expected 3)
232
>>> a,b,c = _it(4) # doctest: +ELLIPSIS
233
Traceback (most recent call last):
234
ValueError: too many values to unpack...
235
>>> failure_too_many(_it(4))
236
Traceback (most recent call last):
237
ValueError: too many values to unpack (expected 3)
243
def failure_too_few(it):
245
>>> try: a,b,c = [1,2]
246
... except ValueError: pass
247
>>> failure_too_few([1,2])
248
Traceback (most recent call last):
249
ValueError: need more than 2 values to unpack
251
>>> try: a,b,c = (1,2)
252
... except ValueError: pass
253
>>> failure_too_few((1,2))
254
Traceback (most recent call last):
255
ValueError: need more than 2 values to unpack
257
>>> try: a,b,c = set([1,2])
258
... except ValueError: pass
259
... else: print("DID NOT FAIL!")
260
>>> failure_too_few(set([1,2]))
261
Traceback (most recent call last):
262
ValueError: need more than 2 values to unpack
264
>>> try: a,b,c = _it(2)
265
... except ValueError: pass
266
... else: print("DID NOT FAIL!")
267
>>> failure_too_few(_it(2))
268
Traceback (most recent call last):
269
ValueError: need more than 2 values to unpack
278
raise ValueError("huhu")
280
def failure_while_unpacking(it):
282
>>> a,b,c = _it_failure(0)
283
Traceback (most recent call last):
285
>>> failure_while_unpacking(_it_failure(0))
286
Traceback (most recent call last):
289
>>> a,b,c = _it_failure(1)
290
Traceback (most recent call last):
292
>>> failure_while_unpacking(_it_failure(1))
293
Traceback (most recent call last):
296
>>> a,b,c = _it_failure(2)
297
Traceback (most recent call last):
299
>>> failure_while_unpacking(_it_failure(2))
300
Traceback (most recent call last):
303
>>> a,b,c = _it_failure(3)
304
Traceback (most recent call last):
306
>>> failure_while_unpacking(_it_failure(3))
307
Traceback (most recent call last):
310
>>> a,b,c = _it_failure(4) # doctest: +ELLIPSIS
311
Traceback (most recent call last):
312
ValueError: too many values to unpack...
313
>>> failure_while_unpacking(_it_failure(4))
314
Traceback (most recent call last):
315
ValueError: too many values to unpack (expected 3)
322
>>> items = range(1,13)
323
>>> unpack_many(items)
324
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
325
>>> unpack_many(iter(items))
326
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
327
>>> unpack_many(list(items))
328
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
329
>>> unpack_many(tuple(items))
330
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
332
a,b,c,d,e,f,g,h,i,j,k,l = it
333
return a,b,c,d,e,f,g,h,i,j,k,l
335
def unpack_many_tuple(tuple it):
337
>>> items = range(1,13)
338
>>> unpack_many_tuple(tuple(items))
339
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
341
a,b,c,d,e,f,g,h,i,j,k,l = it
342
return a,b,c,d,e,f,g,h,i,j,k,l
344
def unpack_many_list(list it):
346
>>> items = range(1,13)
347
>>> unpack_many_list(list(items))
348
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
350
a,b,c,d,e,f,g,h,i,j,k,l = it
351
return a,b,c,d,e,f,g,h,i,j,k,l
353
def unpack_many_int(it):
355
>>> items = range(1,13)
356
>>> unpack_many_int(items)
357
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
358
>>> unpack_many_int(iter(items))
359
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
360
>>> unpack_many_int(list(items))
361
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
362
>>> unpack_many_int(tuple(items))
363
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
368
a,b,c,d,e,f,g,h,i,j,k,l = it
369
return a,b,c,d,e,f,g,h,i,j,k,l
372
@cython.test_fail_if_path_exists('//PyTypeTestNode')
373
def unpack_literal_none_to_builtin_type():
375
>>> unpack_literal_none_to_builtin_type()
376
(None, None, None, None)
379
a, b = c, d = None, None
387
@cython.test_fail_if_path_exists('//PyTypeTestNode')
388
def unpack_literal_none_to_exttype():
390
>>> unpack_literal_none_to_exttype()
391
(None, None, None, None)
394
a, b = c, d = None, None
399
def test_unpack_resultref():
401
>>> test_unpack_resultref() == ((1, set()), 1, set())