小编lon*_*lue的帖子

引用计数在Python C扩展中

我正在编写我对Python的第一个C扩展,并且对我的引用计数感到困惑.这就是我想要做的.

我在for循环中填充了一个dict:

mydict = PyDict_New();

for (...)
{
    pair = PyTuple_Pack(2, PyString_FromString("some string"),
          PyString_FromString("some other string"));

    /* at this point, the refcnt for the tuple is 1, the refcnts for the
       2 string items are 2. Because according to the source, PyString_FromString
       does an INCREF, and PyTuple_Pack() does an INCREF on its items
     */

    PyDict_SetItem(mydict, PyString_FromString("some key"), pair);

    /* At this point, the key's refcnt is 2.  PyString_FromString sets it to 1 and 
       PyDict_SetItem INCREF's it. Ditto for pair since …
Run Code Online (Sandbox Code Playgroud)

c python

11
推荐指数
1
解决办法
2332
查看次数

标签 统计

c ×1

python ×1