| 1 |
|
| 2 |
from rpython.rlib.objectmodel import always_inline |
| 3 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 4 |
from pypy.module.cpyext.api import (cpython_api, CANNOT_FAIL, Py_ssize_t, |
| 5 |
build_type_checkers_flags) |
| 6 |
from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall |
| 7 |
from pypy.module.cpyext.pyobject import decref, incref, PyObject, make_ref |
| 8 |
from pypy.objspace.std.listobject import W_ListObject |
| 9 |
from pypy.interpreter.error import oefmt |
| 10 |
|
| 11 |
|
| 12 |
PyList_Check, PyList_CheckExact = build_type_checkers_flags("List") |
| 13 |
|
| 14 |
@cpython_api([Py_ssize_t], PyObject) |
| 15 |
def PyList_New(space, len): |
| 16 |
"""Return a new list of length len on success, or NULL on failure. |
| 17 |
|
| 18 |
If length is greater than zero, the returned list object's items are |
| 19 |
set to NULL. Thus you cannot use abstract API functions such as |
| 20 |
PySequence_SetItem() or expose the object to Python code before |
| 21 |
setting all items to a real object with PyList_SetItem(). |
| 22 |
""" |
| 23 |
w_list = space.newlist([None] * len) |
| 24 |
|
| 25 |
return w_list |
| 26 |
|
| 27 |
@always_inline |
| 28 |
def get_list_storage(space, w_list): |
| 29 |
from pypy.module.cpyext.sequence import CPyListStrategy |
| 30 |
w_list.convert_to_cpy_strategy(space) |
| 31 |
return CPyListStrategy.unerase(w_list.lstorage) |
| 32 |
|
| 33 |
@cpython_api([rffi.VOIDP, Py_ssize_t, PyObject], lltype.Void, error=CANNOT_FAIL) |
| 34 |
def PyList_SET_ITEM(space, w_list, index, py_item): |
| 35 |
"""Form of PyList_SetItem() without error checking. This is normally |
| 36 |
only used to fill in new lists where there is no previous content. |
| 37 |
|
| 38 |
"steals" a reference to item, and, unlike PyList_SetItem(), does not |
| 39 |
discard a reference to any item that it being replaced; any reference in |
| 40 |
list at position i will be leaked. |
| 41 |
""" |
| 42 |
assert isinstance(w_list, W_ListObject) |
| 43 |
storage = get_list_storage(space, w_list) |
| 44 |
assert 0 <= index < w_list.length() |
| 45 |
storage._elems[index] = py_item |
| 46 |
|
| 47 |
@cpython_api([PyObject, Py_ssize_t, PyObject], rffi.INT_real, error=-1) |
| 48 |
def PyList_SetItem(space, w_list, index, py_item): |
| 49 |
"""Set the item at index index in list to item. Return 0 on success |
| 50 |
or -1 on failure. |
| 51 |
|
| 52 |
This function "steals" a reference to item and discards a reference to |
| 53 |
an item already in the list at the affected position. |
| 54 |
""" |
| 55 |
if not isinstance(w_list, W_ListObject): |
| 56 |
decref(space, py_item) |
| 57 |
PyErr_BadInternalCall(space) |
| 58 |
if index < 0 or index >= w_list.length(): |
| 59 |
decref(space, py_item) |
| 60 |
raise oefmt(space.w_IndexError, "list assignment index out of range") |
| 61 |
storage = get_list_storage(space, w_list) |
| 62 |
py_old = storage._elems[index] |
| 63 |
storage._elems[index] = py_item |
| 64 |
decref(w_list.space, py_old) |
| 65 |
return 0 |
| 66 |
|
| 67 |
@cpython_api([rffi.VOIDP, Py_ssize_t], PyObject, result_is_ll=True) |
| 68 |
def PyList_GET_ITEM(space, w_list, index): |
| 69 |
assert isinstance(w_list, W_ListObject) |
| 70 |
storage = get_list_storage(space, w_list) |
| 71 |
assert 0 <= index < w_list.length() |
| 72 |
return storage._elems[index] |
| 73 |
|
| 74 |
@cpython_api([PyObject, Py_ssize_t], PyObject, result_is_ll=True) |
| 75 |
def PyList_GetItem(space, w_list, index): |
| 76 |
"""Return the object at position pos in the list pointed to by p. The |
| 77 |
position must be positive, indexing from the end of the list is not |
| 78 |
supported. If pos is out of bounds, return NULL and set an |
| 79 |
IndexError exception.""" |
| 80 |
if not isinstance(w_list, W_ListObject): |
| 81 |
PyErr_BadInternalCall(space) |
| 82 |
if index < 0 or index >= w_list.length(): |
| 83 |
raise oefmt(space.w_IndexError, "list index out of range") |
| 84 |
storage = get_list_storage(space, w_list) |
| 85 |
return storage._elems[index] |
| 86 |
|
| 87 |
|
| 88 |
@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1) |
| 89 |
def PyList_Append(space, w_list, w_item): |
| 90 |
if not isinstance(w_list, W_ListObject): |
| 91 |
PyErr_BadInternalCall(space) |
| 92 |
w_list.append(w_item) |
| 93 |
return 0 |
| 94 |
|
| 95 |
@cpython_api([PyObject, Py_ssize_t, PyObject], rffi.INT_real, error=-1) |
| 96 |
def PyList_Insert(space, w_list, index, w_item): |
| 97 |
"""Insert the item item into list list in front of index index. Return |
| 98 |
0 if successful; return -1 and set an exception if unsuccessful. |
| 99 |
Analogous to list.insert(index, item).""" |
| 100 |
space.call_method(space.w_list, "insert", w_list, space.newint(index), w_item) |
| 101 |
return 0 |
| 102 |
|
| 103 |
@cpython_api([rffi.VOIDP], Py_ssize_t, error=CANNOT_FAIL) |
| 104 |
def PyList_GET_SIZE(space, w_obj): |
| 105 |
"""Macro form of PyList_Size() without error checking. |
| 106 |
""" |
| 107 |
assert isinstance(w_obj, W_ListObject) |
| 108 |
return w_obj.length() |
| 109 |
|
| 110 |
|
| 111 |
@cpython_api([PyObject], Py_ssize_t, error=-1) |
| 112 |
def PyList_Size(space, ref): |
| 113 |
"""Return the length of the list object in list; this is equivalent to |
| 114 |
len(list) on a list object. |
| 115 |
""" |
| 116 |
if not PyList_Check(space, ref): |
| 117 |
raise oefmt(space.w_TypeError, "expected list object") |
| 118 |
return PyList_GET_SIZE(space, ref) |
| 119 |
|
| 120 |
@cpython_api([PyObject], PyObject) |
| 121 |
def PyList_AsTuple(space, w_list): |
| 122 |
"""Return a new tuple object containing the contents of list; equivalent to |
| 123 |
tuple(list).""" |
| 124 |
return space.call_function(space.w_tuple, w_list) |
| 125 |
|
| 126 |
@cpython_api([PyObject], rffi.INT_real, error=-1) |
| 127 |
def PyList_Sort(space, w_list): |
| 128 |
"""Sort the items of list in place. Return 0 on success, -1 on |
| 129 |
failure. This is equivalent to list.sort().""" |
| 130 |
if not isinstance(w_list, W_ListObject): |
| 131 |
PyErr_BadInternalCall(space) |
| 132 |
space.call_method(space.w_list, "sort", w_list) |
| 133 |
return 0 |
| 134 |
|
| 135 |
@cpython_api([PyObject], rffi.INT_real, error=-1) |
| 136 |
def PyList_Reverse(space, w_list): |
| 137 |
"""Reverse the items of list in place. Return 0 on success, -1 on |
| 138 |
failure. This is the equivalent of list.reverse().""" |
| 139 |
if not isinstance(w_list, W_ListObject): |
| 140 |
PyErr_BadInternalCall(space) |
| 141 |
space.call_method(space.w_list, "reverse", w_list) |
| 142 |
return 0 |
| 143 |
|
| 144 |
@cpython_api([PyObject, Py_ssize_t, Py_ssize_t], PyObject) |
| 145 |
def PyList_GetSlice(space, w_list, low, high): |
| 146 |
"""Return a list of the objects in list containing the objects between low |
| 147 |
and high. Return NULL and set an exception if unsuccessful. Analogous |
| 148 |
to list[low:high]. Negative indices, as when slicing from Python, are not |
| 149 |
supported.""" |
| 150 |
w_start = space.newint(low) |
| 151 |
w_stop = space.newint(high) |
| 152 |
return space.getslice(w_list, w_start, w_stop) |
| 153 |
|
| 154 |
@cpython_api([PyObject, Py_ssize_t, Py_ssize_t, PyObject], rffi.INT_real, error=-1) |
| 155 |
def PyList_SetSlice(space, w_list, low, high, w_sequence): |
| 156 |
"""Set the slice of list between low and high to the contents of |
| 157 |
itemlist. Analogous to list[low:high] = itemlist. The itemlist may |
| 158 |
be NULL, indicating the assignment of an empty list (slice deletion). |
| 159 |
Return 0 on success, -1 on failure. Negative indices, as when |
| 160 |
slicing from Python, are not supported.""" |
| 161 |
w_start = space.newint(low) |
| 162 |
w_stop = space.newint(high) |
| 163 |
if w_sequence: |
| 164 |
space.setslice(w_list, w_start, w_stop, w_sequence) |
| 165 |
else: |
| 166 |
space.delslice(w_list, w_start, w_stop) |
| 167 |
return 0 |
| 168 |
|
| 169 |
@cpython_api([PyObject, PyObject], PyObject) |
| 170 |
def _PyList_Extend(space, w_list, w_iterable): |
| 171 |
if not isinstance(w_list, W_ListObject): |
| 172 |
PyErr_BadInternalCall(space) |
| 173 |
return space.call_method(space.w_list, "extend", w_list, w_iterable) |