1
2ctypedef fused sequence:
3    list
4    tuple
5    object
6
7def unpack_one(sequence it):
8    """
9    >>> items = [1]
10    >>> unpack_one(items)
11    1
12    >>> unpack_one(iter(items))
13    1
14    >>> unpack_one(list(items))
15    1
16    >>> unpack_one(tuple(items))
17    1
18    """
19    a, = it
20    return a
21
22def unpack_two(sequence it):
23    """
24    >>> items = [1,2]
25    >>> unpack_two(items)
26    (1, 2)
27    >>> unpack_two(iter(items))
28    (1, 2)
29    >>> unpack_two(list(items))
30    (1, 2)
31    >>> unpack_two(tuple(items))
32    (1, 2)
33    """
34    a,b = it
35    return a,b
36
37def unpack_two_int(sequence it):
38    """
39    >>> items = [1,2]
40    >>> unpack_two_int(items)
41    (1, 2)
42    >>> unpack_two_int(iter(items))
43    (1, 2)
44    >>> unpack_two_int(list(items))
45    (1, 2)
46    >>> unpack_two_int(tuple(items))
47    (1, 2)
48
49    >>> items = [1, object()]
50    >>> unpack_two_int(items)  # doctest: +ELLIPSIS
51    Traceback (most recent call last):
52    TypeError: ...int...
53    >>> unpack_two_int(iter(items))  # doctest: +ELLIPSIS
54    Traceback (most recent call last):
55    TypeError: ...int...
56    >>> unpack_two_int(list(items))  # doctest: +ELLIPSIS
57    Traceback (most recent call last):
58    TypeError: ...int...
59    >>> unpack_two_int(tuple(items))  # doctest: +ELLIPSIS
60    Traceback (most recent call last):
61    TypeError: ...int...
62    """
63    cdef int b
64    a,b = it
65    return a,b
66
67def unpack_many(sequence it):
68    """
69    >>> items = range(1,13)
70    >>> unpack_many(items)
71    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
72    >>> unpack_many(iter(items))
73    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
74    >>> unpack_many(list(items))
75    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
76    >>> unpack_many(tuple(items))
77    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
78    """
79    a,b,c,d,e,f,g,h,i,j,k,l = it
80    return a,b,c,d,e,f,g,h,i,j,k,l
81
82def unpack_many_int(sequence it):
83    """
84    >>> items = range(1,13)
85    >>> unpack_many_int(items)
86    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
87    >>> unpack_many_int(iter(items))
88    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
89    >>> unpack_many_int(list(items))
90    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
91    >>> unpack_many_int(tuple(items))
92    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
93
94    >>> items = range(1,10)
95    >>> unpack_many_int(items)
96    Traceback (most recent call last):
97    ValueError: need more than 9 values to unpack
98    >>> unpack_many_int(iter(items))
99    Traceback (most recent call last):
100    ValueError: need more than 9 values to unpack
101    >>> unpack_many_int(list(items))
102    Traceback (most recent call last):
103    ValueError: need more than 9 values to unpack
104    >>> unpack_many_int(tuple(items))
105    Traceback (most recent call last):
106    ValueError: need more than 9 values to unpack
107    """
108    cdef int b
109    cdef long f
110    cdef Py_ssize_t h
111    a,b,c,d,e,f,g,h,i,j,k,l = it
112    return a,b,c,d,e,f,g,h,i,j,k,l
113