Commit 34d5d939 authored by sizeoftank's avatar sizeoftank

Add list tp_as_mapping for issue #1197

parent 4d4d78ae
......@@ -397,7 +397,9 @@ int list_ass_ext_slice(BoxedList* self, PyObject* item, PyObject* value) {
return -1;
}
RELEASE_ASSERT(step != 1, "should have handled this elsewhere");
if (step == 1) {
return list_ass_slice((PyListObject*)self, start, stop, value);
}
/* Make sure s[5:2] = [..] inserts at the right place:
before 5, not before 2. */
......@@ -517,6 +519,23 @@ int list_ass_ext_slice(BoxedList* self, PyObject* item, PyObject* value) {
}
}
int list_ass_subscript(BoxedList* self, PyObject* slice, PyObject* value) {
assert(PyList_Check(self));
if (PyIndex_Check(slice)) {
Py_ssize_t i = PyNumber_AsSsize_t(slice, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return -1;
if (i < 0)
i += PyList_GET_SIZE(self);
return list_ass_item((PyListObject*)self, i, value);
} else if (slice->cls == slice_cls) {
return list_ass_ext_slice(self, slice, value);
} else {
PyErr_Format(PyExc_TypeError, "list indices must be integers, not %.200s", slice->cls->tp_name);
return -1;
}
}
static inline void listSetitemSliceInt64(BoxedList* self, i64 start, i64 stop, i64 step, Box* v) {
RELEASE_ASSERT(step == 1, "step sizes must be 1 in this code path");
......@@ -1533,6 +1552,10 @@ void setupList() {
list_iterator_cls->tp_iternext = listiter_next;
list_iterator_cls->tp_iter = PyObject_SelfIter;
list_cls->tp_as_mapping->mp_length = (lenfunc)list_length;
list_cls->tp_as_mapping->mp_subscript = (binaryfunc)listGetitem<CAPI>;
list_cls->tp_as_mapping->mp_ass_subscript = (objobjargproc)list_ass_subscript;
list_reverse_iterator_cls->giveAttr("__name__", boxString("listreverseiterator"));
hasnext = FunctionMetadata::create((void*)listreviterHasnextUnboxed, BOOL, 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment