[C++-sig] Mapping RAW memory data into Python

Marek Denis marek at octogan.net
Mon Oct 25 19:51:16 CEST 2010


Hi,
While I had some questions concerning accessing objects in Python, that 
were previously created in C++ now I'd like to know whether it's 
possible to get access to the raw memory from Python layer.
Let's suppose I have the char* ptr pointer that points to the memory 
chunk with IPv4 packet. I would like to be able to read (and preferably 
write) data from Python layer (not via wrapped object that contains 
char* ptr and has some methods to write/read data), but without making 
any memory copies.
I can use struct (even define format dynamically) in Python, but I don't 
know how do I pass the proper pointer from C++. If I use 
boost::python::object I had problems that script didn't know char type. 
I can use boost::python::str, return it and use in Python but I suppose 
it does copy data into str object, right?

If not Boost, maybe basic Python C API would work?

BTW. Suppose I had char* txt = "some string";
How do i create boost::python::object() that would containt the whole 
string? Passing *txt or txt into boost::python::object(*txt) didn't work.

-- 
regards

Marek Denis


More information about the Cplusplus-sig mailing list