@@ -5847,11 +5847,9 @@ compute_code_flags(struct compiler *c)
5847
5847
// Merge *tuple* with constant cache.
5848
5848
// Unlike merge_consts_recursive(), this function doesn't work recursively.
5849
5849
static int
5850
- merge_const_tuple (struct compiler * c , PyObject * * tuple )
5850
+ merge_const_one (struct compiler * c , PyObject * * obj )
5851
5851
{
5852
- assert (PyTuple_CheckExact (* tuple ));
5853
-
5854
- PyObject * key = _PyCode_ConstantKey (* tuple );
5852
+ PyObject * key = _PyCode_ConstantKey (* obj );
5855
5853
if (key == NULL ) {
5856
5854
return 0 ;
5857
5855
}
@@ -5862,14 +5860,18 @@ merge_const_tuple(struct compiler *c, PyObject **tuple)
5862
5860
if (t == NULL ) {
5863
5861
return 0 ;
5864
5862
}
5865
- if (t == key ) { // tuple is new constant.
5863
+ if (t == key ) { // obj is new constant.
5866
5864
return 1 ;
5867
5865
}
5868
5866
5869
- PyObject * u = PyTuple_GET_ITEM (t , 1 );
5870
- Py_INCREF (u );
5871
- Py_DECREF (* tuple );
5872
- * tuple = u ;
5867
+ if (PyTuple_CheckExact (t )) {
5868
+ // t is still borrowed reference
5869
+ t = PyTuple_GET_ITEM (t , 1 );
5870
+ }
5871
+
5872
+ Py_INCREF (t );
5873
+ Py_DECREF (* obj );
5874
+ * obj = t ;
5873
5875
return 1 ;
5874
5876
}
5875
5877
@@ -5899,10 +5901,10 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
5899
5901
if (!freevars )
5900
5902
goto error ;
5901
5903
5902
- if (!merge_const_tuple (c , & names ) ||
5903
- !merge_const_tuple (c , & varnames ) ||
5904
- !merge_const_tuple (c , & cellvars ) ||
5905
- !merge_const_tuple (c , & freevars ))
5904
+ if (!merge_const_one (c , & names ) ||
5905
+ !merge_const_one (c , & varnames ) ||
5906
+ !merge_const_one (c , & cellvars ) ||
5907
+ !merge_const_one (c , & freevars ))
5906
5908
{
5907
5909
goto error ;
5908
5910
}
@@ -5919,7 +5921,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
5919
5921
if (consts == NULL ) {
5920
5922
goto error ;
5921
5923
}
5922
- if (!merge_const_tuple (c , & consts )) {
5924
+ if (!merge_const_one (c , & consts )) {
5923
5925
Py_DECREF (consts );
5924
5926
goto error ;
5925
5927
}
@@ -6041,10 +6043,18 @@ assemble(struct compiler *c, int addNone)
6041
6043
goto error ;
6042
6044
}
6043
6045
6044
- if (_PyBytes_Resize (& a .a_lnotab , a .a_lnotab_off ) < 0 )
6046
+ if (_PyBytes_Resize (& a .a_lnotab , a .a_lnotab_off ) < 0 ) {
6045
6047
goto error ;
6046
- if (_PyBytes_Resize (& a .a_bytecode , a .a_offset * sizeof (_Py_CODEUNIT )) < 0 )
6048
+ }
6049
+ if (!merge_const_one (c , & a .a_lnotab )) {
6047
6050
goto error ;
6051
+ }
6052
+ if (_PyBytes_Resize (& a .a_bytecode , a .a_offset * sizeof (_Py_CODEUNIT )) < 0 ) {
6053
+ goto error ;
6054
+ }
6055
+ if (!merge_const_one (c , & a .a_bytecode )) {
6056
+ goto error ;
6057
+ }
6048
6058
6049
6059
co = makecode (c , & a , consts );
6050
6060
error :
0 commit comments