Skip to content
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

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

Merged
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

vstinner commented Nov 22, 2022

@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
13 checks passed
@vstinner vstinner deleted the setref_null branch Nov 23, 2022
@erlend-aasland
Copy link
Contributor

erlend-aasland commented Nov 23, 2022

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.

None yet

3 participants