return_addressof

Definition

Class return_addressof is a model of ResultConverterGenerator which can be used to wrap C++ functions returning any pointer, such that the pointer value is converted to unsigned int and it is copied into a new Python object.

This call policy was created to be used with ctypes package and provide access to some raw\low level data, without creating wrappers.

Pay attention: you have to manage the memory by your own.

Example

int* get_value(){
    static int buffer[] = { 0,1,2,3,4 };
    return buffer;
}

namespace bpl = boost::python;
BOOST_PYTHON_MODULE(my_module){
  def( "get_value"
       , bpl::return_value_policy< pyplusplus::call_policies::return_addressof<> >() );
}

The Py++ code is not that different from what you already know:

from pyplusplus import module_builder
from pyplusplus.module_builder import call_policies

mb = module_builder.module_builder_t( ... )
mb.free_function( return_type='float *' ).call_policies \
    = call_policies.return_value_policy( call_policies.return_addressof )

Python code:

import ctypes
import my_module

buffer_type = ctypes.c_int * 5
buffer = buffer_type.from_address( my_module.get_value() )
assert [0,1,2,3,4] == list( buffer )