Commit 21ab994e authored by Marius Wachtler's avatar Marius Wachtler

Merge pull request #1198 from undingen/tuple_contains

tuple: implement tp_as_sequence->sq_contains
parents 24f44628 33e1db9e
...@@ -314,17 +314,20 @@ Box* tupleNonzero(BoxedTuple* self) { ...@@ -314,17 +314,20 @@ Box* tupleNonzero(BoxedTuple* self) {
return boxBool(self->size() != 0); return boxBool(self->size() != 0);
} }
Box* tupleContains(BoxedTuple* self, Box* elt) { static int tuplecontains(BoxedTuple* self, Box* elt) noexcept {
int size = self->size();
for (Box* e : *self) { for (Box* e : *self) {
int r = PyObject_RichCompareBool(elt, e, Py_EQ); int r = PyObject_RichCompareBool(elt, e, Py_EQ);
if (r == -1)
throwCAPIException();
if (r) if (r)
Py_RETURN_TRUE; return r;
} }
Py_RETURN_FALSE; return 0;
}
Box* tupleContains(BoxedTuple* self, Box* elt) {
int r = tuplecontains(self, elt);
if (r == -1)
throwCAPIException();
return boxBool(r);
} }
Box* tupleIndex(BoxedTuple* self, Box* elt, Box* startBox, Box** args) { Box* tupleIndex(BoxedTuple* self, Box* elt, Box* startBox, Box** args) {
...@@ -805,6 +808,7 @@ void setupTuple() { ...@@ -805,6 +808,7 @@ void setupTuple() {
tuple_cls->tp_as_sequence->sq_length = (lenfunc)tuplelength; tuple_cls->tp_as_sequence->sq_length = (lenfunc)tuplelength;
tuple_cls->tp_as_sequence->sq_concat = (binaryfunc)tupleconcat; tuple_cls->tp_as_sequence->sq_concat = (binaryfunc)tupleconcat;
tuple_cls->tp_as_sequence->sq_repeat = (ssizeargfunc)tuplerepeat; tuple_cls->tp_as_sequence->sq_repeat = (ssizeargfunc)tuplerepeat;
tuple_cls->tp_as_sequence->sq_contains = (objobjproc)tuplecontains;
tuple_cls->tp_iter = tupleIter; tuple_cls->tp_iter = tupleIter;
FunctionMetadata* hasnext = FunctionMetadata::create((void*)tupleiterHasnextUnboxed, BOOL, 1); FunctionMetadata* hasnext = FunctionMetadata::create((void*)tupleiterHasnextUnboxed, 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