GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDefault`. by colesbury · Pull Request #112211 · python/cpython

gh-112066: Use PyDict_SetDefaultRef in place of PyDict_SetDefault. #112211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
colesbury merged 3 commits into python:main from colesbury:PyDict_SetDefaultRef-usage
Feb 7, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-112066: Use PyDict_SetDefaultRef in place of PyDict_SetDefault. #112211

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
14a00f4
gh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDefault`.
colesbury Nov 17, 2023
654347a
Merge branch 'main' into PyDict_SetDefaultRef-usage
colesbury Feb 6, 2024
db6f518
Update Python/compile.c
colesbury Feb 7, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

5 changes: 2 additions & 3 deletions 5 Modules/_json.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/posixmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion 3 Modules/pyexpat.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions 6 Objects/typeobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
12 changes: 7 additions & 5 deletions 12 Objects/unicodeobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
29 changes: 17 additions & 12 deletions 29 Python/compile.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.