C extension/char array question

Bob Greschke bob at passcal.nmt.edu
Tue Dec 4 12:31:14 EST 2001


You don't know what I'm asking, because I don't know what I'm saying.
:)

What I want to do (today...I think) is create the equivalent of a C
array of strings like

   char *Array[] = {"THIS", "THAT"};

BUT do it in Python such that it can then be passed to an extension
wrapper function, such that it can then be passed on to a C function
so that the called function can do something like

   printf("%s", X[1]);

and have the output on the screen be "THAT".

I don't know what "char *Array[] = {"THIS", "THAT"};" would look like
written in Python, nor do I have any idea what would be used in the
PyArg_ParseTuple() function in the wrapper function to make it look
like an array of strings.  I could just put the creation of Array in
the .c extension file with all of the wrapper functions for the
program, but it would be cleaner if I could create the list of strings
in Python, instead.

I'm not sure if this is any clearer or not. :)

Bob

"Martin von Loewis" <loewis at informatik.hu-berlin.de> wrote in message
news:j4zo4zyryl.fsf at informatik.hu-berlin.de...
> "Bob Greschke" <bob at passcal.nmt.edu> writes:
>
> > What I want to be able to do is code the list of things to look
for
> > ("THIS" and "THAT") into the Python part, and call a C extension
> > function that calls the C library function that does the job of
> > RememberMe().
>
> The simplest solution is to do
>
> PyObject *RememberMe = NULL;
>
> PyObject *
> MyMod_Remember(PyObject *self, PyObject *args)
> {
>   Py_XDECREF(RememberMe); // forget old value
>   Py_INCREF(args);
>   RememberMe = args;      // hold onto the argument tuple
>   Py_INCREF(Py_None);
>   return Py_None;
> }
>
> > I guess I'm not sure what to put "THIS" and "THAT" into
> > (a list?), and what format specifier to use in
PyArg_ParseTuple(), to
> > make sure that C is able to keep track of the addresses of the
char
> > array elements so it can use them in the strstr() function -- if
this
> > can be done in a straightforward manner at all.
>
> Not sure what you are asking here - if this is a question at all.
>
> > Will I have to 'recreate' the array of strings in the extension,
> > like with malloc()?
>
> No. Just increment the refcount on the object you get, and you can
> hold onto it as long as you want.
>
> Regards,
> Martin





More information about the Python-list mailing list