Question concerning array.array and C++
Hrvoje Niksic
hniksic at xemacs.org
Wed Nov 5 10:24:27 EST 2008
[ 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 at gmx.net> 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
More information about the Python-list
mailing list