[C++-sig] passing wrapped c++ objects to boost wrapped function

Kim Branson kim.branson at gmail.com
Thu Jan 4 17:49:06 CET 2007


Hi all

heres the complete minimal example.

cheers
kim

#########test.cpp
#include "functions.cpp"
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(test)
{
     def("numberOfAtoms", numberOfAtoms);
     def("finalBondIndex",finalBondIndex);
}

######functions.cpp

/*
*  functions.cpp
*  OEMolTest
*/
#include "functions.h"

int numberOfAtoms(OEMol &mol)
{
     int numAtoms = 0 ;
     numAtoms = mol.NumAtoms();
     return numAtoms;
}



int  finalBondIndex(OEMol &mol)
{
     int numOfBonds = 0 ;
     int index =0 ;

     OEIter<OEBondBase> bond;
     for ( bond=mol.GetBonds(); bond; ++bond ) {
         index = bond->GetEndIdx();
         numOfBonds++;
     }
     return index ;
}
#########functions.h
/*
*  functions.h
*  OEMolTest
*/

# include "oechem.h"
# include "oesystem.h"
using namespace OEChem ;
using namespace OESystem;

int numberOfAtoms(OEMol &mol);
int finalBondIndex(OEMol &mol);



#########Heres the python test case.  openeye.oechem is  a wrapped  
lib that provides OEMol objects
#the same as the c++ code expects to be passed.


#!/usr/bin/python

from openeye.oechem import *
import test

mol = OEMol()

print "mol is ",mol
print type(mol)
ifs = oemolistream("ligand.mol2")
OEReadMolecule(ifs,mol)

print "loaded mol"

print "loaded mol is ",mol

numatoms =0
numbonds =0
print "Call tes.numberOfAtoms with no arguments"
try:
     numatoms = test.numberOfAtoms();
except:
     print "Caught exception "

print "Call test.finalBondIndex with no arguments"
try:
     numbonds = test.finalBondIndex()
except:
     print "Caught exception "



print "Call test.numberOfAtoms with OEMol argument"

numatoms = test.numberOfAtoms(mol);

print("Number of atoms is %i  \n" %(numatoms))

#ligandObject = ligand.Ligand(mol)


#####Jamfile
# This is the top of our own project tree
project-root ;

#   Include definitions needed for Python modules
import python ;

extension test                     # Declare a Python extension  
called test
:   test.cpp                       # source
     # requirements and dependencies for Boost.Python extensions
     <template>@boost/libs/python/build/extension
:    <include>"/usr/local/openeye/include"
:    <library-path>"/usr/local/openeye/lib"
     ;


On Jan 4, 2007, at 5:29 AM, Roman Yakovenko wrote:
> On 1/4/07, Kim Branson <kim.branson at gmail.com> wrote:
>> Hi,
>>
>> i'm trying to glue together some c++ code using python as an  
>> intermediate
>> layer.
>>
>> i have python bindings for a library, and i can create python wrapped
>> instances of this object in python
>>
>> mol = OEMol()
>> in python this is a proxy of the C++ instance.
>>  <openeye.oechem.OEMol; proxy of C++ OEMolWrapper instance at
>> _0030dfd0_p_OEMolWrapper>
>>
>> I have other c++ code  that uses an OEMol interface like so.
>>
>> int numberOfAtoms(OEMol &mol)
>> {
>>     int numAtoms = 0 ;
>>     numAtoms = mol.NumAtoms();
>>     return numAtoms;
>> }
>>
>> i created a simple wrapper for it.
>>
>> #include "functions.cpp"
>> #include <boost/python/module.hpp>
>> #include <boost/python/def.hpp>
>> using namespace boost::python;
>>
>> BOOST_PYTHON_MODULE(test)
>> {
>>     def("numberOfAtoms", numberOfAtoms);
>>     def("finalBondIndex",finalBondIndex);
>> }
>>
>> trying to pass a mol object into it from python produces the  
>> following
>> result.
>>
>> Traceback (most recent call last):
>>   File "functionsTest.py", line 35, in ?
>>     numatoms = test.numberOfAtoms(mol);
>> Boost.Python.ArgumentError: Python argument types in
>>     test.numberOfAtoms(OEMol)
>> did not match C++ signature:
>>     numberOfAtoms(OEChem::OEMol {lvalue})
>>
>> I assume this is failing because the boost wrapped function does  
>> not get
>> OEChem::OEMol, but the python object PyObject?
>>
>> How can i wrap the function to solve this?   I'm unsure on what  
>> exactly is
>> being passed in.
>>
>
> Can you post complete minimal example?
>
>
> -- 
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list