@@ -357,6 +357,15 @@ Box* setIOr(BoxedSet* lhs, BoxedSet* rhs) {
357
357
return incref (lhs);
358
358
}
359
359
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
+
360
369
Box* setOr (BoxedSet* lhs, BoxedSet* rhs) {
361
370
RELEASE_ASSERT (PyAnySet_Check (lhs), " " );
362
371
if (!PyAnySet_Check (rhs))
@@ -367,6 +376,15 @@ Box* setOr(BoxedSet* lhs, BoxedSet* rhs) {
367
376
return setIOr (rtn, rhs);
368
377
}
369
378
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
+
370
388
Box* setIAnd (BoxedSet* lhs, BoxedSet* rhs) {
371
389
RELEASE_ASSERT (PyAnySet_Check (lhs), " " );
372
390
if (!PyAnySet_Check (rhs))
@@ -377,6 +395,15 @@ Box* setIAnd(BoxedSet* lhs, BoxedSet* rhs) {
377
395
return incref (lhs);
378
396
}
379
397
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
+
380
407
Box* setAnd (BoxedSet* lhs, BoxedSet* rhs) {
381
408
RELEASE_ASSERT (PyAnySet_Check (lhs), " " );
382
409
if (!PyAnySet_Check (rhs))
@@ -385,6 +412,15 @@ Box* setAnd(BoxedSet* lhs, BoxedSet* rhs) {
385
412
return setIntersection2 (lhs, rhs);
386
413
}
387
414
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
+
388
424
Box* setISub (BoxedSet* lhs, BoxedSet* rhs) {
389
425
RELEASE_ASSERT (PyAnySet_Check (lhs), " " );
390
426
if (!PyAnySet_Check (rhs))
@@ -397,6 +433,15 @@ Box* setISub(BoxedSet* lhs, BoxedSet* rhs) {
397
433
return incref (lhs);
398
434
}
399
435
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
+
400
445
Box* setSub (BoxedSet* lhs, BoxedSet* rhs) {
401
446
RELEASE_ASSERT (PyAnySet_Check (lhs), " " );
402
447
if (!PyAnySet_Check (rhs))
@@ -407,6 +452,15 @@ Box* setSub(BoxedSet* lhs, BoxedSet* rhs) {
407
452
return setISub (rtn, rhs);
408
453
}
409
454
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
+
410
464
Box* setIXor (BoxedSet* lhs, BoxedSet* rhs) {
411
465
RELEASE_ASSERT (PyAnySet_Check (lhs), " " );
412
466
if (!PyAnySet_Check (rhs))
@@ -427,6 +481,15 @@ Box* setXor(BoxedSet* lhs, BoxedSet* rhs) {
427
481
return setIXor (rtn, rhs);
428
482
}
429
483
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
+
430
493
Box* setIter (BoxedSet* self) noexcept {
431
494
RELEASE_ASSERT (PyAnySet_Check (self), " " );
432
495
return new BoxedSetIterator (self);
@@ -437,6 +500,10 @@ Box* setLen(BoxedSet* self) {
437
500
return boxInt (self->s .size ());
438
501
}
439
502
503
+ static Py_ssize_t set_length (Box* self) noexcept {
504
+ return static_cast <BoxedSet*>(self)->s .size ();
505
+ }
506
+
440
507
Box* setAdd (BoxedSet* self, Box* v) {
441
508
RELEASE_ASSERT (isSubclass (self->cls , set_cls), " %s" , self->cls ->tp_name );
442
509
@@ -741,13 +808,13 @@ Box* setGt(BoxedSet* self, BoxedSet* rhs) {
741
808
return setIssuperset (self, rhs);
742
809
}
743
810
744
- Box* setContains (BoxedSet* self, Box* key) {
811
+ static inline int setContainsShared (BoxedSet* self, Box* key) {
745
812
RELEASE_ASSERT (PyAnySet_Check (self), " " );
746
813
747
814
if (PySet_Check (key)) {
748
815
try {
749
816
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 ();
751
818
} catch (ExcInfo e) {
752
819
if (!e.matches (TypeError))
753
820
throw e;
@@ -756,11 +823,24 @@ Box* setContains(BoxedSet* self, Box* key) {
756
823
757
824
BoxedSet* tmpKey = makeNewSet (frozenset_cls, key);
758
825
AUTO_DECREF (tmpKey);
759
- return boxBool ( self->s .find (tmpKey) != self->s .end () );
826
+ return self->s .find (tmpKey) != self->s .end ();
760
827
}
761
828
}
762
829
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
+ }
764
844
}
765
845
766
846
Box* setRemove (BoxedSet* self, Box* key) {
@@ -1098,6 +1178,17 @@ void setupSet() {
1098
1178
set_cls->freeze ();
1099
1179
frozenset_cls->freeze ();
1100
1180
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
+
1101
1192
frozenset_cls->tp_repr = set_cls->tp_repr = set_repr;
1102
1193
frozenset_cls->tp_iter = set_cls->tp_iter = (decltype (set_cls->tp_iter ))setIter;
1103
1194
}
0 commit comments