[Python-Dev] Add Py_REPLACE and Py_XREPLACE macros
Serhiy Storchaka
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:
>
> Py_XDECREF(ptr);
> ptr = NULL;
>
> But other unsafe idiomatic code is widely used in the sources:
>
> Py_XDECREF(ptr);
> ptr = new_value;
>
> Every occurrence of such code is potential bug for same reasons as for
> Py_CLEAR.
>
> It was offered [1] 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 [2].
>
> [1] http://bugs.python.org/issue16447
> [2] http://bugs.python.org/issue20440
There are objections to these patches. Raymond against backporting the
patch unless some known bugs are being fixed [1]. 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 [2]. 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 [3]. 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 [4].
Greg shared Martin's expectation and suggested to revive this thread.
[1] http://bugs.python.org/issue20440#msg209701
[2] http://bugs.python.org/issue20440#msg209894
[3] http://bugs.python.org/issue20440#msg210447
[4] http://bugs.python.org/issue3081#msg102645
More information about the Python-Dev
mailing list