@@ -5806,14 +5806,12 @@ compute_code_flags(struct compiler *c)
5806
5806
return flags ;
5807
5807
}
5808
5808
5809
- // Merge *tuple * with constant cache.
5809
+ // Merge *obj * with constant cache.
5810
5810
// Unlike merge_consts_recursive(), this function doesn't work recursively.
5811
5811
static int
5812
- merge_const_tuple (struct compiler * c , PyObject * * tuple )
5812
+ merge_const_one (struct compiler * c , PyObject * * obj )
5813
5813
{
5814
- assert (PyTuple_CheckExact (* tuple ));
5815
-
5816
- PyObject * key = _PyCode_ConstantKey (* tuple );
5814
+ PyObject * key = _PyCode_ConstantKey (* obj );
5817
5815
if (key == NULL ) {
5818
5816
return 0 ;
5819
5817
}
@@ -5824,14 +5822,18 @@ merge_const_tuple(struct compiler *c, PyObject **tuple)
5824
5822
if (t == NULL ) {
5825
5823
return 0 ;
5826
5824
}
5827
- if (t == key ) { // tuple is new constant.
5825
+ if (t == key ) { // obj is new constant.
5828
5826
return 1 ;
5829
5827
}
5830
5828
5831
- PyObject * u = PyTuple_GET_ITEM (t , 1 );
5832
- Py_INCREF (u );
5833
- Py_DECREF (* tuple );
5834
- * tuple = u ;
5829
+ if (PyTuple_CheckExact (t )) {
5830
+ // t is still borrowed reference
5831
+ t = PyTuple_GET_ITEM (t , 1 );
5832
+ }
5833
+
5834
+ Py_INCREF (t );
5835
+ Py_DECREF (* obj );
5836
+ * obj = t ;
5835
5837
return 1 ;
5836
5838
}
5837
5839
@@ -5861,10 +5863,10 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
5861
5863
if (!freevars )
5862
5864
goto error ;
5863
5865
5864
- if (!merge_const_tuple (c , & names ) ||
5865
- !merge_const_tuple (c , & varnames ) ||
5866
- !merge_const_tuple (c , & cellvars ) ||
5867
- !merge_const_tuple (c , & freevars ))
5866
+ if (!merge_const_one (c , & names ) ||
5867
+ !merge_const_one (c , & varnames ) ||
5868
+ !merge_const_one (c , & cellvars ) ||
5869
+ !merge_const_one (c , & freevars ))
5868
5870
{
5869
5871
goto error ;
5870
5872
}
@@ -5881,7 +5883,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
5881
5883
if (consts == NULL ) {
5882
5884
goto error ;
5883
5885
}
5884
- if (!merge_const_tuple (c , & consts )) {
5886
+ if (!merge_const_one (c , & consts )) {
5885
5887
Py_DECREF (consts );
5886
5888
goto error ;
5887
5889
}
@@ -6028,10 +6030,18 @@ assemble(struct compiler *c, int addNone)
6028
6030
goto error ;
6029
6031
}
6030
6032
6031
- if (_PyBytes_Resize (& a .a_lnotab , a .a_lnotab_off ) < 0 )
6033
+ if (_PyBytes_Resize (& a .a_lnotab , a .a_lnotab_off ) < 0 ) {
6032
6034
goto error ;
6033
- if (_PyBytes_Resize (& a .a_bytecode , a .a_offset * sizeof (_Py_CODEUNIT )) < 0 )
6035
+ }
6036
+ if (!merge_const_one (c , & a .a_lnotab )) {
6034
6037
goto error ;
6038
+ }
6039
+ if (_PyBytes_Resize (& a .a_bytecode , a .a_offset * sizeof (_Py_CODEUNIT )) < 0 ) {
6040
+ goto error ;
6041
+ }
6042
+ if (!merge_const_one (c , & a .a_bytecode )) {
6043
+ goto error ;
6044
+ }
6035
6045
6036
6046
co = makecode (c , & a , consts );
6037
6047
error :
0 commit comments