Python array --> C

Gordon McMillan gmcm at hypernet.com
Wed Jul 7 09:38:04 EDT 1999


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




More information about the Python-list mailing list