[C++-sig] from-python converter for aligned class

Jim Bosch talljimbo at gmail.com
Sun Feb 12 19:25:19 CET 2012


On 02/12/2012 12:49 PM, Václav Šmilauer wrote:
> I wrote a custom to-python converter for an aligned struct (It is a
> 128-bit aligned vector type from the http://eigen.tuxfamily.org
> library). I followed
> http://www.boost.org/doc/libs/1_39_0/libs/python/doc/v2/faq.html#custom_string),
> the converter looks like this:
>
> class AlignedType{ /*... */} __attribute__((aligned(16)));
> struct converter{
> converter(){
> py::converter::registry::push_back(&convertible,&construct,py::type_id<AlignedType>());
>
> static void* convertible(PyObject* obj_ptr){ /*...*/ }
> static void construct(PyObject* obj_ptr,
> py::converter::rvalue_from_python_stage1_data* data){
> void*
> storage=((py::converter::rvalue_from_python_storage<VT>*)(data))->storage.bytes;
>
> // !! this creates AlignedType instance at a possibly unaligned address !!
> new (storage) AlignedType;
> data->convertible=storage;
> }
> };
>
> I am getting crashes due to mis-alignment of the resulting object,
> because it is created at a possible unaligned address. Is there a way
> around this? Can I allocate another chunk of memory, or enforce the
> alignment before it is allocated?
>

Hmm.  You might be able to make this work with another class that holds 
the AlignedType and has an implicit conversion to it:

struct AlignedHolder {

     operator AlignedType const & () const {
         return *p;
     }

     std::auto_ptr<AlignedType> p;
};

You'd then write an rvalue converter for AlignedHolder, but register it 
with type_id<AlignedType>().  Boost.Python will see the type_id for 
AlignedType and try to use it when you have an "AlignedType const &" 
argument, but it will try to pass the AlignedHolder object to your 
function...at which point the implicit conversion will kick in.

I haven't tested this, and it involves really tricking Boost.Python, but 
I think it might work.

Jim


More information about the Cplusplus-sig mailing list