c++ 调用 python API Py_XDECREF 和 Py_DECREF

创建日期: 2024-06-07 17:30 | 作者: 风波 | 浏览次数: 13 | 分类: C++

来源:https://stackoverflow.com/questions/41915305/is-there-any-benefit-to-using-py-decref-instead-of-py-xdecref-for-python-c-exten

If you know that the object cannot be NULL, the benefits of Py_DECREF over Py_XDECREF are that:

These points can be important when dealing with low-level code, which is why Python core and most extensions are careful to only use Py_XDECREF (or Py_CLEAR in tp_clear) when the pointer can actually be NULL.

Technically it's undefined behavior, which means that a crash is not guaranteed the way it would be with an actual assertion. In practice, however, the compiler has little choice but to generate code that dereferences the pointer, which will lead to a memory fault if it is NULL.

13 浏览
9 爬虫
0 评论