473,651 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble Passing Array of Strings (using Numeric) to C Extension Module

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:

import Numeric
import TestDLL # my C Extension Module

a = Numeric.array(['foo', 'bar'], 'O' )
print 'a =', a
print 'type a[0] =', type(a[0])

print TestDLL.StringA rrayIn( a )

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_ParseTup le( args, "O", &pObject ) ) return NULL;

if ( ( pArray = ( PyArrayObject * )PyArray_Contig uousFromObject(
pObject, PyArray_OBJECT, 1, 1 ) ) == NULL ) return NULL;

iCount = pArray->dimensions[0];
iStride = pArray->strides[0];

bString = PyString_Check( ( PyObject * )( pArray->data ) );

Py_DECREF( pArray );
return Py_BuildValue( "iiO", iCount, iStride, PyBool_FromLong (
bString ) );
}

static PyMethodDef Methods[] =
{
{ "StringArrayIn" , _StringArrayIn, METH_VARARGS, "" },
{ NULL, NULL, 0, NULL } /*
Sentinel */
};

This code produces the following output:

a = [foo bar ]
type a[0] = <type 'str'>
(2, 4, False)

The iCount and iStride values are as I expect (2 and 4 respectively),
but performing a PyString_Check on the first array element (or more
likely, what I think is the first array element) returns 'False'.

BTW, the output above is from running the Python interpreter from the
command line. When I run this code in IDLE, I get a GPF if I don't
comment out the call to the PyString_Check function :-(

What am I doing wrong here?

Sep 20 '06 #1
2 2272
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_ParseTup le( args, "O", &pObject ) ) return NULL;

if ( ( pArray = ( PyArrayObject * )PyArray_Contig uousFromObject(
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

Sep 20 '06 #2

Travis E. Oliphant wrote:
>
PyString_Check( *( ( PyObject ** )( pArray->data ) ) );
Thank you very much Travis! My mistake is now quite obvious (and
should have been right from the start).

Regarding NumPy, I probably should upgrade from Numeric. It's just
hard to change something that is currently working fine for me and my
users.

Sep 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4948
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
0
1780
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
1
1979
by: youngdubliner | last post by:
I'm having a problem ........ I've stripped all my code to help isolate the problem. Its seems to be with importing numarray when python is embedded in C. I have a simple C program it Opens Python imports a script and then Closes Python like so .......
8
3854
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
4
2499
by: Chris Weisiger | last post by:
I'm trying to install numeric on my MacOS X box using Darwin, with the eventual goal of satisfying all of PyGame's dependencies so I can finally start working on my semester project. I would be using MacPython, except that I can't seem to get its Package Manager to work. Anyway, when I try to install numeric, I get the following error: chriswei% sudo ./setup.py install Password: running install running build
7
3255
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
12
2527
by: Sheldon | last post by:
Hi, I have two arrays that are of the same dimension but having 3 different values: 255, 1 or 2. I would like to set all the positions in both arrays having 255 to be equal, i.e., where one array has 255, I set the same elements in the other array to 255 and visa versa. Does anyone know how to do this without using for loops? Sincerely,
23
7395
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
6
6816
by: Peter Wuertz | last post by:
Hi, I'm writing a C module for python, that accesses a special usb camera. This module is supposed to provide python with data (alot of data). Then SciPy is used to fit the data. My question is, how to make python read from a C array? By reading the documentation, one could get the impression that PyBufferObjects do that job.
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.