如何获取PyObject的引用计数? [英] How to get reference count of a PyObject?

查看:579
本文介绍了如何获取PyObject的引用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C ++获取 PyObject 的引用计数?

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

有一些函数 Py_INCREF Py_DECREF 减少它,但我还没有找到返回对象的引用计数的任何函数。

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.

我需要它为调试目的。

推荐答案

每个对象的引用计数都存储在 PyObject https://hg.python.org/cpython/file/37d896c3604a/Include/object.h#l107> ob_refcnt

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;

或者,您可以使用 Py_REFCNT

Alternatively, you can use the Py_REFCNT Macro.

这篇关于如何获取PyObject的引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆