]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDefault`. (#112211)
authorSam Gross <colesbury@gmail.com>
Wed, 7 Feb 2024 18:43:18 +0000 (13:43 -0500)
committerGitHub <noreply@github.com>
Wed, 7 Feb 2024 18:43:18 +0000 (13:43 -0500)
This changes a number of internal usages of `PyDict_SetDefault` to use `PyDict_SetDefaultRef`.

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Modules/_json.c
Modules/posixmodule.c
Modules/pyexpat.c
Objects/typeobject.c
Objects/unicodeobject.c
Python/compile.c

index 24b292ce70e5ebda434e3ab027c7cc541056148a..c55299899e77fe5c444e2b9719830cf047a7b271 100644 (file)
@@ -691,11 +691,10 @@ _parse_object_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ss
             key = scanstring_unicode(pystr, idx + 1, s->strict, &next_idx);
             if (key == NULL)
                 goto bail;
-            memokey = PyDict_SetDefault(memo, key, key);
-            if (memokey == NULL) {
+            if (PyDict_SetDefaultRef(memo, key, key, &memokey) < 0) {
                 goto bail;
             }
-            Py_SETREF(key, Py_NewRef(memokey));
+            Py_SETREF(key, memokey);
             idx = next_idx;
 
             /* skip whitespace between key and : delimiter, read :, skip whitespace */
index 22891135bde0af7edaeaff9dbb0af1dab2fc05b4..e26265fc874ebb4e3936b21d28e6662d4bca8c8e 100644 (file)
@@ -1627,7 +1627,7 @@ convertenviron(void)
             Py_DECREF(d);
             return NULL;
         }
-        if (PyDict_SetDefault(d, k, v) == NULL) {
+        if (PyDict_SetDefaultRef(d, k, v, NULL) < 0) {
             Py_DECREF(v);
             Py_DECREF(k);
             Py_DECREF(d);
index 7c08eda83e66b2abdf48a0fb65dd8ef13f7f9db8..62cd262a7885e93e251ec8a07d6a8425f1ddbc89 100644 (file)
@@ -1615,7 +1615,8 @@ static int init_handler_descrs(pyexpat_state *state)
         if (descr == NULL)
             return -1;
 
-        if (PyDict_SetDefault(state->xml_parse_type->tp_dict, PyDescr_NAME(descr), descr) == NULL) {
+        if (PyDict_SetDefaultRef(state->xml_parse_type->tp_dict,
+                                 PyDescr_NAME(descr), descr, NULL) < 0) {
             Py_DECREF(descr);
             return -1;
         }
index e220d10ce563c29be7af1d419ef561ddd3fab828..c65d0ec2acae5268d0c5209b90407738764d565d 100644 (file)
@@ -6683,7 +6683,7 @@ type_add_method(PyTypeObject *type, PyMethodDef *meth)
     int err;
     PyObject *dict = lookup_tp_dict(type);
     if (!(meth->ml_flags & METH_COEXIST)) {
-        err = PyDict_SetDefault(dict, name, descr) == NULL;
+        err = PyDict_SetDefaultRef(dict, name, descr, NULL) < 0;
     }
     else {
         err = PyDict_SetItem(dict, name, descr) < 0;
@@ -6731,7 +6731,7 @@ type_add_members(PyTypeObject *type)
         if (descr == NULL)
             return -1;
 
-        if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
+        if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) {
             Py_DECREF(descr);
             return -1;
         }
@@ -6756,7 +6756,7 @@ type_add_getset(PyTypeObject *type)
             return -1;
         }
 
-        if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
+        if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) {
             Py_DECREF(descr);
             return -1;
         }
index b236ddba9cdc69fa503b5e105824d013ccd1c91b..0a569a950e88e290b621bf9bef2db14556ea83a1 100644 (file)
@@ -14894,16 +14894,18 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p)
     PyObject *interned = get_interned_dict(interp);
     assert(interned != NULL);
 
-    PyObject *t = PyDict_SetDefault(interned, s, s);
-    if (t == NULL) {
+    PyObject *t;
+    int res = PyDict_SetDefaultRef(interned, s, s, &t);
+    if (res < 0) {
         PyErr_Clear();
         return;
     }
-
-    if (t != s) {
-        Py_SETREF(*p, Py_NewRef(t));
+    else if (res == 1) {
+        // value was already present (not inserted)
+        Py_SETREF(*p, t);
         return;
     }
+    Py_DECREF(t);
 
     if (_Py_IsImmortal(s)) {
         // XXX Restrict this to the main interpreter?
index 4c1d3bb2d2b475a573bd600a77438be64e18a4d7..15e5cf38a37b97aa7cc1c5ddb4e4fb0b6d480a7b 100644 (file)
@@ -958,14 +958,15 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
         return NULL;
     }
 
-    // t is borrowed reference
-    PyObject *t = PyDict_SetDefault(const_cache, key, key);
-    if (t != key) {
-        // o is registered in const_cache.  Just use it.
-        Py_XINCREF(t);
+    PyObject *t;
+    int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
+    if (res != 0) {
+        // o was not inserted into const_cache. t is either the existing value
+        // or NULL (on error).
         Py_DECREF(key);
         return t;
     }
+    Py_DECREF(t);
 
     // We registered o in const_cache.
     // When o is a tuple or frozenset, we want to merge its
@@ -7527,22 +7528,26 @@ _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj)
         return ERROR;
     }
 
-    // t is borrowed reference
-    PyObject *t = PyDict_SetDefault(const_cache, key, key);
+    PyObject *t;
+    int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
     Py_DECREF(key);
-    if (t == NULL) {
+    if (res < 0) {
         return ERROR;
     }
-    if (t == key) {  // obj is new constant.
+    if (res == 0) {  // inserted: obj is new constant.
+        Py_DECREF(t);
         return SUCCESS;
     }
 
     if (PyTuple_CheckExact(t)) {
-        // t is still borrowed reference
-        t = PyTuple_GET_ITEM(t, 1);
+        PyObject *item = PyTuple_GET_ITEM(t, 1);
+        Py_SETREF(*obj, Py_NewRef(item));
+        Py_DECREF(t);
+    }
+    else {
+        Py_SETREF(*obj, t);
     }
 
-    Py_SETREF(*obj, Py_NewRef(t));
     return SUCCESS;
 }