list_pop.pyx 3.31 KB
Newer Older
Robert Bradshaw's avatar
Robert Bradshaw committed
1 2 3 4 5 6 7 8 9 10 11 12
cimport cython

class A:
    def pop(self, *args):
        print args
        return None


@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
def simple_pop(L):
    """
Stefan Behnel's avatar
Stefan Behnel committed
13
    >>> L = list(range(10))
Robert Bradshaw's avatar
Robert Bradshaw committed
14 15 16 17 18 19 20 21
    >>> simple_pop(L)
    9
    >>> simple_pop(L)
    8
    >>> L
    [0, 1, 2, 3, 4, 5, 6, 7]
    >>> while L:
    ...    _ = simple_pop(L)
22

Robert Bradshaw's avatar
Robert Bradshaw committed
23 24 25 26 27 28 29 30 31 32 33 34
    >>> L
    []
    >>> simple_pop(L)
    Traceback (most recent call last):
    ...
    IndexError: pop from empty list

    >>> simple_pop(A())
    ()
    """
    return L.pop()

35 36 37 38 39 40 41 42 43 44 45 46 47
@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
def simple_pop_typed(list L):
    """
    >>> L = list(range(10))
    >>> simple_pop_typed(L)
    9
    >>> simple_pop_typed(L)
    8
    >>> L
    [0, 1, 2, 3, 4, 5, 6, 7]
    >>> while L:
    ...    _ = simple_pop_typed(L)
48

49 50 51 52 53 54 55 56 57 58
    >>> L
    []
    >>> simple_pop_typed(L)
    Traceback (most recent call last):
    ...
    IndexError: pop from empty list
    """
    return L.pop()


Robert Bradshaw's avatar
Robert Bradshaw committed
59 60 61 62
@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
def index_pop(L, int i):
    """
Stefan Behnel's avatar
Stefan Behnel committed
63
    >>> L = list(range(10))
Robert Bradshaw's avatar
Robert Bradshaw committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    >>> index_pop(L, 2)
    2
    >>> index_pop(L, -2)
    8
    >>> L
    [0, 1, 3, 4, 5, 6, 7, 9]
    >>> index_pop(L, 100)
    Traceback (most recent call last):
    ...
    IndexError: pop index out of range
    >>> index_pop(L, -100)
    Traceback (most recent call last):
    ...
    IndexError: pop index out of range
78

Robert Bradshaw's avatar
Robert Bradshaw committed
79 80
    >>> while L:
    ...    _ = index_pop(L, 0)
81

Robert Bradshaw's avatar
Robert Bradshaw committed
82 83
    >>> L
    []
84

Robert Bradshaw's avatar
Robert Bradshaw committed
85 86 87 88 89 90 91 92 93 94
    >>> index_pop(L, 0)
    Traceback (most recent call last):
    ...
    IndexError: pop from empty list

    >>> index_pop(A(), 3)
    (3,)
    """
    return L.pop(i)

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
def index_pop_typed(list L, int i):
    """
    >>> L = list(range(10))
    >>> index_pop_typed(L, 2)
    2
    >>> index_pop_typed(L, -2)
    8
    >>> L
    [0, 1, 3, 4, 5, 6, 7, 9]
    >>> index_pop_typed(L, 100)
    Traceback (most recent call last):
    ...
    IndexError: pop index out of range
    >>> index_pop_typed(L, -100)
    Traceback (most recent call last):
    ...
    IndexError: pop index out of range
114

115 116
    >>> while L:
    ...    _ = index_pop_typed(L, 0)
117

118 119
    >>> L
    []
120

121 122 123 124 125 126 127
    >>> index_pop_typed(L, 0)
    Traceback (most recent call last):
    ...
    IndexError: pop from empty list
    """
    return L.pop(i)

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
def index_pop_literal(list L):
    """
    >>> L = list(range(10))
    >>> index_pop_literal(L)
    0
    >>> L
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    >>> while L:
    ...    _ = index_pop_literal(L)

    >>> L
    []

    >>> index_pop_literal(L)
    Traceback (most recent call last):
    ...
    IndexError: pop from empty list
    """
    return L.pop(0)

151

Robert Bradshaw's avatar
Robert Bradshaw committed
152 153 154
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
def crazy_pop(L):
    """
Stefan Behnel's avatar
Stefan Behnel committed
155
    >>> crazy_pop(list(range(10)))    # doctest: +ELLIPSIS
Robert Bradshaw's avatar
Robert Bradshaw committed
156
    Traceback (most recent call last):
Stefan Behnel's avatar
Stefan Behnel committed
157
    TypeError: pop... at most 1 argument...3...
Robert Bradshaw's avatar
Robert Bradshaw committed
158 159 160 161
    >>> crazy_pop(A())
    (1, 2, 3)
    """
    return L.pop(1, 2, 3)