Re: Question concerning array.array and C++



[ Please consider posting to the capi-sig list, which is dedicated to
answering questions like yours.
http://mail.python.org/mailman/listinfo/capi-sig ]

Fabio <Auslieferator@xxxxxxx> writes:

Consider the object

array.array('c',[40,40,40])

Can I create such an object from within the C++ layer and pass it to
the Python layer?

Yes, simply access the array type object using getattr (the "."
operator), much like you would from Python, and instantiate the type
by calling it:

// import array
PyObject *array_module = PyImport_ImportModule("array");
if (!array_module)
goto error;

// array_type = array.array
PyObject *array_type = PyObject_GetAttrString(array_module, "array");
Py_DECREF(array_module);
if (!array_type)
goto error;

// array = array_type('c', [40, 40, 40])
PyObject *array = PyObject_CallFunction(array_type, "c[iii]", 'c', 40, 40, 40);
if (!array)
goto error;

// at this point you have (a new reference to) the array object
.