Number of Links and Python Link Types

Hello,

I have some problems with link counting in python. What I want to do is return the tuple from C ++ to python using the ctypes module.

C ++:

PyObject* foo(...)
{

  ...
  return Py_BuildValue("(s, s)", value1, value2);
}

Python:

pointer = c_foo(...) # c_foo loaded with ctypes
obj = cast(pointer, py_object).value

I was not sure about the number of obj links, so I tried sys.getrefcount()  and got it 3. I think it should be 2(functions getrefcountdo one ref by themselves).

Now I cannot do it Py_DECREF()before returning to C ++, because the object will be deleted. Can i reduce the number of links in python?

edit What happens with the ref count when calling the translation function? I am not sure about the documentation below. http://docs.python.org/library/ctypes.html#ctypes.cast

ctypes.cast(obj, type)     C. , , obj. type , obj , .

+5
2

, . http://docs.python.org/library/ctypes.html#callback-functions , .

clib = ctypes.cdll.LoadLibrary('some.so')
c_foo = clib.c_foo
c_foo.restype = ctypes.py_object

, .

+4

++ , C-API, , ctypes c python (, int, float ..).

C-API " " ( ctypes), , python Py_BuildValue. , .

Py_XINCREF/Py_XDECREF (, Py_INCREF/Py_DECREF, NULL), :

, python ( py_map). ++ Foo, python ( py_Foo). , [], py_Foo python:

F = py_Map["key"]

, F, ++ !

++ []:

...
PyObject* result; // My py_Foo object
Py_XINCREF(result); // transfer the ownership
return result;
}

python. .

+4

All Articles