[Python-Dev] Add Py_REPLACE and Py_XREPLACE macros

Nick Coghlan ncoghlan at gmail.com
Mon Feb 17 00:27:40 CET 2014


On 17 Feb 2014 06:12, "Serhiy Storchaka" <storchaka at gmail.com> wrote:
>
> 16.02.14 02:05, Nick Coghlan написав(ла):
>
>> It's also likely than many of these crashes could only be reproduced
>> through incorrect usage of the C API.
>
>
> Rather through queer or malicious usage of Python classes with strange
code in __del__.

This change doesn't fix any of the known crashers in Lib/test/crashers,
though - I applied the patch locally and checked.

>> For example:
>>
>>      Py_CLEAR_AND_SET
>>      Py_XCLEAR_AND_SET
>>
>> Such that Py_CLEAR and Py_XCLEAR are equivalent to:
>
>
> There is no Py_XCLEAR. Py_CLEAR itself checks its argument for NULL (as
Py_XDECREF and Py_XINCREF). And these names looks too cumbersome to me.
>
>
>> While the name does suggest the macro will expand to:
>>
>>      Py_CLEAR(target);
>>      target = value;
>>
>> which isn't quite accurate, it's close enough that people should still
>> be able to understand what the operation does.
>
>
> This is not just inaccurate, this is wrong. Py_REPLACE first assigns new
value and then DECREF old value.

The point is that people already know what Py_CLEAR does. This operation is
like Py_CLEAR (the old reference is only removed *after* the pointer has
been updated), except that the value it is being replaced with can be
something other than NULL. If the replacement value *is* NULL, then the new
operation is *exactly* equivalent to Py_CLEAR.

Operations that do related things should ideally have related names. The
point of my deliberately erroneous expansion is that it's an error a reader
can make while still correctly understanding the *logic* of the code, even
though they're missing a subtlety of the mechanics.

> So it rather can be named as Py_SET_AND_DECREF, but of course this name
looks ugly and confusing.

An explicit name like Py_SET_AND_DECREF would also be reasonable. It's
substantially less confusing than Py_REPLACE, as it is less ambiguous about
whether or not the refcount on the new value is adjusted.

Cheers,
Nick.

>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
https://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140217/db44f251/attachment-0001.html>


More information about the Python-Dev mailing list