[Numpy-discussion] character arrays supported by C API?
jmiller at stsci.edu
Thu Oct 14 06:11:22 CDT 2004
On Thu, 2004-10-14 at 04:20, Faheem Mitha wrote:
> On Wed, 15 Sep 2004, Todd Miller wrote:
> > On Wed, 2004-09-15 at 00:52, Faheem Mitha wrote:
> >> Dear People,
> >> Are character arrays supported by the Numarray C API? My impression from
> >> the documentation is no, but I would appreciate a confirmation. Thanks.
> >> Faheem.
> > Yes and no. CharArray is not as well supported from C as NumArray;
> > there are no easy to call functions which will convert a nested sequence
> > of strings into a CharArray.
> > However, it is possible to call the Python functions in the CharArray
> > module from C, and a pre-existing CharArray is a PyArrayObject so it
> > can be manipulated in C as a struct; it's shape and strides are
> > visible, it's itemsize is the length of the string, etc.
> > What is it you want to do? What functions do you think would help?
> Hi. Sorry about the slow reply.
> What I want to do is extremely simple. I want to convert (in C++) a C++
> character array to a CharArray. The simplest way of doing this would be to
> create an array of the appropriate size, and write character strings into
> it element by element.
> So, a utility function which creates a character array of appropriate
> dimensions would be useful. Also a utility function which convert a list
> of strings into a Character Array would also be desirable.
> Currently I am having to work around this limitation by returning lists of
> strings back to Python. I'd prefer to not have to do that.
That's a sensible addition, but right now, such a function does not
exist, and I don't have time to add it myself. The way to achieve this
without C-API support by CharArray is to do a Python callback. The
steps in C would be roughly:
0. Import the numarray.strings module. PyImport_ImportModule().
1. Get the module's dictionary object. PyModule_GetDict().
2. Get a pointer to CharArray by looking it up in the dictionary.
3. Construct an argument tuple which contains the constructor
4. Call the constructor using the arg tuple. The return value is the
Similar steps are done for NumArray in the current C-API in newarray.ch
More information about the Numpy-discussion