@@ -1055,15 +1055,14 @@ insert_into_dictkeys(PyDictKeysObject *keys, PyObject *name)
1055
1055
Internal routine to insert a new item into the table.
1056
1056
Used both by the internal resize routine and by the public insert routine.
1057
1057
Returns -1 if an error occurred, or 0 on success.
1058
+ Consumes key and value references.
1058
1059
*/
1059
1060
static int
1060
1061
insertdict (PyDictObject * mp , PyObject * key , Py_hash_t hash , PyObject * value )
1061
1062
{
1062
1063
PyObject * old_value ;
1063
1064
PyDictKeyEntry * ep ;
1064
1065
1065
- Py_INCREF (key );
1066
- Py_INCREF (value );
1067
1066
if (mp -> ma_values != NULL && !PyUnicode_CheckExact (key )) {
1068
1067
if (insertion_resize (mp ) < 0 )
1069
1068
goto Fail ;
@@ -1138,6 +1137,7 @@ insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
1138
1137
}
1139
1138
1140
1139
// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1140
+ // Consumes key and value references.
1141
1141
static int
1142
1142
insert_to_emptydict (PyDictObject * mp , PyObject * key , Py_hash_t hash ,
1143
1143
PyObject * value )
@@ -1146,6 +1146,8 @@ insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1146
1146
1147
1147
PyDictKeysObject * newkeys = new_keys_object (PyDict_LOG_MINSIZE );
1148
1148
if (newkeys == NULL ) {
1149
+ Py_DECREF (key );
1150
+ Py_DECREF (value );
1149
1151
return -1 ;
1150
1152
}
1151
1153
if (!PyUnicode_CheckExact (key )) {
@@ -1155,8 +1157,6 @@ insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1155
1157
mp -> ma_keys = newkeys ;
1156
1158
mp -> ma_values = NULL ;
1157
1159
1158
- Py_INCREF (key );
1159
- Py_INCREF (value );
1160
1160
MAINTAIN_TRACKING (mp , key , value );
1161
1161
1162
1162
size_t hashpos = (size_t )hash & (PyDict_MINSIZE - 1 );
@@ -1529,39 +1529,51 @@ _PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
1529
1529
return value ;
1530
1530
}
1531
1531
1532
- /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1533
- * dictionary if it's merely replacing the value for an existing key.
1534
- * This means that it's safe to loop over a dictionary with PyDict_Next()
1535
- * and occasionally replace a value -- but you can't insert new keys or
1536
- * remove them.
1537
- */
1532
+ /* Consumes references to key and value */
1538
1533
int
1539
- PyDict_SetItem ( PyObject * op , PyObject * key , PyObject * value )
1534
+ _PyDict_SetItem_Take2 ( PyDictObject * mp , PyObject * key , PyObject * value )
1540
1535
{
1541
- PyDictObject * mp ;
1542
- Py_hash_t hash ;
1543
- if (!PyDict_Check (op )) {
1544
- PyErr_BadInternalCall ();
1545
- return -1 ;
1546
- }
1547
1536
assert (key );
1548
1537
assert (value );
1549
- mp = (PyDictObject * )op ;
1538
+ assert (PyDict_Check (mp ));
1539
+ Py_hash_t hash ;
1550
1540
if (!PyUnicode_CheckExact (key ) ||
1551
1541
(hash = ((PyASCIIObject * ) key )-> hash ) == -1 )
1552
1542
{
1553
1543
hash = PyObject_Hash (key );
1554
- if (hash == -1 )
1544
+ if (hash == -1 ) {
1545
+ Py_DECREF (key );
1546
+ Py_DECREF (value );
1555
1547
return -1 ;
1548
+ }
1556
1549
}
1557
-
1558
1550
if (mp -> ma_keys == Py_EMPTY_KEYS ) {
1559
1551
return insert_to_emptydict (mp , key , hash , value );
1560
1552
}
1561
1553
/* insertdict() handles any resizing that might be necessary */
1562
1554
return insertdict (mp , key , hash , value );
1563
1555
}
1564
1556
1557
+ /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1558
+ * dictionary if it's merely replacing the value for an existing key.
1559
+ * This means that it's safe to loop over a dictionary with PyDict_Next()
1560
+ * and occasionally replace a value -- but you can't insert new keys or
1561
+ * remove them.
1562
+ */
1563
+ int
1564
+ PyDict_SetItem (PyObject * op , PyObject * key , PyObject * value )
1565
+ {
1566
+ if (!PyDict_Check (op )) {
1567
+ PyErr_BadInternalCall ();
1568
+ return -1 ;
1569
+ }
1570
+ assert (key );
1571
+ assert (value );
1572
+ Py_INCREF (key );
1573
+ Py_INCREF (value );
1574
+ return _PyDict_SetItem_Take2 ((PyDictObject * )op , key , value );
1575
+ }
1576
+
1565
1577
int
1566
1578
_PyDict_SetItem_KnownHash (PyObject * op , PyObject * key , PyObject * value ,
1567
1579
Py_hash_t hash )
@@ -1577,6 +1589,8 @@ _PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1577
1589
assert (hash != -1 );
1578
1590
mp = (PyDictObject * )op ;
1579
1591
1592
+ Py_INCREF (key );
1593
+ Py_INCREF (value );
1580
1594
if (mp -> ma_keys == Py_EMPTY_KEYS ) {
1581
1595
return insert_to_emptydict (mp , key , hash , value );
1582
1596
}
@@ -1917,6 +1931,8 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1917
1931
}
1918
1932
1919
1933
while (_PyDict_Next (iterable , & pos , & key , & oldvalue , & hash )) {
1934
+ Py_INCREF (key );
1935
+ Py_INCREF (value );
1920
1936
if (insertdict (mp , key , hash , value )) {
1921
1937
Py_DECREF (d );
1922
1938
return NULL ;
@@ -1936,6 +1952,8 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1936
1952
}
1937
1953
1938
1954
while (_PySet_NextEntry (iterable , & pos , & key , & hash )) {
1955
+ Py_INCREF (key );
1956
+ Py_INCREF (value );
1939
1957
if (insertdict (mp , key , hash , value )) {
1940
1958
Py_DECREF (d );
1941
1959
return NULL ;
@@ -2562,11 +2580,16 @@ dict_merge(PyObject *a, PyObject *b, int override)
2562
2580
int err = 0 ;
2563
2581
Py_INCREF (key );
2564
2582
Py_INCREF (value );
2565
- if (override == 1 )
2583
+ if (override == 1 ) {
2584
+ Py_INCREF (key );
2585
+ Py_INCREF (value );
2566
2586
err = insertdict (mp , key , hash , value );
2587
+ }
2567
2588
else {
2568
2589
err = _PyDict_Contains_KnownHash (a , key , hash );
2569
2590
if (err == 0 ) {
2591
+ Py_INCREF (key );
2592
+ Py_INCREF (value );
2570
2593
err = insertdict (mp , key , hash , value );
2571
2594
}
2572
2595
else if (err > 0 ) {
@@ -2967,7 +2990,10 @@ PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
2967
2990
if (hash == -1 )
2968
2991
return NULL ;
2969
2992
}
2993
+
2970
2994
if (mp -> ma_keys == Py_EMPTY_KEYS ) {
2995
+ Py_INCREF (key );
2996
+ Py_INCREF (defaultobj );
2971
2997
if (insert_to_emptydict (mp , key , hash , defaultobj ) < 0 ) {
2972
2998
return NULL ;
2973
2999
}
0 commit comments