C extension/char array question

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Dec 4 09:34:26 EST 2001


"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