[Python-Dev] Py_CLEAR and assigning values

Guido van Rossum guido at python.org
Tue Aug 5 22:48:06 CEST 2008


I don't see a way that __del__ could be invoked and access the NULL
between the Py_CLEAR() call and the Py_INCREF() call in the second
version.

On Tue, Aug 5, 2008 at 1:38 PM, Paul Pogonyshev <pogonyshev at gmx.net> wrote:
> Hi,
>
> Sorry if this topic was discussed before, but I couldn't find.
>
> Py_CLEAR documentation explains why it is better than Py_DECREF and
> NULL assignment.  However, I don't understand why there is no similar
> macro for the case you want to replace one object with another?
>
> I.e. 'naive' way:
>
>        Py_DECREF (self->x);
>        /* This is prone to the same bug Py_CLEAR prevents. */
>        self->x = y;
>        Py_INCREF (self->x);
>
> Py_CLEAR way:
>
>        Py_CLEAR (self->x);
>        /* But __del__ can now in principle trigger access to NULL. */
>        self->x = y;
>        Py_INCREF (self->x);
>
> Way I'd think would be possible:
>
>        Py_ASSIGN (self->x, y)
>
> where Py_ASSIGN is defined this way:
>
> #define Py_ASSIGN(op, val)                              \
>        do {                                            \
>                PyObject *_py_tmp = (PyObject *)(op);   \
>                (op) = val;                             \
>                Py_XINCREF(op);                         \
>                Py_XDECREF(_py_tmp);                    \
>        } while (0)
>
> Do I miss something obvious?  Or is there already a standard way to
> do that which I overlooked?
>
> Paul
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list