Embedded Python and sharing Data?

Martin von Loewis loewis at informatik.hu-berlin.de
Wed Mar 14 05:50:58 EST 2001


Karl Bellve <Karl.Bellve at umassmed.edu> writes:

> The question is, how do I share data between python and my
> program. The data can be as large as a gigabyte and is usually
> stored in memory. I obviously don't want to duplicate this data :-)
> Can I pass pointers? Can I create a Python Object but then how do I
> tell python that it contains a normal integer array?

The best way would be to define your own data type, which should
implement the buffer protocol (see
http://www.python.org/doc/current/api/buffer-structs.html for the
definition of the buffer protocol, and xxmodule.c for a minimum
example of a C type). Then you get an object for which you can do
indexed access to bytes, slicing, etc. You can even use
PyBuffer_FromMemory if it is *always* stored in memory
(i.e. accessible through a pointer); in that case you don't have to
implement your own type.

If you want to expose the data in a more structured way (e.g. as an
array of ints), you again have to define your own type, and provide it
with accessor functions. You'ld implement the sequence protocol, and
construct integer objects when given an integer index.

Regards,
Martin



More information about the Python-list mailing list