@@ -741,9 +741,11 @@ dict_add_o(PyObject *dict, PyObject *o)
741
741
return arg ;
742
742
}
743
743
744
- // Merge const *o* recursively and return constant key object.
744
+ /* Merge const *o* and return constant key object.
745
+ * If recursive, insert all elements if o is a tuple or frozen set.
746
+ */
745
747
static PyObject *
746
- merge_consts_recursive (PyObject * const_cache , PyObject * o )
748
+ const_cache_insert (PyObject * const_cache , PyObject * o , bool recursive )
747
749
{
748
750
assert (PyDict_CheckExact (const_cache ));
749
751
// None and Ellipsis are immortal objects, and key is the singleton.
@@ -767,14 +769,18 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
767
769
}
768
770
Py_DECREF (t );
769
771
772
+ if (!recursive ) {
773
+ return key ;
774
+ }
775
+
770
776
// We registered o in const_cache.
771
777
// When o is a tuple or frozenset, we want to merge its
772
778
// items too.
773
779
if (PyTuple_CheckExact (o )) {
774
780
Py_ssize_t len = PyTuple_GET_SIZE (o );
775
781
for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
776
782
PyObject * item = PyTuple_GET_ITEM (o , i );
777
- PyObject * u = merge_consts_recursive (const_cache , item );
783
+ PyObject * u = const_cache_insert (const_cache , item , recursive );
778
784
if (u == NULL ) {
779
785
Py_DECREF (key );
780
786
return NULL ;
@@ -816,7 +822,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
816
822
PyObject * item ;
817
823
Py_hash_t hash ;
818
824
while (_PySet_NextEntry (o , & pos , & item , & hash )) {
819
- PyObject * k = merge_consts_recursive (const_cache , item );
825
+ PyObject * k = const_cache_insert (const_cache , item , recursive );
820
826
if (k == NULL ) {
821
827
Py_DECREF (tuple );
822
828
Py_DECREF (key );
@@ -850,6 +856,12 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
850
856
return key ;
851
857
}
852
858
859
+ static PyObject *
860
+ merge_consts_recursive (PyObject * const_cache , PyObject * o )
861
+ {
862
+ return const_cache_insert (const_cache , o , true);
863
+ }
864
+
853
865
static Py_ssize_t
854
866
compiler_add_const (PyObject * const_cache , struct compiler_unit * u , PyObject * o )
855
867
{
@@ -7506,37 +7518,22 @@ compute_code_flags(struct compiler *c)
7506
7518
return flags ;
7507
7519
}
7508
7520
7509
- // Merge *obj* with constant cache.
7510
- // Unlike merge_consts_recursive(), this function doesn't work recursively.
7521
+ // Merge *obj* with constant cache, without recursion.
7511
7522
int
7512
7523
_PyCompile_ConstCacheMergeOne (PyObject * const_cache , PyObject * * obj )
7513
7524
{
7514
- assert (PyDict_CheckExact (const_cache ));
7515
- PyObject * key = _PyCode_ConstantKey (* obj );
7525
+ PyObject * key = const_cache_insert (const_cache , * obj , false);
7516
7526
if (key == NULL ) {
7517
7527
return ERROR ;
7518
7528
}
7519
-
7520
- PyObject * t ;
7521
- int res = PyDict_SetDefaultRef (const_cache , key , key , & t );
7522
- Py_DECREF (key );
7523
- if (res < 0 ) {
7524
- return ERROR ;
7525
- }
7526
- if (res == 0 ) { // inserted: obj is new constant.
7527
- Py_DECREF (t );
7528
- return SUCCESS ;
7529
- }
7530
-
7531
- if (PyTuple_CheckExact (t )) {
7532
- PyObject * item = PyTuple_GET_ITEM (t , 1 );
7529
+ if (PyTuple_CheckExact (key )) {
7530
+ PyObject * item = PyTuple_GET_ITEM (key , 1 );
7533
7531
Py_SETREF (* obj , Py_NewRef (item ));
7534
- Py_DECREF (t );
7532
+ Py_DECREF (key );
7535
7533
}
7536
7534
else {
7537
- Py_SETREF (* obj , t );
7535
+ Py_SETREF (* obj , key );
7538
7536
}
7539
-
7540
7537
return SUCCESS ;
7541
7538
}
7542
7539
0 commit comments