[Python-Dev] Add Py_REPLACE and Py_XREPLACE macros

Serhiy Storchaka storchaka at gmail.com
Sun Feb 16 21:10:40 CET 2014


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__.

> 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. So it rather can be named as 
Py_SET_AND_DECREF, but of course this name looks ugly and confusing.




More information about the Python-Dev mailing list