@@ -1186,7 +1186,7 @@ dummy_func(
11861186 }
11871187
11881188 macro (BINARY_OP_SUBSCR_STR_INT ) =
1189- _GUARD_TOS_NON_NEGATIVE_COMPACT_INT + _GUARD_NOS_COMPACT_ASCII +
1189+ _GUARD_TOS_INT + _GUARD_NOS_COMPACT_ASCII +
11901190 unused /5 + _BINARY_OP_SUBSCR_STR_INT + _POP_TOP_INT + _POP_TOP_UNICODE ;
11911191
11921192 op (_BINARY_OP_SUBSCR_STR_INT , (str_st , sub_st -- res , s , i )) {
@@ -1195,8 +1195,12 @@ dummy_func(
11951195
11961196 assert (PyLong_CheckExact (sub ));
11971197 assert (PyUnicode_CheckExact (str ));
1198- Py_ssize_t index = ((PyLongObject * )sub )-> long_value .ob_digit [0 ];
1199- EXIT_IF (PyUnicode_GET_LENGTH (str ) <= index );
1198+ Py_ssize_t index = _PyLong_CompactValue ((PyLongObject * )sub );
1199+ Py_ssize_t len = PyUnicode_GET_LENGTH (str );
1200+ if (index < 0 ) {
1201+ index += len ;
1202+ }
1203+ EXIT_IF (index < 0 || len <= index );
12001204 uint8_t c = PyUnicode_1BYTE_DATA (str )[index ];
12011205 assert (c < 128 );
12021206 STAT_INC (BINARY_OP , hit );
@@ -1208,7 +1212,7 @@ dummy_func(
12081212 }
12091213
12101214 macro (BINARY_OP_SUBSCR_USTR_INT ) =
1211- _GUARD_TOS_NON_NEGATIVE_COMPACT_INT + _GUARD_NOS_UNICODE +
1215+ _GUARD_TOS_INT + _GUARD_NOS_UNICODE +
12121216 unused /5 + _BINARY_OP_SUBSCR_USTR_INT + _POP_TOP_INT + _POP_TOP_UNICODE ;
12131217
12141218 op (_BINARY_OP_SUBSCR_USTR_INT , (str_st , sub_st -- res , s , i )) {
@@ -1217,8 +1221,12 @@ dummy_func(
12171221
12181222 assert (PyLong_CheckExact (sub ));
12191223 assert (PyUnicode_CheckExact (str ));
1220- Py_ssize_t index = ((PyLongObject * )sub )-> long_value .ob_digit [0 ];
1221- EXIT_IF (PyUnicode_GET_LENGTH (str ) <= index );
1224+ Py_ssize_t index = _PyLong_CompactValue ((PyLongObject * )sub );
1225+ Py_ssize_t len = PyUnicode_GET_LENGTH (str );
1226+ if (index < 0 ) {
1227+ index += len ;
1228+ }
1229+ EXIT_IF (index < 0 || len <= index );
12221230 // Specialize for reading an ASCII character from any string:
12231231 Py_UCS4 c = PyUnicode_READ_CHAR (str , index );
12241232 EXIT_IF (Py_ARRAY_LENGTH (_Py_SINGLETON (strings ).ascii ) <= c );
@@ -1241,7 +1249,7 @@ dummy_func(
12411249 }
12421250
12431251 macro (BINARY_OP_SUBSCR_TUPLE_INT ) =
1244- _GUARD_TOS_NON_NEGATIVE_COMPACT_INT +
1252+ _GUARD_TOS_INT +
12451253 _GUARD_NOS_TUPLE +
12461254 _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS +
12471255 unused /5 +
@@ -1257,9 +1265,12 @@ dummy_func(
12571265 assert (PyLong_CheckExact (sub ));
12581266 assert (PyTuple_CheckExact (tuple ));
12591267
1260- // Deopt unless sub < PyTuple_Size(list)
1261- Py_ssize_t index = ((PyLongObject * )sub )-> long_value .ob_digit [0 ];
1262- EXIT_IF (index >= PyTuple_GET_SIZE (tuple ));
1268+ Py_ssize_t index = _PyLong_CompactValue ((PyLongObject * )sub );
1269+ Py_ssize_t len = PyTuple_GET_SIZE (tuple );
1270+ if (index < 0 ) {
1271+ index += len ;
1272+ }
1273+ EXIT_IF (index < 0 || index >= len );
12631274 }
12641275
12651276 op (_BINARY_OP_SUBSCR_TUPLE_INT , (tuple_st , sub_st -- res , ts , ss )) {
@@ -1270,7 +1281,10 @@ dummy_func(
12701281 assert (PyTuple_CheckExact (tuple ));
12711282
12721283 STAT_INC (BINARY_OP , hit );
1273- Py_ssize_t index = ((PyLongObject * )sub )-> long_value .ob_digit [0 ];
1284+ Py_ssize_t index = _PyLong_CompactValue ((PyLongObject * )sub );
1285+ if (index < 0 ) {
1286+ index += PyTuple_GET_SIZE (tuple );
1287+ }
12741288 PyObject * res_o = PyTuple_GET_ITEM (tuple , index );
12751289 assert (res_o != NULL );
12761290 res = PyStackRef_FromPyObjectNew (res_o );
@@ -1413,13 +1427,13 @@ dummy_func(
14131427
14141428 op (_GUARD_TOS_NON_NEGATIVE_COMPACT_INT , (value -- value )) {
14151429 PyObject * value_o = PyStackRef_AsPyObjectBorrow (value );
1416- EXIT_IF (! PyLong_CheckExact (value_o ));
1430+ assert ( PyLong_CheckExact (value_o ));
14171431 EXIT_IF (!_PyLong_IsNonNegativeCompact ((PyLongObject * )value_o ));
14181432 }
14191433
14201434 macro (STORE_SUBSCR_LIST_INT ) =
1421- _GUARD_TOS_NON_NEGATIVE_COMPACT_INT + _GUARD_NOS_LIST +
1422- unused / 1 + _STORE_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP ;
1435+ _GUARD_TOS_INT + _GUARD_NOS_LIST + unused / 1 +
1436+ _STORE_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP ;
14231437
14241438 op (_STORE_SUBSCR_LIST_INT , (value , list_st , sub_st -- ls , ss )) {
14251439 PyObject * sub = PyStackRef_AsPyObjectBorrow (sub_st );
@@ -1431,7 +1445,10 @@ dummy_func(
14311445 Py_ssize_t index = _PyLong_CompactValue ((PyLongObject * )sub );
14321446 DEOPT_IF (!LOCK_OBJECT (list ));
14331447 Py_ssize_t len = PyList_GET_SIZE (list );
1434- if (index >= len ) {
1448+ if (index < 0 ) {
1449+ index += len ;
1450+ }
1451+ if (index < 0 || index >= len ) {
14351452 UNLOCK_OBJECT (list );
14361453 DEOPT_IF (true);
14371454 }
0 commit comments