Re: getting bytes from c into pyroot

From: <wlavrijsen_at_lbl.gov>
Date: Mon, 28 Nov 2011 11:45:36 -0800


Andy,

> Thanks for the info. I wasn't aware of the pickling via TBufferFile...
> this is what I'm trying to do, but I assumed that pickle wouldn't be
> cross-language safe and these serialized objects are being sent to
> C++. From what you say, that this should be totally safe, though, right?

yes, as it's the contents of the TBufferFile::Buffer() that are used in full. In any case, the point is to be able to reconstruct the C++ objects after the pickling, so being able to return the results to C++ has been a requirement.

> Regarding the general case, what type would PyString_FromStringAndSize
> return? Wouldn't any string-like type get truncated at the first null
> byte?

You would not be able to use it as a normal string, no. It's a byte buffer and needs to be used as such. In RootModule.cxx there is the expand function that does the reverse of recreating the C++ object from the string and it gets the buffer size as an argument.

You'll find another example in e.g. Python/marshal.c which does the same thing, but then for marshalling instead of pickling.

> I was only successful with something like this:
>
> void Thinger(TBufferFile* buf, char* msg) {
> memcpy(msg, buf->Buffer(), buf->Length());
> }

Internally, PyString_FromStringAndSize uses a memcpy, too.

>>>> mv = memoryview(bytearray(buf.Length())).tobytes()
>>>> a = array.array('c', mv)
>>>> ROOT.Thinger(buf, a)
>
> i.e. passing-by-reference something pointerey (like an array.array)
> with a predetermined length from Python to be set in a C function...

Yes, because in the above, you now need a predefined block array. It is not possible to copy into the internal buffer of a python string object.

Note that PyString_FromStringAndSize is accessible through ctypes.

Best regards,

            Wim

-- 
WLavrijsen_at_lbl.gov    --    +1 (510) 486 6411    --    www.lavrijsen.net
Received on Mon Nov 28 2011 - 20:45:48 CET

This archive was generated by hypermail 2.2.0 : Tue Nov 29 2011 - 17:50:02 CET