@@ -1387,21 +1387,28 @@ nop_out(cfg_instr **instrs, int size)
13871387 }
13881388}
13891389
1390- /* Does not steal reference to "newconst" */
1391- static bool
1390+ /* Does not steal reference to "newconst".
1391+ Return 1 if changed instruction to LOAD_SMALL_INT.
1392+ Return 0 if could not change instruction to LOAD_SMALL_INT.
1393+ Return -1 on error.
1394+ */
1395+ static int
13921396maybe_instr_make_load_smallint (cfg_instr * instr , PyObject * newconst ,
13931397 PyObject * consts , PyObject * const_cache )
13941398{
13951399 if (PyLong_CheckExact (newconst )) {
13961400 int overflow ;
13971401 long val = PyLong_AsLongAndOverflow (newconst , & overflow );
1402+ if (val == -1 && PyErr_Occurred ()) {
1403+ return -1 ;
1404+ }
13981405 if (!overflow && _PY_IS_SMALL_INT (val )) {
13991406 assert (_Py_IsImmortal (newconst ));
14001407 INSTR_SET_OP1 (instr , LOAD_SMALL_INT , (int )val );
1401- return true ;
1408+ return 1 ;
14021409 }
14031410 }
1404- return false ;
1411+ return 0 ;
14051412}
14061413
14071414
@@ -1410,9 +1417,9 @@ static int
14101417instr_make_load_const (cfg_instr * instr , PyObject * newconst ,
14111418 PyObject * consts , PyObject * const_cache )
14121419{
1413- if ( maybe_instr_make_load_smallint (instr , newconst , consts , const_cache )) {
1414- assert ( instr -> i_opcode == LOAD_SMALL_INT );
1415- return SUCCESS ;
1420+ int res = maybe_instr_make_load_smallint (instr , newconst , consts , const_cache );
1421+ if ( res ) {
1422+ return res == -1 ? ERROR : SUCCESS ;
14161423 }
14171424 int oparg = add_const (newconst , consts , const_cache );
14181425 RETURN_IF_ERROR (oparg );
@@ -2054,8 +2061,11 @@ basicblock_optimize_load_const(PyObject *const_cache, basicblock *bb, PyObject *
20542061 cfg_instr * inst = & bb -> b_instr [i ];
20552062 if (inst -> i_opcode == LOAD_CONST ) {
20562063 PyObject * constant = get_const_value (inst -> i_opcode , inst -> i_oparg , consts );
2057- ( void ) maybe_instr_make_load_smallint (inst , constant , consts , const_cache );
2064+ int res = maybe_instr_make_load_smallint (inst , constant , consts , const_cache );
20582065 Py_DECREF (constant );
2066+ if (res < 0 ) {
2067+ return ERROR ;
2068+ }
20592069 }
20602070 bool is_copy_of_load_const = (opcode == LOAD_CONST &&
20612071 inst -> i_opcode == COPY &&
0 commit comments