@@ -566,109 +566,6 @@ extern "C" Box* intTruediv(BoxedInt* lhs, Box* rhs) {
566566 }
567567}
568568
569- extern " C" Box* intEqInt (BoxedInt* lhs, BoxedInt* rhs) {
570- assert (isSubclass (lhs->cls , int_cls));
571- assert (isSubclass (rhs->cls , int_cls));
572- return boxBool (lhs->n == rhs->n );
573- }
574-
575- extern " C" Box* intEq (BoxedInt* lhs, Box* rhs) {
576- if (!isSubclass (lhs->cls , int_cls))
577- raiseExcHelper (TypeError, " descriptor '__eq__' requires a 'int' object but received a '%s'" , getTypeName (lhs));
578-
579- if (isSubclass (rhs->cls , int_cls)) {
580- BoxedInt* rhs_int = static_cast <BoxedInt*>(rhs);
581- return boxBool (lhs->n == rhs_int->n );
582- } else {
583- return NotImplemented;
584- }
585- }
586-
587- extern " C" Box* intNeInt (BoxedInt* lhs, BoxedInt* rhs) {
588- assert (isSubclass (lhs->cls , int_cls));
589- assert (isSubclass (rhs->cls , int_cls));
590- return boxBool (lhs->n != rhs->n );
591- }
592-
593- extern " C" Box* intNe (BoxedInt* lhs, Box* rhs) {
594- if (!isSubclass (lhs->cls , int_cls))
595- raiseExcHelper (TypeError, " descriptor '__ne__' requires a 'int' object but received a '%s'" , getTypeName (lhs));
596-
597- if (!isSubclass (rhs->cls , int_cls)) {
598- return NotImplemented;
599- }
600- BoxedInt* rhs_int = static_cast <BoxedInt*>(rhs);
601- return boxBool (lhs->n != rhs_int->n );
602- }
603-
604- extern " C" Box* intLtInt (BoxedInt* lhs, BoxedInt* rhs) {
605- assert (isSubclass (lhs->cls , int_cls));
606- assert (isSubclass (rhs->cls , int_cls));
607- return boxBool (lhs->n < rhs->n );
608- }
609-
610- extern " C" Box* intLt (BoxedInt* lhs, Box* rhs) {
611- if (!isSubclass (lhs->cls , int_cls))
612- raiseExcHelper (TypeError, " descriptor '__lt__' requires a 'int' object but received a '%s'" , getTypeName (lhs));
613-
614- if (!isSubclass (rhs->cls , int_cls)) {
615- return NotImplemented;
616- }
617- BoxedInt* rhs_int = static_cast <BoxedInt*>(rhs);
618- return boxBool (lhs->n < rhs_int->n );
619- }
620-
621- extern " C" Box* intLeInt (BoxedInt* lhs, BoxedInt* rhs) {
622- assert (isSubclass (lhs->cls , int_cls));
623- assert (isSubclass (rhs->cls , int_cls));
624- return boxBool (lhs->n <= rhs->n );
625- }
626-
627- extern " C" Box* intLe (BoxedInt* lhs, Box* rhs) {
628- if (!isSubclass (lhs->cls , int_cls))
629- raiseExcHelper (TypeError, " descriptor '__le__' requires a 'int' object but received a '%s'" , getTypeName (lhs));
630-
631- if (!isSubclass (rhs->cls , int_cls)) {
632- return NotImplemented;
633- }
634- BoxedInt* rhs_int = static_cast <BoxedInt*>(rhs);
635- return boxBool (lhs->n <= rhs_int->n );
636- }
637-
638- extern " C" Box* intGtInt (BoxedInt* lhs, BoxedInt* rhs) {
639- assert (isSubclass (lhs->cls , int_cls));
640- assert (isSubclass (rhs->cls , int_cls));
641- return boxBool (lhs->n > rhs->n );
642- }
643-
644- extern " C" Box* intGt (BoxedInt* lhs, Box* rhs) {
645- if (!isSubclass (lhs->cls , int_cls))
646- raiseExcHelper (TypeError, " descriptor '__gt__' requires a 'int' object but received a '%s'" , getTypeName (lhs));
647-
648- if (!isSubclass (rhs->cls , int_cls)) {
649- return NotImplemented;
650- }
651- BoxedInt* rhs_int = static_cast <BoxedInt*>(rhs);
652- return boxBool (lhs->n > rhs_int->n );
653- }
654-
655- extern " C" Box* intGeInt (BoxedInt* lhs, BoxedInt* rhs) {
656- assert (isSubclass (lhs->cls , int_cls));
657- assert (isSubclass (rhs->cls , int_cls));
658- return boxBool (lhs->n >= rhs->n );
659- }
660-
661- extern " C" Box* intGe (BoxedInt* lhs, Box* rhs) {
662- if (!isSubclass (lhs->cls , int_cls))
663- raiseExcHelper (TypeError, " descriptor '__ge__' requires a 'int' object but received a '%s'" , getTypeName (lhs));
664-
665- if (!isSubclass (rhs->cls , int_cls)) {
666- return NotImplemented;
667- }
668- BoxedInt* rhs_int = static_cast <BoxedInt*>(rhs);
669- return boxBool (lhs->n >= rhs_int->n );
670- }
671-
672569extern " C" Box* intLShiftInt (BoxedInt* lhs, BoxedInt* rhs) {
673570 assert (isSubclass (lhs->cls , int_cls));
674571 assert (isSubclass (rhs->cls , int_cls));
@@ -1119,6 +1016,33 @@ static int64_t int_hash(BoxedInt* o) noexcept {
11191016 return n;
11201017}
11211018
1019+ static PyObject* int_richcompare (PyObject* v, PyObject* w, int op) noexcept {
1020+ if (!PyInt_Check (v) || !PyInt_Check (w)) {
1021+ Py_INCREF (Py_NotImplemented);
1022+ return Py_NotImplemented;
1023+ }
1024+
1025+ int64_t lhs = static_cast <BoxedInt*>(v)->n ;
1026+ int64_t rhs = static_cast <BoxedInt*>(w)->n ;
1027+
1028+ switch (op) {
1029+ case Py_EQ:
1030+ return boxBool (lhs == rhs);
1031+ case Py_NE:
1032+ return boxBool (lhs != rhs);
1033+ case Py_LT:
1034+ return boxBool (lhs < rhs);
1035+ case Py_LE:
1036+ return boxBool (lhs <= rhs);
1037+ case Py_GT:
1038+ return boxBool (lhs > rhs);
1039+ case Py_GE:
1040+ return boxBool (lhs >= rhs);
1041+ default :
1042+ RELEASE_ASSERT (0 , " %d" , op);
1043+ }
1044+ }
1045+
11221046void setupInt () {
11231047 for (int i = 0 ; i < NUM_INTERNED_INTS; i++) {
11241048 interned_ints[i] = new BoxedInt (i);
@@ -1138,12 +1062,8 @@ void setupInt() {
11381062 int_cls->giveAttr (" __pow__" ,
11391063 new BoxedFunction (boxRTFunction ((void *)intPow, UNKNOWN, 3 , 1 , false , false ), { None }));
11401064
1141- _addFuncIntUnknown (" __eq__" , BOXED_BOOL, (void *)intEqInt, (void *)intEq);
1142- _addFuncIntUnknown (" __ne__" , BOXED_BOOL, (void *)intNeInt, (void *)intNe);
1143- _addFuncIntUnknown (" __lt__" , BOXED_BOOL, (void *)intLtInt, (void *)intLt);
1144- _addFuncIntUnknown (" __le__" , BOXED_BOOL, (void *)intLeInt, (void *)intLe);
1145- _addFuncIntUnknown (" __gt__" , BOXED_BOOL, (void *)intGtInt, (void *)intGt);
1146- _addFuncIntUnknown (" __ge__" , BOXED_BOOL, (void *)intGeInt, (void *)intGe);
1065+ // Note: CPython implements int comparisons using tp_compare
1066+ int_cls->tp_richcompare = int_richcompare;
11471067
11481068 _addFuncIntUnknown (" __lshift__" , UNKNOWN, (void *)intLShiftInt, (void *)intLShift);
11491069 _addFuncIntUnknown (" __rshift__" , UNKNOWN, (void *)intRShiftInt, (void *)intRShift);
0 commit comments