[C++-sig] Boost Python and ctypes

Tristam MacDonald swiftcoder at gmail.com
Tue Feb 26 21:49:41 CET 2008


Thank you for the prompt answer - that accomplishes exactly what I was
trying to do!

I haven't had a lot of luck tracking down the hooks to add a manual
conversion for 'float *' into the types registry, do you have any reference
for that handy?

- Tristam

On 26/02/2008, Joseph Lisee <jlisee at gmail.com> wrote:
>
> I don't know how to integrate this into Boost.Python in a clean way, but
> here
> is a litter terminal example that can help:
>
> >>> array = (ctypes.c_int * 3)(1,2,3)
> >>> addr = ctypes.addressof(array)
> >>> arrayPtr = ctypes.cast(addr, ctypes.POINTER((ctypes.c_int)))
> >>> array[0]
> 1
> >>> arrayPtr[0]
> 1
>
> So basically if you can get a long value into python representing the
> address
> of your float array you are good to go.  This is done in Python-Ogre by
> just
> wrapping a manual cast function:
>
> unsigned long castToAddress(float* ptr)
> {
>     return (unsigned long)ptr;
> }
>
> A more permanent way to do this would be to change your wrapper to execute
> this code with Boost.Python's C++ python API interface (not tested):
>
> bp::object convertFloat(float* ptr)
> {
>     bp::object ctypes = bp::import("ctypes");
>
>     bp::long addr((unsigned addr)ptr);
>     bp::object pointerType(ctypes["POINTER"](ctypes["c_float"]));
>
>     return ctypes["cast"](addr, pointerType);
> }
>
> I believe you can do this by specifying a manual to python convert for the
> "float*" type.
>
> Hope that helps,
> Joseph Lisee
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080226/d60e22a7/attachment.htm>


More information about the Cplusplus-sig mailing list