Python array --> C

Randy Heiland heiland at ncsa.uiuc.edu
Wed Jul 7 09:23:38 EDT 1999


Gordon McMillan wrote:

> Randy Heiland writes:
>
> > In my continuing quest to better understand embedding/extending, I
> > have the following question...
> >
> > in Demo/embed, I have modified demo.c so that the static module
> > xyzzy_foo(self, args) now does the following (instead of returning
> > 42L): {
> >         unsigned char retval[5] = {0,2,4,8,16};
> >         return Py_BuildValue("s#", retval,5);
> > }
> > i.e., it returns an array of unsigned chars.
> >
> > From main(), I would like to invoke xyzzy.foo() and get this array
> > of unsigned chars into a C array. How do I do this?  I assume I'll
> > need to use PyRun_String(...) somehow - true?
>
> Not at all. You'll want to use PyString methods to get its size and
> char *, etc. You can use python as a lib without ever involving the
> interpreter. See the C API reference manual (and be prepared to grep
> the objects and modules subdirectories).
>
> - Gordon

Can you please be more specific (sorry, I need a lot of hand-holding).
Exactly what PyString method
would I call in main() to (1) invoke the xyzzy.foo() method and (2) get
the resulting byte array into a C array?

--Randy





More information about the Python-list mailing list