Skip to content

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