[C++-sig] Re: Python string -> const char * + size_t
Eric Hopper
hopper at omnifarious.org
Tue Jun 21 04:58:29 CEST 2005
Dave Abrahams wrote:
> Eric Hopper wrote:
>> The hash function is only going to read it and munge it a bit, so as
>> long as the data remains around for the duration of the call, it's
>> not a problem.
>>
>> I want to somehow pass a Python string to an interface something like:
>>
>> class Something {
>> public:
>> void hash_data(const char *data, size_t datalen);
>> };
>>
>> What is the easiest and cleanest way to make this happen using
>> boost::python?
>
> Well, if you need the length it's a little harder. Make a thin
> wrapper function that accepts a boost::python::str object x, then use
>
> x.ptr()
>
> to get the PyObject* and use the Python/C API to get the pointer and
> length.
That worked like a charm. I thought that might be the answer, but I was
a bit worried that ::boost::python::str made a copy. I'm glad to learn
that it doesn't. :-)
Thanks,
--
"It does me no injury for my neighbor to say there are twenty gods or no God.
It neither picks my pocket nor breaks my leg." --- Thomas Jefferson
"Go to Heaven for the climate, Hell for the company." -- Mark Twain
-- Eric Hopper (hopper at omnifarious.org http://www.omnifarious.org/~hopper) --
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050620/e3299448/attachment.pgp>
More information about the Cplusplus-sig
mailing list