[Python-Dev] Add Py_REPLACE and Py_XREPLACE macros
storchaka at gmail.com
Sat Feb 15 17:52:51 CET 2014
29.01.14 20:24, Serhiy Storchaka написав(ла):
> The Py_CLEAR macros is used as safe alternative for following unsafe
> idiomatic code:
> ptr = NULL;
> But other unsafe idiomatic code is widely used in the sources:
> ptr = new_value;
> Every occurrence of such code is potential bug for same reasons as for
> It was offered  to introduce new macros Py_REPLACE and Py_XREPLACE
> for safe replace with Py_DECREF and Py_XDECREF respectively.
> Automatically generated patch contains about 50 replaces .
>  http://bugs.python.org/issue16447
>  http://bugs.python.org/issue20440
There are objections to these patches. Raymond against backporting the
patch unless some known bugs are being fixed . But it fixes at least
one bug that caused a crash. And I suspect that there may be other bugs,
just we still have no reproducers. Even if we don't know how to
reproduce the bug, the current code looks obviously wrong. Also porting
the patch will make the sync easier. Note that the patches were
automatically generated, which reduces the possibility of errors. I just
slightly corrected formatting, remove unused variables and fixed one error.
Martin's objections are that the macros do add to the learning curve and
his expects that Py_REPLACE(op, op2) does an INCREF on op2, but it does
not . Antoine's original Py_(X)SETREF macros did INCREF and seems
this was one of their flaw, because in most cases INCREF is not needed.
Alternative names Py_(X)ASSIGN were suggested to break connotation of an
INCREF . As for learning curve, I think that it would be better to
have standard macros for code that can be written (and often are
written) incorrectly. And even already correct code can be written with
these macros in more short and clear way .
Greg shared Martin's expectation and suggested to revive this thread.
More information about the Python-Dev