Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ef3ceab

Browse filesBrowse files
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>
1 parent fedbf77 commit ef3ceab
Copy full SHA for ef3ceab

File tree

Expand file treeCollapse file tree

6 files changed

+32
-25
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+32
-25
lines changed

‎Modules/_json.c

Copy file name to clipboardExpand all lines: Modules/_json.c
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,10 @@ _parse_object_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ss
691691
key = scanstring_unicode(pystr, idx + 1, s->strict, &next_idx);
692692
if (key == NULL)
693693
goto bail;
694-
memokey = PyDict_SetDefault(memo, key, key);
695-
if (memokey == NULL) {
694+
if (PyDict_SetDefaultRef(memo, key, key, &memokey) < 0) {
696695
goto bail;
697696
}
698-
Py_SETREF(key, Py_NewRef(memokey));
697+
Py_SETREF(key, memokey);
699698
idx = next_idx;
700699

701700
/* skip whitespace between key and : delimiter, read :, skip whitespace */

‎Modules/posixmodule.c

Copy file name to clipboardExpand all lines: Modules/posixmodule.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ convertenviron(void)
16271627
Py_DECREF(d);
16281628
return NULL;
16291629
}
1630-
if (PyDict_SetDefault(d, k, v) == NULL) {
1630+
if (PyDict_SetDefaultRef(d, k, v, NULL) < 0) {
16311631
Py_DECREF(v);
16321632
Py_DECREF(k);
16331633
Py_DECREF(d);

‎Modules/pyexpat.c

Copy file name to clipboardExpand all lines: Modules/pyexpat.c
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,8 @@ static int init_handler_descrs(pyexpat_state *state)
16151615
if (descr == NULL)
16161616
return -1;
16171617

1618-
if (PyDict_SetDefault(state->xml_parse_type->tp_dict, PyDescr_NAME(descr), descr) == NULL) {
1618+
if (PyDict_SetDefaultRef(state->xml_parse_type->tp_dict,
1619+
PyDescr_NAME(descr), descr, NULL) < 0) {
16191620
Py_DECREF(descr);
16201621
return -1;
16211622
}

‎Objects/typeobject.c

Copy file name to clipboardExpand all lines: Objects/typeobject.c
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6683,7 +6683,7 @@ type_add_method(PyTypeObject *type, PyMethodDef *meth)
66836683
int err;
66846684
PyObject *dict = lookup_tp_dict(type);
66856685
if (!(meth->ml_flags & METH_COEXIST)) {
6686-
err = PyDict_SetDefault(dict, name, descr) == NULL;
6686+
err = PyDict_SetDefaultRef(dict, name, descr, NULL) < 0;
66876687
}
66886688
else {
66896689
err = PyDict_SetItem(dict, name, descr) < 0;
@@ -6731,7 +6731,7 @@ type_add_members(PyTypeObject *type)
67316731
if (descr == NULL)
67326732
return -1;
67336733

6734-
if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
6734+
if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) {
67356735
Py_DECREF(descr);
67366736
return -1;
67376737
}
@@ -6756,7 +6756,7 @@ type_add_getset(PyTypeObject *type)
67566756
return -1;
67576757
}
67586758

6759-
if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
6759+
if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) {
67606760
Py_DECREF(descr);
67616761
return -1;
67626762
}

‎Objects/unicodeobject.c

Copy file name to clipboardExpand all lines: Objects/unicodeobject.c
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14894,16 +14894,18 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p)
1489414894
PyObject *interned = get_interned_dict(interp);
1489514895
assert(interned != NULL);
1489614896

14897-
PyObject *t = PyDict_SetDefault(interned, s, s);
14898-
if (t == NULL) {
14897+
PyObject *t;
14898+
int res = PyDict_SetDefaultRef(interned, s, s, &t);
14899+
if (res < 0) {
1489914900
PyErr_Clear();
1490014901
return;
1490114902
}
14902-
14903-
if (t != s) {
14904-
Py_SETREF(*p, Py_NewRef(t));
14903+
else if (res == 1) {
14904+
// value was already present (not inserted)
14905+
Py_SETREF(*p, t);
1490514906
return;
1490614907
}
14908+
Py_DECREF(t);
1490714909

1490814910
if (_Py_IsImmortal(s)) {
1490914911
// XXX Restrict this to the main interpreter?

‎Python/compile.c

Copy file name to clipboardExpand all lines: Python/compile.c
+17-12Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -958,14 +958,15 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
958958
return NULL;
959959
}
960960

961-
// t is borrowed reference
962-
PyObject *t = PyDict_SetDefault(const_cache, key, key);
963-
if (t != key) {
964-
// o is registered in const_cache. Just use it.
965-
Py_XINCREF(t);
961+
PyObject *t;
962+
int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
963+
if (res != 0) {
964+
// o was not inserted into const_cache. t is either the existing value
965+
// or NULL (on error).
966966
Py_DECREF(key);
967967
return t;
968968
}
969+
Py_DECREF(t);
969970

970971
// We registered o in const_cache.
971972
// When o is a tuple or frozenset, we want to merge its
@@ -7527,22 +7528,26 @@ _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj)
75277528
return ERROR;
75287529
}
75297530

7530-
// t is borrowed reference
7531-
PyObject *t = PyDict_SetDefault(const_cache, key, key);
7531+
PyObject *t;
7532+
int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
75327533
Py_DECREF(key);
7533-
if (t == NULL) {
7534+
if (res < 0) {
75347535
return ERROR;
75357536
}
7536-
if (t == key) { // obj is new constant.
7537+
if (res == 0) { // inserted: obj is new constant.
7538+
Py_DECREF(t);
75377539
return SUCCESS;
75387540
}
75397541

75407542
if (PyTuple_CheckExact(t)) {
7541-
// t is still borrowed reference
7542-
t = PyTuple_GET_ITEM(t, 1);
7543+
PyObject *item = PyTuple_GET_ITEM(t, 1);
7544+
Py_SETREF(*obj, Py_NewRef(item));
7545+
Py_DECREF(t);
7546+
}
7547+
else {
7548+
Py_SETREF(*obj, t);
75437549
}
75447550

7545-
Py_SETREF(*obj, Py_NewRef(t));
75467551
return SUCCESS;
75477552
}
75487553

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.