Commit ef007829 authored by Kevin Modzelewski's avatar Kevin Modzelewski Committed by GitHub

Merge pull request #1287 from sizeoftank/work_tp_as_mapping_issue1197

Add missing tp_as_mapping
parents 37fa8a96 6a6d74fc
......@@ -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);
......
......@@ -2987,6 +2987,9 @@ void setupStr() {
str_cls->tp_as_sequence->sq_repeat = (ssizeargfunc)string_repeat;
str_cls->tp_as_sequence->sq_slice = str_slice;
str_cls->tp_as_mapping->mp_length = (lenfunc)str_length;
str_cls->tp_as_mapping->mp_subscript = (binaryfunc)strGetitem<CAPI>;
str_cls->tp_new = (newfunc)strNewPacked;
basestring_cls->giveAttr("__doc__",
......
......@@ -811,6 +811,9 @@ void setupTuple() {
tuple_cls->tp_as_sequence->sq_contains = (objobjproc)tuplecontains;
tuple_cls->tp_iter = tupleIter;
tuple_cls->tp_as_mapping->mp_length = (lenfunc)tuplelength;
tuple_cls->tp_as_mapping->mp_subscript = (binaryfunc)tupleGetitem<CAPI>;
FunctionMetadata* hasnext = FunctionMetadata::create((void*)tupleiterHasnextUnboxed, BOOL, 1);
hasnext->addVersion((void*)tupleiterHasnext, BOXED_BOOL);
tuple_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext));
......
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