[C++-sig] Python string -> const char * + size_t

Eric Hopper hopper at omnifarious.org
Fri Jun 10 23:26:09 CEST 2005


I'm trying to connect a hash function written in C++ to Python.  This
function will be processing a ton of data.  In Python, it's quite
natural to store random data in a string.  It's my impression that when
boost::python converts that string to a ::std::string it makes a copy of
it.  For this application, that will add a significant amount of CPU to
the time for processing the data, and that's important.

I would like instead to pass in pointers that point into the internal
data structure that Python is storing the string in.  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?

I'm willing to write a wrapper that has a slightly different (and
possibly boost::python specific) interface as long as the wrapper sucks
up very little CPU time.

I do want to manipulate Something objects in other ways in the Python
interpreter.

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/20050610/85eb8562/attachment.pgp>


More information about the Cplusplus-sig mailing list