By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Question concerning array.array and C++

P: n/a
Hi All,
I have a question concerning the use of array.array inside of C++ code I
wrote.

I am working with _big_ data files but some entries in these files are
usually bounded say between -5 to 40. Returning a Python list makes no
sense. In Python I always work with the array.array module which does
the trick. But now that I wrote my own C++ module for some preprocessing
I need the return array.array objects.

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?

I already looked at arraymodule.c and tried to link the arraymodule.o
file to my code but then I have to mess around with extern "C" and the
like and it gets really messy.

Any help would be great!

Fabio

Nov 5 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
[ 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 <Au***********@gmx.netwrites:
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
Nov 5 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.