I am working on the Python C Extension documentation to define new types and have just finished Providing more granular control over data attributes .
In this section, they modify the sample code to ensure that the first and last attributes of the Noddy structure can never be NULL , for example, by initializing empty string attributes to new and adding getters and setters that raise TypeError if the user tries to delete or otherwise set these attributes to null.
Additionally (and the point of my question), the author changes all Py_XDECREF to Py_DECREF for these attributes, stating that:
With these changes, we can guarantee that the first and last members will never be NULL, so we can remove checks for NULL values ββin almost all cases. This means that most Py_XDECREF () calls can be converted to Py_DECREF () calls. The only place we cannot change these calls is in the deallocator, where it is likely that the initialization of these members failed in the constructor.
It seems to me that it would be safer to use Py_XDECREF , given that Py_DECREF leads to a segmentation fault if it passed a NULL value.
What is the advantage of using Py_DECREF over Py_XDECREF ?
python python-c-extension python-c-api
Matthew moisen
source share