How to get reference count of a PyObject?

2019-02-12 14:59发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-12 15:32

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.

查看更多
登录 后发表回答