@@ -1638,14 +1638,14 @@ compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1638
1638
}
1639
1639
1640
1640
#define ADDOP_O (C , OP , O , TYPE ) { \
1641
- assert((OP) != LOAD_CONST ); /* use ADDOP_LOAD_CONST */ \
1641
+ assert(!HAS_CONST (OP)); /* use ADDOP_LOAD_CONST */ \
1642
1642
if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1643
1643
return 0; \
1644
1644
}
1645
1645
1646
1646
/* Same as ADDOP_O, but steals a reference. */
1647
1647
#define ADDOP_N (C , OP , O , TYPE ) { \
1648
- assert((OP) != LOAD_CONST ); /* use ADDOP_LOAD_CONST_NEW */ \
1648
+ assert(!HAS_CONST (OP)); /* use ADDOP_LOAD_CONST_NEW */ \
1649
1649
if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1650
1650
Py_DECREF((O)); \
1651
1651
return 0; \
@@ -7951,6 +7951,24 @@ assemble(struct compiler *c, int addNone)
7951
7951
return co ;
7952
7952
}
7953
7953
7954
+ static PyObject *
7955
+ get_const_value (int opcode , int oparg , PyObject * co_consts )
7956
+ {
7957
+ PyObject * constant = NULL ;
7958
+ assert (HAS_CONST (opcode ));
7959
+ if (opcode == LOAD_CONST ) {
7960
+ constant = PyList_GET_ITEM (co_consts , oparg );
7961
+ }
7962
+
7963
+ if (constant == NULL ) {
7964
+ PyErr_SetString (PyExc_SystemError ,
7965
+ "Internal error: failed to get value of a constant" );
7966
+ return NULL ;
7967
+ }
7968
+ Py_INCREF (constant );
7969
+ return constant ;
7970
+ }
7971
+
7954
7972
/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7955
7973
with LOAD_CONST (c1, c2, ... cn).
7956
7974
The consts table must still be in list form so that the
@@ -7968,7 +7986,7 @@ fold_tuple_on_constants(struct compiler *c,
7968
7986
assert (inst [n ].i_oparg == n );
7969
7987
7970
7988
for (int i = 0 ; i < n ; i ++ ) {
7971
- if (inst [i ].i_opcode != LOAD_CONST ) {
7989
+ if (! HAS_CONST ( inst [i ].i_opcode ) ) {
7972
7990
return 0 ;
7973
7991
}
7974
7992
}
@@ -7979,9 +7997,12 @@ fold_tuple_on_constants(struct compiler *c,
7979
7997
return -1 ;
7980
7998
}
7981
7999
for (int i = 0 ; i < n ; i ++ ) {
8000
+ int op = inst [i ].i_opcode ;
7982
8001
int arg = inst [i ].i_oparg ;
7983
- PyObject * constant = PyList_GET_ITEM (consts , arg );
7984
- Py_INCREF (constant );
8002
+ PyObject * constant = get_const_value (op , arg , consts );
8003
+ if (constant == NULL ) {
8004
+ return -1 ;
8005
+ }
7985
8006
PyTuple_SET_ITEM (newconst , i , constant );
7986
8007
}
7987
8008
if (merge_const_one (c , & newconst ) == 0 ) {
@@ -8107,8 +8128,12 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
8107
8128
switch (nextop ) {
8108
8129
case POP_JUMP_IF_FALSE :
8109
8130
case POP_JUMP_IF_TRUE :
8110
- cnt = PyList_GET_ITEM (consts , oparg );
8131
+ cnt = get_const_value (inst -> i_opcode , oparg , consts );
8132
+ if (cnt == NULL ) {
8133
+ goto error ;
8134
+ }
8111
8135
is_true = PyObject_IsTrue (cnt );
8136
+ Py_DECREF (cnt );
8112
8137
if (is_true == -1 ) {
8113
8138
goto error ;
8114
8139
}
@@ -8124,8 +8149,12 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
8124
8149
break ;
8125
8150
case JUMP_IF_FALSE_OR_POP :
8126
8151
case JUMP_IF_TRUE_OR_POP :
8127
- cnt = PyList_GET_ITEM (consts , oparg );
8152
+ cnt = get_const_value (inst -> i_opcode , oparg , consts );
8153
+ if (cnt == NULL ) {
8154
+ goto error ;
8155
+ }
8128
8156
is_true = PyObject_IsTrue (cnt );
8157
+ Py_DECREF (cnt );
8129
8158
if (is_true == -1 ) {
8130
8159
goto error ;
8131
8160
}
@@ -8310,6 +8339,9 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
8310
8339
fold_rotations (inst - oparg + 1 , oparg );
8311
8340
}
8312
8341
break ;
8342
+ default :
8343
+ /* All HAS_CONST opcodes should be handled with LOAD_CONST */
8344
+ assert (!HAS_CONST (inst -> i_opcode ));
8313
8345
}
8314
8346
}
8315
8347
return 0 ;
0 commit comments