Wim,
Excellent, thanks! Using the pickling for talking to C++ will save a lot of trouble, and I hadn't realized PyString_FromStringAndSize was part of ctypes -- very handy!
Best,
Andy
On 11/28/2011 02:45 PM, wlavrijsen_at_lbl.gov wrote:
> 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
Received on Tue Nov 29 2011 - 16:29:14 CET
This archive was generated by hypermail 2.2.0 : Tue Nov 29 2011 - 23:50:01 CET