@@ -850,6 +850,10 @@ array_richcompare(PyObject *v, PyObject *w, int op)
850850 return res ;
851851}
852852
853+ static int
854+ array_binop_specialize (PyObject * v , PyObject * w , int oparg ,
855+ _PyBinaryOpSpecializationDescr * * descr );
856+
853857static Py_ssize_t
854858array_length (PyObject * op )
855859{
@@ -2966,6 +2970,7 @@ static PyType_Slot array_slots[] = {
29662970 {Py_tp_new , array_new },
29672971 {Py_tp_traverse , array_tp_traverse },
29682972 {Py_tp_token , Py_TP_USE_SPEC },
2973+ {Py_tp_binop_specialize , array_binop_specialize },
29692974
29702975 /* as sequence */
29712976 {Py_sq_length , array_length },
@@ -2998,6 +3003,61 @@ static PyType_Spec array_spec = {
29983003 .slots = array_slots ,
29993004};
30003005
3006+ static inline int
3007+ array_subscr_guard (PyObject * lhs , PyObject * rhs )
3008+ {
3009+ PyObject * exc = PyErr_GetRaisedException ();
3010+ int ret = PyType_GetBaseByToken (Py_TYPE (lhs ), & array_spec , NULL );
3011+ if (ret < 0 ) {
3012+ if (PyErr_ExceptionMatches (PyExc_TypeError )) {
3013+ PyErr_Clear ();
3014+ ret = 0 ;
3015+ }
3016+ }
3017+ _PyErr_ChainExceptions1 (exc );
3018+ return ret ;
3019+ }
3020+
3021+ static PyObject *
3022+ array_subscr_action (PyObject * lhs , PyObject * rhs )
3023+ {
3024+ return array_subscr (lhs , rhs );
3025+ }
3026+
3027+ static int
3028+ array_binop_specialize (PyObject * v , PyObject * w , int oparg ,
3029+ _PyBinaryOpSpecializationDescr * * descr )
3030+ {
3031+ array_state * state = find_array_state_by_type (Py_TYPE (v ));
3032+
3033+ if (!array_Check (v , state )) {
3034+ return 0 ;
3035+ }
3036+
3037+ * descr = NULL ;
3038+ switch (oparg ) {
3039+ case NB_SUBSCR :
3040+ if (array_subscr_guard (v , w )) {
3041+ * descr = (_PyBinaryOpSpecializationDescr * )PyMem_Malloc (
3042+ sizeof (_PyBinaryOpSpecializationDescr ));
3043+ if (* descr == NULL ) {
3044+ PyErr_NoMemory ();
3045+ return -1 ;
3046+ }
3047+ * * descr = (_PyBinaryOpSpecializationDescr ) {
3048+ .oparg = oparg ,
3049+ .guard = array_subscr_guard ,
3050+ .action = array_subscr_action ,
3051+ };
3052+ return 1 ;
3053+ }
3054+ break ;
3055+ }
3056+
3057+ return 0 ;
3058+ }
3059+
3060+
30013061/*********************** Array Iterator **************************/
30023062
30033063/*[clinic input]
@@ -3204,41 +3264,6 @@ do { \
32043264 state->str_ ## string = tmp; \
32053265} while (0)
32063266
3207- static inline int
3208- array_subscr_guard (PyObject * lhs , PyObject * rhs )
3209- {
3210- PyObject * exc = PyErr_GetRaisedException ();
3211- int ret = PyType_GetBaseByToken (Py_TYPE (lhs ), & array_spec , NULL );
3212- if (ret < 0 ) {
3213- if (PyErr_ExceptionMatches (PyExc_TypeError )) {
3214- PyErr_Clear ();
3215- ret = 0 ;
3216- }
3217- }
3218- _PyErr_ChainExceptions1 (exc );
3219- return ret ;
3220- }
3221-
3222- static PyObject *
3223- array_subscr_action (PyObject * lhs , PyObject * rhs )
3224- {
3225- return array_subscr (lhs , rhs );
3226- }
3227-
3228- static int
3229- array_register_specializations (void )
3230- {
3231- _PyBinaryOpSpecializationDescr descr = {
3232- .oparg = NB_SUBSCR ,
3233- .guard = array_subscr_guard ,
3234- .action = array_subscr_action ,
3235- };
3236- if (_Py_Specialize_AddBinaryOpExtention (& descr ) < 0 ) {
3237- return -1 ;
3238- }
3239- return 0 ;
3240- }
3241-
32423267static int
32433268array_modexec (PyObject * m )
32443269{
@@ -3278,10 +3303,6 @@ array_modexec(PyObject *m)
32783303 }
32793304 Py_DECREF (res );
32803305
3281- if (array_register_specializations () < 0 ) {
3282- return -1 ;
3283- }
3284-
32853306 if (PyModule_AddType (m , state -> ArrayType ) < 0 ) {
32863307 return -1 ;
32873308 }
0 commit comments