[Python-Dev] Poll: Py_REPLACE/Py_ASSIGN/etc

Nick Coghlan ncoghlan at gmail.com
Fri Feb 28 13:27:00 CET 2014

On 28 Feb 2014 19:05, "Larry Hastings" <larry at hastings.org> wrote:
> On 02/26/2014 11:13 PM, Georg Brandl wrote:
>> Am 26.02.2014 17:09, schrieb Ryan Gonzalez:
>>> I like Py_DECREF_REPLACE. It gives the impression that it decrefs the
>>> and replaces it.
>> Agreed, most other suggestions are not really explicit enough.
> +1 from me too.  When I saw Py_SETREF I thought, oh, it sets the thing
and increfs it.
> FWIW this vote is just on the name.  I haven't stared at the whole
Py_REPLACE idea enough to have an opinion about whether or not to use it.
But if we use it I'm +1 on Py_DECREF_REPLACE.

For additional context, the idea itself is necessary for the same reason
Py_CLEAR was added: to help ensure that an object's state is never pointing
at another object that is in the process of being deleted. The difference
is that Py_CLEAR only allows setting the pointer to NULL, while the point
of the new macro is to set it to an arbitrary existing point. There is no
implicit incref as that isn't needed for correctness (you can do the incref
before the pointer replacement, and often the reference count will already
be correct without an explicit incref anyway).

With the new macro in place, the existing Py_CLEAR(x) macro would be
equivalent to Py_SETREF(x, NULL).

Originally I was also concerned about the "how will people know there's no
implicit incref?", but I've since become satisfied with the fact that the
precedent set by the reference stealing SET_ITEM macros is strong enough to
justify the shorter name.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140228/e7db7b66/attachment.html>

More information about the Python-Dev mailing list