[Pyrex] fix for tp_clear()

Stefan Behnel stefan_ml at behnel.de
Sat Jan 12 23:23:26 CET 2008


Hi,

I happened to read this page, which describes how tp_clear() should be
implemented.

http://docs.python.org/ext/node24.html

The race condition that is described there and in the comment in
Python/Include/object.h hits Pyrex and Cython (although I can't tell how rare
it really is - I never noticed anything like that).

Also, Pyrex and Cython set the type members to None, while the page above
assigns NULL. I don't know if None is needed here - to mimic the old
behaviour, you could do the usual

    "tmp = %s; %s = Py_None; Py_INCREF(Py_None);" % (name, name)

dance instead of the

    "tmp = %s; %s = 0;" % (name, name)

INCREFs are cheap, so this won't matter too much performance-wise. The
important bit is that the pointer gets cleared *before* the final XDECREF(),
which may lead to the execution of arbitrary code (such as deallocs and whatnot).

I'd like to hear opinions why the None would be necessary here, otherwise I'd
just leave it with the NULL.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tp_clear-fix.patch
Type: text/x-patch
Size: 1247 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20080112/219ffa5f/attachment.bin 


More information about the Pyrex mailing list