[C++-sig] Boost.Python at runtime

David Abrahams dave at boost-consulting.com
Wed Aug 17 23:09:57 CEST 2005


Allen Bierbaum <allenb at vrsource.org> writes:

> Has anyone every used or even tried to use Boost.Python to dynamically 
> create python types at runtime (instead of the standard compile time 
> method)?
>
> I am working on an very large extensible C++ class library that has a 
> reflective interface that I can extend as much as I need.  So instead of 
> trying to create an enormous wrapper at compile time (that would be 
> quickly out of date) I have been investigating the possibility of 
> dynamically creating the python class/type wrappers on demand. 
>
> So for example if I discovered at run-time that I wanted a wrapper for a 
> class Hello I would:
>
> - Ask Hello's reflective interface for information about the class and 
> it's bases
> - Create an associated boost.python class object
> - For each method Hello reports
>    - add method to the wrapper class using signature and method info 
> from Hello
>
> I would also need to handle registering associated smart pointers, etc.  
> The details are definitely complex, but before I get to far into it I 
> was wondering if anyone had done anything similar or had tried and could 
> tell me how it went or if it is impossible with the current system.

Boost.Python is not exactly designed to do that, and what you're
asking isn't possible to do in portable C++ without knowing all of the
classes and member function signatures that you might want to wrap at
compile-time anyway.  

However, see
http://aspn.activestate.com/ASPN/Mail/Message/cpp-sig/2781474

You can of course generate Boost.Python wrapping code dynamically and
compile it into extension modules that are then loaded into your
running Python program :)

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list