Skip to content

Commit 22c8495

Browse files
committed
implement tp_* function pointers for set object
1 parent f149857 commit 22c8495

File tree

1 file changed

+95
-4
lines changed

1 file changed

+95
-4
lines changed

src/runtime/set.cpp

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ Box* setIOr(BoxedSet* lhs, BoxedSet* rhs) {
357357
return incref(lhs);
358358
}
359359

360+
static PyObject* set_ior(PySetObject* so, PyObject* other) {
361+
try {
362+
return setIOr((BoxedSet*)so, (BoxedSet*)other);
363+
} catch (ExcInfo e) {
364+
setCAPIException(e);
365+
return NULL;
366+
}
367+
}
368+
360369
Box* setOr(BoxedSet* lhs, BoxedSet* rhs) {
361370
RELEASE_ASSERT(PyAnySet_Check(lhs), "");
362371
if (!PyAnySet_Check(rhs))
@@ -367,6 +376,15 @@ Box* setOr(BoxedSet* lhs, BoxedSet* rhs) {
367376
return setIOr(rtn, rhs);
368377
}
369378

379+
static PyObject* set_or(PySetObject* so, PyObject* other) noexcept {
380+
try {
381+
return setOr((BoxedSet*)so, (BoxedSet*)other);
382+
} catch (ExcInfo e) {
383+
setCAPIException(e);
384+
return NULL;
385+
}
386+
}
387+
370388
Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) {
371389
RELEASE_ASSERT(PyAnySet_Check(lhs), "");
372390
if (!PyAnySet_Check(rhs))
@@ -377,6 +395,15 @@ Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) {
377395
return incref(lhs);
378396
}
379397

398+
static PyObject* set_iand(PyObject* so, PyObject* other) noexcept {
399+
try {
400+
return setIAnd(static_cast<BoxedSet*>(so), static_cast<BoxedSet*>(other));
401+
} catch (ExcInfo e) {
402+
setCAPIException(e);
403+
return NULL;
404+
}
405+
}
406+
380407
Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) {
381408
RELEASE_ASSERT(PyAnySet_Check(lhs), "");
382409
if (!PyAnySet_Check(rhs))
@@ -385,6 +412,15 @@ Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) {
385412
return setIntersection2(lhs, rhs);
386413
}
387414

415+
static PyObject* set_and(PyObject* so, PyObject* other) noexcept {
416+
try {
417+
return setAnd(static_cast<BoxedSet*>(so), static_cast<BoxedSet*>(other));
418+
} catch (ExcInfo e) {
419+
setCAPIException(e);
420+
return NULL;
421+
}
422+
}
423+
388424
Box* setISub(BoxedSet* lhs, BoxedSet* rhs) {
389425
RELEASE_ASSERT(PyAnySet_Check(lhs), "");
390426
if (!PyAnySet_Check(rhs))
@@ -397,6 +433,15 @@ Box* setISub(BoxedSet* lhs, BoxedSet* rhs) {
397433
return incref(lhs);
398434
}
399435

436+
static PyObject* set_isub(PyObject* so, PyObject* other) noexcept {
437+
try {
438+
return setISub(static_cast<BoxedSet*>(so), static_cast<BoxedSet*>(other));
439+
} catch (ExcInfo e) {
440+
setCAPIException(e);
441+
return NULL;
442+
}
443+
}
444+
400445
Box* setSub(BoxedSet* lhs, BoxedSet* rhs) {
401446
RELEASE_ASSERT(PyAnySet_Check(lhs), "");
402447
if (!PyAnySet_Check(rhs))
@@ -407,6 +452,15 @@ Box* setSub(BoxedSet* lhs, BoxedSet* rhs) {
407452
return setISub(rtn, rhs);
408453
}
409454

455+
static PyObject* set_sub(PyObject* so, PyObject* other) noexcept {
456+
try {
457+
return setSub(static_cast<BoxedSet*>(so), static_cast<BoxedSet*>(other));
458+
} catch (ExcInfo e) {
459+
setCAPIException(e);
460+
return NULL;
461+
}
462+
}
463+
410464
Box* setIXor(BoxedSet* lhs, BoxedSet* rhs) {
411465
RELEASE_ASSERT(PyAnySet_Check(lhs), "");
412466
if (!PyAnySet_Check(rhs))
@@ -427,6 +481,15 @@ Box* setXor(BoxedSet* lhs, BoxedSet* rhs) {
427481
return setIXor(rtn, rhs);
428482
}
429483

484+
static PyObject* set_xor(PyObject* so, PyObject* other) noexcept {
485+
try {
486+
return setXor(static_cast<BoxedSet*>(so), static_cast<BoxedSet*>(other));
487+
} catch (ExcInfo e) {
488+
setCAPIException(e);
489+
return NULL;
490+
}
491+
}
492+
430493
Box* setIter(BoxedSet* self) noexcept {
431494
RELEASE_ASSERT(PyAnySet_Check(self), "");
432495
return new BoxedSetIterator(self);
@@ -437,6 +500,10 @@ Box* setLen(BoxedSet* self) {
437500
return boxInt(self->s.size());
438501
}
439502

503+
static Py_ssize_t set_length(Box* self) noexcept {
504+
return static_cast<BoxedSet*>(self)->s.size();
505+
}
506+
440507
Box* setAdd(BoxedSet* self, Box* v) {
441508
RELEASE_ASSERT(isSubclass(self->cls, set_cls), "%s", self->cls->tp_name);
442509

@@ -741,13 +808,13 @@ Box* setGt(BoxedSet* self, BoxedSet* rhs) {
741808
return setIssuperset(self, rhs);
742809
}
743810

744-
Box* setContains(BoxedSet* self, Box* key) {
811+
static inline int setContainsShared(BoxedSet* self, Box* key) {
745812
RELEASE_ASSERT(PyAnySet_Check(self), "");
746813

747814
if (PySet_Check(key)) {
748815
try {
749816
BoxAndHash k_hash(key);
750-
return boxBool(self->s.find(k_hash) != self->s.end());
817+
return self->s.find(k_hash) != self->s.end();
751818
} catch (ExcInfo e) {
752819
if (!e.matches(TypeError))
753820
throw e;
@@ -756,11 +823,24 @@ Box* setContains(BoxedSet* self, Box* key) {
756823

757824
BoxedSet* tmpKey = makeNewSet(frozenset_cls, key);
758825
AUTO_DECREF(tmpKey);
759-
return boxBool(self->s.find(tmpKey) != self->s.end());
826+
return self->s.find(tmpKey) != self->s.end();
760827
}
761828
}
762829

763-
return boxBool(self->s.find(key) != self->s.end());
830+
return self->s.find(key) != self->s.end();
831+
}
832+
833+
Box* setContains(BoxedSet* self, Box* key) {
834+
return boxBool(setContainsShared(self, key));
835+
}
836+
837+
static int set_contains(PyObject* self, PyObject* key) noexcept {
838+
try {
839+
return setContainsShared((BoxedSet*)self, key);
840+
} catch (ExcInfo e) {
841+
setCAPIException(e);
842+
return -1;
843+
}
764844
}
765845

766846
Box* setRemove(BoxedSet* self, Box* key) {
@@ -1098,6 +1178,17 @@ void setupSet() {
10981178
set_cls->freeze();
10991179
frozenset_cls->freeze();
11001180

1181+
frozenset_cls->tp_as_sequence->sq_length = set_cls->tp_as_sequence->sq_length = set_length;
1182+
frozenset_cls->tp_as_sequence->sq_contains = set_cls->tp_as_sequence->sq_contains = (objobjproc)set_contains;
1183+
1184+
set_cls->tp_as_number->nb_and = (binaryfunc)set_and;
1185+
set_cls->tp_as_number->nb_inplace_and = (binaryfunc)set_iand;
1186+
set_cls->tp_as_number->nb_or = (binaryfunc)set_or;
1187+
set_cls->tp_as_number->nb_inplace_or = (binaryfunc)set_ior;
1188+
set_cls->tp_as_number->nb_xor = (binaryfunc)set_xor;
1189+
set_cls->tp_as_number->nb_subtract = (binaryfunc)set_sub;
1190+
set_cls->tp_as_number->nb_inplace_subtract = (binaryfunc)set_isub;
1191+
11011192
frozenset_cls->tp_repr = set_cls->tp_repr = set_repr;
11021193
frozenset_cls->tp_iter = set_cls->tp_iter = (decltype(set_cls->tp_iter))setIter;
11031194
}

0 commit comments

Comments
 (0)