summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-02-07 18:43:18 (GMT)
committerGitHub <noreply@github.com>2024-02-07 18:43:18 (GMT)
commitef3ceab09d2d0959c343c662461123d5b0e0b64b (patch)
tree7c7be8598454fca38217703987b9f60e66e4a952 /Python
parentfedbf77191ea9d6515b39f958cc9e588d23517c9 (diff)
downloadcpython-ef3ceab09d2d0959c343c662461123d5b0e0b64b.zip
cpython-ef3ceab09d2d0959c343c662461123d5b0e0b64b.tar.gz
cpython-ef3ceab09d2d0959c343c662461123d5b0e0b64b.tar.bz2
gh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDefault`. (#112211)
This changes a number of internal usages of `PyDict_SetDefault` to use `PyDict_SetDefaultRef`. Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 4c1d3bb..15e5cf3 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -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;
}