[C++-sig] Efficient string passing
Mohan Ganesalingam
mg262 at cam.ac.uk
Thu Nov 19 13:30:16 CET 2009
Dear all,
I recently sent an e-mail which (I think) didn't reach the list, about
trying to return large strings from C++ into Python without copying them.
By looking at the C/Python API I've subsequently figured out roughly how to
do this... something like
Class World
{
object return_object()
{
PyObject *result = PyString_FromStringAndSize(0, 7);
// initialise the underlying string using PyString_AsString
handle<> h(result);
return object();
}
...
main_namespace["World"] = class_<World>("World")
.def("return_object", &World::return_object);
Unfortunately this doesn't work; when I call w.return_object(), I get back
None. Returning a raw PyObject * instead of a boost::python::object works,
but I can't tell if that involves a copy... and using
return_value_policy<manage_new_object>() with a PyObject * return crashes.
Any advice here would be very much appreciated!
Thank you and best wishes,
Mohan
More information about the Cplusplus-sig
mailing list