Question concerning array.array and C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabio

    Question concerning array.array and C++

    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

  • Hrvoje Niksic

    #2
    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@ 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_Import Module("array") ;
    if (!array_module)
    goto error;

    // array_type = array.array
    PyObject *array_type = PyObject_GetAtt rString(array_m odule, "array");
    Py_DECREF(array _module);
    if (!array_type)
    goto error;

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

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

    Comment

    Working...