Trouble Passing Array of Strings (using Numeric) to C Extension Module
Travis E. Oliphant
oliphant.travis at ieee.org
Wed Sep 20 15:28:25 EDT 2006
goetzie wrote:
> I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I
> am passing a Numeric array of strings (objects) to a C Extension module
> using the following python code:
Numeric 23.8 is *very* old and unsupported. Unless you absolutely have
to use Numeric (then use 24.2), NumPy is a better choice for new code.
With that advice aside. Let's see...
>
> And here is the relevent code from my C Extension module:
>
> static PyObject * _StringArrayIn( PyObject *self, PyObject *args )
> {
> PyObject *pObject; // input array
> PyArrayObject *pArray; // contiguous array
> int iCount;
> int iStride;
> BOOL bString;
>
> if ( !PyArg_ParseTuple( args, "O", &pObject ) ) return NULL;
>
> if ( ( pArray = ( PyArrayObject * )PyArray_ContiguousFromObject(
> pObject, PyArray_OBJECT, 1, 1 ) ) == NULL ) return NULL;
>
> iCount = pArray->dimensions[0];
> iStride = pArray->strides[0];
>
> bString = PyString_Check( ( PyObject * )( pArray->data ) );
>
This is the problem.:
pArray->data should be interpreted as (PyObject **) -- an array of
PyObject *'s, and then de-referenced to get the PyObject * present at
the first address
So. this should work to check that the first entry in the array is a
string:
PyString_Check( *( ( PyObject ** )( pArray->data ) ) );
By the way, NumPy has support for true string (and unicode) arrays (as
well as object arrays like this)...
-Travis
More information about the Python-list
mailing list