Re: getting bytes from c into pyroot

From: Andy Mastbaum <mastbaum_at_hep.upenn.edu>
Date: Fri, 25 Nov 2011 15:16:43 -0500


Wim,

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?

Regarding the general case, what type would PyString_FromStringAndSize return? Wouldn't any string-like type get truncated at the first null byte? I was only successful with something like this:

void Thinger(TBufferFile* buf, char* msg) {  memcpy(msg, buf->Buffer(), buf->Length()); }

>>> 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...

Thanks,
Andy

On 11/23/2011 05:54 PM, wlavrijsen_at_lbl.gov wrote:
> Andy,
>
>> The TString class solves this problem by providing a constructor
>> taking a char* and a size.
>
> as does the Python C-API string manipulation functions. Problem is that in
> the dictionary, there is nothing more on the 'char*' return that indicates
> it is a byte-buffer rather than a string (which is by far the more common
> case), and of course a length can not be had.
>
> What is the use case here? The reason I'm asking is that the pickling of
> ROOT objects works by serializing them into a TBufferFile, and then
> stuffing
> its Buffer() result into a python string, using the length.
>
>> Is there some wrapper function like that in PyROOT that I've
>> overlooked, or is there some other way to get handle arrays/strings
>> containing nulls?
>
> No, nothing of the kind exists (other than the pickling described
> above), so
> you'd have to add such a wrapper yourself, using something like:
>
> PyString_FromStringAndSize( buff->Buffer(), buff->Length() );
>
> Best regards,
> Wim
Received on Fri Nov 25 2011 - 21:16:51 CET

This archive was generated by hypermail 2.2.0 : Mon Nov 28 2011 - 23:50:01 CET