[C++-sig] passing wrapped c++ objects to boost wrapped function
Kim Branson
kim.branson at stanford.edu
Fri Jan 5 23:32:52 CET 2007
I'm attempting to extract the underlying OEMol object, from the
PySwigObject.
The extract<int> wont work, since i need to use more complex
functionality from the OEMol in the c++ classes i'm attempting to
pass the OEMol to.
I found some stuff on the wiki which indicated that the below might
work. I'm unclear on what exactly the type that finalBondIndex gets
passed.
I thought the code below would take a PySwigObject and return a
pointer of type OEChem::OEMol, and then my other code would then
get a reference to the OEChem::OEMol object that the PySwigObject
refers to?
Or is it more complicated than this? if a print a mol i get
<openeye.oechem.OEMol; proxy of C++ OEMolWrapper instance at
_a0386400_p_OEMolWrapper>, does that mean the type is a OEMolWrapper
instance and i should register that as a type?
cheers
Kim
++++++++++++++++++++++++++++++++++++++
#include "functions.h"
#include "python.h"
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
//#include "swig_arg.hpp"
using namespace boost::python;
typedef struct {
PyObject_HEAD
void *ptr;
const char *desc;
} PySwigObject;
void* extract_swig_wrapped_pointer(PyObject* obj)
{
char thisStr[] = "this";
//first we need to get the this attribute from the Python Object
if (!PyObject_HasAttrString(obj, thisStr))
return NULL;
PyObject* thisAttr = PyObject_GetAttrString(obj, thisStr);
if (thisAttr == NULL)
return NULL;
//This Python Object is a SWIG Wrapper and contains our pointer
return (((PySwigObject*)thisAttr)->ptr);
};
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 ;
};
BOOST_PYTHON_MODULE(test)
{
boost::python::converter::registry::insert
(&extract_swig_wrapped_pointer, type_id<OEMol>());
//BOOST_PYTHON_SWIG_ARG(OEMol)
def("finalBondIndex",finalBondIndex);
}
I run this like so
++++++++++++++++++++++++++++++++++++++
#!/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
numbonds =0
print "Call test.finalBondIndex with no arguments"
try:
numbonds = test.finalBondIndex()
except TypeError, e:
print "Caught exception ",e
print "Call test.finalBondIndex with OEMol argument"
try:
numbonds = test.finalBondIndex(mol.this)
print("Number of bonds is %i \n" %(numbonds))
except TypeError, e:
print "Caught exception ",e
and get the following result:
++++++++++++++++++++++++++++++++++++++
mol is <openeye.oechem.OEMol; proxy of C++ OEMolWrapper instance at
_a0386400_p_OEMolWrapper>
<class 'openeye.oechem.OEMol'>
loaded mol
loaded mol is <openeye.oechem.OEMol; proxy of C++ OEMolWrapper
instance at _a0386400_p_OEMolWrapper>
Call test.finalBondIndex with no arguments
Caught exception Python argument types in
test.finalBondIndex()
did not match C++ signature:
finalBondIndex(OEChem::OEMol {lvalue})
Call test.finalBondIndex with OEMol argument
Caught exception Python argument types in
test.finalBondIndex(PySwigObject)
did not match C++ signature:
finalBondIndex(OEChem::OEMol {lvalue})
On Jan 5, 2007, at 6:25 AM, Son, Serguei wrote:
> int numberOfAtoms(object mol)
> {
> int numAtoms = extract<int>(mol.attr("name")());
> return numAtoms;
> }
Dr Kim Branson, PhD
Stanford University, Stanford, CA, USA
Department of Chemistry, Group of Prof. Dr. V. Pande
Postdoctoral Fellow: Folding at home - distributed computing
Clark Center, 94305-5080 Stanford, CA, phone: (650) 723 1817
email kim.branson at stanford.edu
"I cook with wine, sometimes i even add it to the food" -- WC Fields
More information about the Cplusplus-sig
mailing list