How to get reference count of a PyObject?

让人想犯罪 __ 提交于 2019-12-05 14:32:09

问题


How to get reference count of a PyObject from C++?

There are functions Py_INCREF and Py_DECREF which increase/decrease it, but I haven't found any function which return object's reference count.

I need it for debugging purposes.


回答1:


The reference count of each and every object is stored in the PyObject itself, in a variable called ob_refcnt. You can directly access that.

typedef struct _object {
    _PyObject_HEAD_EXTRA
    Py_ssize_t ob_refcnt;          # Reference count
    struct _typeobject *ob_type;
} PyObject;

Alternatively, you can use the Py_REFCNT Macro.



来源:https://stackoverflow.com/questions/26134455/how-to-get-reference-count-of-a-pyobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!