mirror of https://github.com/python/cpython
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>
This commit is contained in:
parent
fedbf77191
commit
ef3ceab09d
|
@ -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);
|
key = scanstring_unicode(pystr, idx + 1, s->strict, &next_idx);
|
||||||
if (key == NULL)
|
if (key == NULL)
|
||||||
goto bail;
|
goto bail;
|
||||||
memokey = PyDict_SetDefault(memo, key, key);
|
if (PyDict_SetDefaultRef(memo, key, key, &memokey) < 0) {
|
||||||
if (memokey == NULL) {
|
|
||||||
goto bail;
|
goto bail;
|
||||||
}
|
}
|
||||||
Py_SETREF(key, Py_NewRef(memokey));
|
Py_SETREF(key, memokey);
|
||||||
idx = next_idx;
|
idx = next_idx;
|
||||||
|
|
||||||
/* skip whitespace between key and : delimiter, read :, skip whitespace */
|
/* skip whitespace between key and : delimiter, read :, skip whitespace */
|
||||||
|
|
|
@ -1627,7 +1627,7 @@ convertenviron(void)
|
||||||
Py_DECREF(d);
|
Py_DECREF(d);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (PyDict_SetDefault(d, k, v) == NULL) {
|
if (PyDict_SetDefaultRef(d, k, v, NULL) < 0) {
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
Py_DECREF(k);
|
Py_DECREF(k);
|
||||||
Py_DECREF(d);
|
Py_DECREF(d);
|
||||||
|
|
|
@ -1615,7 +1615,8 @@ static int init_handler_descrs(pyexpat_state *state)
|
||||||
if (descr == NULL)
|
if (descr == NULL)
|
||||||
return -1;
|
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);
|
Py_DECREF(descr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6683,7 +6683,7 @@ type_add_method(PyTypeObject *type, PyMethodDef *meth)
|
||||||
int err;
|
int err;
|
||||||
PyObject *dict = lookup_tp_dict(type);
|
PyObject *dict = lookup_tp_dict(type);
|
||||||
if (!(meth->ml_flags & METH_COEXIST)) {
|
if (!(meth->ml_flags & METH_COEXIST)) {
|
||||||
err = PyDict_SetDefault(dict, name, descr) == NULL;
|
err = PyDict_SetDefaultRef(dict, name, descr, NULL) < 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
err = PyDict_SetItem(dict, name, descr) < 0;
|
err = PyDict_SetItem(dict, name, descr) < 0;
|
||||||
|
@ -6731,7 +6731,7 @@ type_add_members(PyTypeObject *type)
|
||||||
if (descr == NULL)
|
if (descr == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
|
if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) {
|
||||||
Py_DECREF(descr);
|
Py_DECREF(descr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -6756,7 +6756,7 @@ type_add_getset(PyTypeObject *type)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
|
if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) {
|
||||||
Py_DECREF(descr);
|
Py_DECREF(descr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14894,16 +14894,18 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p)
|
||||||
PyObject *interned = get_interned_dict(interp);
|
PyObject *interned = get_interned_dict(interp);
|
||||||
assert(interned != NULL);
|
assert(interned != NULL);
|
||||||
|
|
||||||
PyObject *t = PyDict_SetDefault(interned, s, s);
|
PyObject *t;
|
||||||
if (t == NULL) {
|
int res = PyDict_SetDefaultRef(interned, s, s, &t);
|
||||||
|
if (res < 0) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (res == 1) {
|
||||||
if (t != s) {
|
// value was already present (not inserted)
|
||||||
Py_SETREF(*p, Py_NewRef(t));
|
Py_SETREF(*p, t);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Py_DECREF(t);
|
||||||
|
|
||||||
if (_Py_IsImmortal(s)) {
|
if (_Py_IsImmortal(s)) {
|
||||||
// XXX Restrict this to the main interpreter?
|
// XXX Restrict this to the main interpreter?
|
||||||
|
|
|
@ -958,14 +958,15 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// t is borrowed reference
|
PyObject *t;
|
||||||
PyObject *t = PyDict_SetDefault(const_cache, key, key);
|
int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
|
||||||
if (t != key) {
|
if (res != 0) {
|
||||||
// o is registered in const_cache. Just use it.
|
// o was not inserted into const_cache. t is either the existing value
|
||||||
Py_XINCREF(t);
|
// or NULL (on error).
|
||||||
Py_DECREF(key);
|
Py_DECREF(key);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
Py_DECREF(t);
|
||||||
|
|
||||||
// We registered o in const_cache.
|
// We registered o in const_cache.
|
||||||
// When o is a tuple or frozenset, we want to merge its
|
// 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;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// t is borrowed reference
|
PyObject *t;
|
||||||
PyObject *t = PyDict_SetDefault(const_cache, key, key);
|
int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
|
||||||
Py_DECREF(key);
|
Py_DECREF(key);
|
||||||
if (t == NULL) {
|
if (res < 0) {
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
if (t == key) { // obj is new constant.
|
if (res == 0) { // inserted: obj is new constant.
|
||||||
|
Py_DECREF(t);
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyTuple_CheckExact(t)) {
|
if (PyTuple_CheckExact(t)) {
|
||||||
// t is still borrowed reference
|
PyObject *item = PyTuple_GET_ITEM(t, 1);
|
||||||
t = 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;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue