Embedding: how to extend a C++ instance by Python methods

Renzo Tomaselli renzo.tomaselli at tecnotp.it
Mon Jul 8 10:13:57 EDT 2002


Gerhard Häring <gerhard.haering at gmx.de> wrote in message news:<slrnai93tv.l5.gerhard.haering at lilith.my-fqdn.de>...
> Renzo Tomaselli wrote in comp.lang.python:
> > Hi all,
> > my current goal is to extend a C++ instance behavior by a Python
> > method,
> 
> A C++ class that's exposed to Python, with one method delegating to a
> Python method defined in a .py file?

Correct, but though an undefined number of methods.
 
> > so that an explicit "execute(scriptName, args)" method on such
> > instance will end up into running a Python method defined by
> > scriptName.
> 
> Which execute() function is this? I found the rest of your post pretty
> confusing.
> 
> If all you want is call a Python callable (function/method/...), you
> can use PyObject_CallObject for this.
> 
> Else could you please try to rephrase your question a little?
> 
> Gerhard

Ok, I apologize for not begin clear. In short, I start from a C++
environment where I want to store Python scripts into a kind of
OODBMS.
There is a number of C++ objects I want to expose to Python, no
problems here.
Then I want to allow users to add their own behaviour to these
objects, say add new Python methods and run (execute) them on demand.
This is done both for overridding existing C++ methods and for adding
quite new methods.
So I need to implement a couple of extra C++ methods on my objects
such as:

add(name, functionBody)

where functionBodyis a string containing the declaration of a Python
function, to be run having the first arg bound to the invoking "self"
object. I expect this operation to add a new method to the current
object (e.g. its C-wrapper or a Python object derived from it) under
the specified name, so that in the future the user can ask for running
it through a second C++ method:

execute(name, argList)

while getting something back. Adding does some more tricks, like
storing the function body into my db, but this is not important here.
A key point is that I want users to be able defining just method
bodies, not to play around with entire modules. For this reason I
would like to implement the housekeeping details in C++ as much as
possible.
What I miss here is about creating callable objects from these stored
declarations, so that they can be added to instances and invoked on
the fly.
Any help will be appreciated,
                      Renzo Tomaselli      
---------------------------------------------------------------------------
TecnoTP s.n.c. Special Information System Design
Maso Pelauchi I38050 Ronchi Valsugana,  Trento TN  ITALY
e-mail: renzo.tomaselli at tecnotp.it   
---------------------------------------------------------------------------



More information about the Python-list mailing list