<div dir="ltr"><div><span class="im" style="font-size:12.8px"><br>> In your something class, the data type returned from allocate_memory()<br>> needs to be something that Python understands. Since that allocation<br>> function (member) will be allocating 100s of MB of memory, how will<br>> this memory map to a Python data type?<br><br></span><span style="font-size:12.8px">>>Does it have to be a Python (native) data type ? Could you explain your</span><br style="font-size:12.8px"><span style="font-size:12.8px">>>use-case a little more ?</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">It needs to be a data type that can be allocated in C++ but accessible in Python</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">>></span><span style="font-size:12.8px">Could you explain your </span><span style="font-size:12.8px">use-case a little more ?</span></div><div><span style="font-size:12.8px">I have C++ code which is interfacing to a driver, from Python.</span></div><div><span style="font-size:12.8px">The user invokes a python function which calls my C++ code (via Boost). The C++ code makes a C++ structure, allocates memory for the ioctl call,</span></div><div><span style="font-size:12.8px">puts a pointer to the allocated memory into the C++ structure (as well as filling in other structure members), calls the ioctl and returns the success/failure</span></div><div><span style="font-size:12.8px">results to Python. The user needs a way to get access to the allocated memory, which was filled in by the ioctl call.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">>></span><span style="font-size:12.8px">f you really need to expose the memory as a buffer to Python, you may </span><span style="font-size:12.8px">want to use one of the newer C APIs such as</span></div><div><span style="font-size:12.8px">I'm sure there are many ways to do this, but I've already chosen boost for this project. Boost exists in order to</span></div><div><span style="font-size:12.8px">pass data back and forth between C++ & Python, there must be some way to do this</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The boost documentation is not very helpful, but I know many people have shared data between Python & C++ for </span></div><div><span style="font-size:12.8px">a long time this</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Thanks</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 12, 2016 at 10:14 AM, Stefan Seefeld <span dir="ltr"><<a href="mailto:stefan@seefeld.name" target="_blank">stefan@seefeld.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 12.01.2016 12:52, Tony Cappellini wrote:<br>
><br>
> Stefan,<br>
><br>
><br>
> To: <a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a> <mailto:<a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a>><br>
<span class="">> Subject: Re: [C++-sig] Passing memory allocated in C++ to Python<br>
> Message-ID: <<a href="mailto:56945036.8080302@seefeld.name">56945036.8080302@seefeld.name</a><br>
</span>> <mailto:<a href="mailto:56945036.8080302@seefeld.name">56945036.8080302@seefeld.name</a>>><br>
<span class="">> Content-Type: text/plain; charset=windows-1252<br>
><br>
> > Essentially- Python needs access to the return value from a malloc() call.<br>
><br>
> >>Really ? Why ? You say your C++ code needs to access the pointer(s).<br>
> >>Python has no notion of "pointer", but it will happily pass around<br>
> >>atever data you expose from C++.<br>
><br>
> Sorry about that Stefan, my post should have been clearer.<br>
><br>
> I know Python doesn't know about pointers.<br>
> I should have said "Python needs access to the memory pointed to by<br>
> the memory allocated with malloc()".<br>
><br>
><br>
> >>f you expose the above class together with the constructor and thei<br>
> >>'call_ioctl()' member function I think you have all you need.<br>
><br>
> In your something class, the data type returned from allocate_memory()<br>
> needs to be something that Python understands. Since that allocation<br>
> function (member) will be allocating 100s of MB of memory, how will<br>
> this memory map to a Python data type?<br>
<br>
</span>Does it have to be a Python (native) data type ? Could you explain your<br>
use-case a little more ?<br>
<span class=""><br>
><br>
> Is a bytearray better to use than a list (as far as performance is<br>
> concerned)? Is a list better to use than a string (as far as<br>
> performance is concerned). Are there other data types that should be<br>
> considered?<br>
<br>
</span>It all depends on what you intend to do with the memory.<br>
<span class=""><br>
><br>
> The user will need to access that data as bytes, words, longs, and<br>
> possibly sequences.<br>
><br>
> I've tried using boost's extract, to convert the char * returned from<br>
> malloc() into a Python string,<br>
> and a Python list, but these result in compile errors.<br>
<br>
</span>That's because the extract<>() logic is meant to be used for the<br>
inverse: to get access to C/C++ objects embedded into a Python object.<br>
Once you reflect your C++ type "something" to Python, boost.python will<br>
implicitly convert between the Python (wrapper) type and the C++<br>
"something" type for all function calls.<br>
However, sometimes you may still have a Python object, knowing that it<br>
actually contains a "something". extract<> is meant to give access to<br>
that (either by-value or by-reference, depending on your usage).<br>
<br>
If you really need to expose the memory as a buffer to Python, you may<br>
want to use one of the newer C APIs such as<br>
|PyMemoryView_FromMemory<br>
(<a href="https://docs.python.org/3/c-api/memoryview.html" rel="noreferrer" target="_blank">https://docs.python.org/3/c-api/memoryview.html</a>). Support for that<br>
isn't built into boost.python yet, so you'll have to add some glue code<br>
(e.g.<br>
<a href="http://stackoverflow.com/questions/23064407/expose-c-buffer-as-python-3-bytes" rel="noreferrer" target="_blank">http://stackoverflow.com/questions/23064407/expose-c-buffer-as-python-3-bytes</a>).<br>
This may actually be worth including into the library. We just haven't<br>
had anyone asking for such a feature - yet.<br>
|<br>
(I have used similar approaches in the past, where memory from a C++<br>
library I wrote is bound to a NumPy array, so the data can be accessed<br>
copy-free from other Python modules via the NumPy interface.)<br>
<br>
Regards,<br>
Stefan<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
<br>
...ich hab' noch einen Koffer in Berlin...<br>
<br>
</font></span></blockquote></div><br></div>