[Python-3000] Use Py_CLEAR only

Thomas Wouters thomas at python.org
Sun Apr 23 23:42:26 CEST 2006


On 4/23/06, "Martin v. Löwis" <martin at v.loewis.de> wrote:

> For pointer that live on the heap, Py_[X]DECREF is usually
> done because either
> a) the memory block containing the pointer is about to be
>    released (e.g. when a tuple is deallocated), or
> b) the pointer is about to be changed to point to something
>    else (e.g. a setitem operation)


Eh, hang on. The reason to use Py_CLEAR() isn't that the pointer may be used
after the Py_DECREF(), but that it may be used *as a result of DECREF'ing*.
DECREFs can cause cleanup, which can execute arbitrary Python or C code
through finalizers or weakref callbacks. So whatever you're doing after the
Py_DECREF/Py_CLEAR call is immaterial to the decision to use either. You
should use Py_CLEAR when the pointer you are DECREF'ing *might* go away, and
is accessible from outside the function (i.e. it isn't only reachable
through automatic variables.)

However, I agree that it should be a conscious decision, and not an
automatism.

--
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20060423/cb3e99a1/attachment.htm 


More information about the Python-3000 mailing list