I'm writing a C extension for Python and going through the documentation, I'm having a hard time understanding member assignment in the __init__ function.
So there, in section 2.2, member assignment is done as follow:
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
Py_XDECREF(tmp);
}
The explanation says later:
Our type doesn’t restrict the type of the first member, so it could be any kind of object. It could have a destructor that causes code to be executed that tries to access the first member; or that destructor could release the Global interpreter Lock and let arbitrary code run in other threads that accesses and modifies our object.
If I Py_XDECREF it, self->first will become invalid.
I understand the case where, if the destructor releases the Global interpreter Lock, there is a dangling pointer and my object (self) could be modified, etc... Everything goes out of control.
But why is it an issue the destructor accesses it ? Why this part:
It could have a destructor that causes code to be executed that tries to access the first member;
is an issue ? If the destructor of self->first accesses itself, fine. I don't care, it's its problem.
Hope I'm clear enough. Thanks for your replies.
firstcould have a reference back to the object being initialised, this other object's destructor could itself changefirst(it's exposed via aPyMemberDef) and cause an issue, if the code wasn't being as careful. can write up a better description as an answer if that's about right...firstdo something with the back reference (theselfofself->first) ?