Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-99537: Use Py_SETREF(var, NULL) in C code #99687

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.

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
merged 1 commit into from
Nov 23, 2022
Merged

gh-99537: Use Py_SETREF(var, NULL) in C code #99687

merged 1 commit into from
Nov 23, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Nov 22, 2022

Replace "Py_DECREF(var); var = NULL;" with "Py_SETREF(var, NULL);".

Replace "Py_DECREF(var); var = NULL;" with "Py_SETREF(var, NULL);".
@vstinner
Copy link
Member Author

@erlend-aasland: I wrote PR #99686 to replace Py_XDECREF(var); var=NULL; with Py_CLEAR(var);. This PR is to replace Py_DECREF(var); var=NULL; with Py_SETREF(var, NULL);. What do you think? Does it make the code shorter and more readable?

For me, Py_SETREF() is easier because I don't have to think: hum, should DECREF be called before or after setting the variable to the new value (NULL)? From my point of view, it looks like an "atomic" which is always "correct" (when used properly: when the variable already contains a strong reference).

@vstinner vstinner merged commit 81f7359 into python:main Nov 23, 2022
@vstinner vstinner deleted the setref_null branch November 23, 2022 13:57
@erlend-aasland
Copy link
Contributor

What do you think? Does it make the code shorter and more readable?

Sorry, I forgot about this! IMO, this change improves readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants