/*I am sorry the prev post is a mistake...*/
//the c++ code
using namespace boost::python;
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MO DULE(hello)
{
class_<World>(" World")
.def("greet", &World::gree t)
.def("set", &World::set)
;
}
int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize() ;
if (!Py_IsInitiali zed())
return -1;
inithello();
PyRun_SimpleStr ing("import sys");
PyRun_SimpleStr ing("sys.path.a ppend('./')");
PyObject* pName,*pModule, *pDict,*pFunc,* pArgs;
pName = PyString_FromSt ring("pytest");
pModule = PyImport_Import (pName);
if (!pModule)
return -1;
pDict = PyModule_GetDic t(pModule);
if (!pDict)
return -1;
pFunc = PyDict_GetItemS tring(pDict,"ad d");
if (!pFunc || !PyCallable_Che ck(pFunc))
return -1;
World w;
w.set("hi from main!");
pArgs = PyTuple_New(3);
PyTuple_SetItem (pArgs,0,Py_Bui ldValue("i",3)) ;
PyTuple_SetItem (pArgs,1,Py_Bui ldValue("i",4)) ;
PyTuple_SetItem (pArgs,2,Py_Bui ldValue("O",&w) );
try
{
PyObject_CallOb ject(pFunc,pArg s);
}
catch(error_alr eady_set)
{
// handle the exception in some way
}
Py_DECREF(pName );
Py_DECREF(pArgs );
Py_DECREF(pModu le);
Py_Finalize();
getchar();
return 0;
}
//the python's script
import hello
from hello import *
def add(a,b,t):
print "a + b = " + str(a+b)
w = hello.World()
w.set("hi from script!")
print w.greet()
print t.greet()
return
but the exception occur when run to "print t.greet()"
How can I solve it?
//the c++ code
using namespace boost::python;
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MO DULE(hello)
{
class_<World>(" World")
.def("greet", &World::gree t)
.def("set", &World::set)
;
}
int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize() ;
if (!Py_IsInitiali zed())
return -1;
inithello();
PyRun_SimpleStr ing("import sys");
PyRun_SimpleStr ing("sys.path.a ppend('./')");
PyObject* pName,*pModule, *pDict,*pFunc,* pArgs;
pName = PyString_FromSt ring("pytest");
pModule = PyImport_Import (pName);
if (!pModule)
return -1;
pDict = PyModule_GetDic t(pModule);
if (!pDict)
return -1;
pFunc = PyDict_GetItemS tring(pDict,"ad d");
if (!pFunc || !PyCallable_Che ck(pFunc))
return -1;
World w;
w.set("hi from main!");
pArgs = PyTuple_New(3);
PyTuple_SetItem (pArgs,0,Py_Bui ldValue("i",3)) ;
PyTuple_SetItem (pArgs,1,Py_Bui ldValue("i",4)) ;
PyTuple_SetItem (pArgs,2,Py_Bui ldValue("O",&w) );
try
{
PyObject_CallOb ject(pFunc,pArg s);
}
catch(error_alr eady_set)
{
// handle the exception in some way
}
Py_DECREF(pName );
Py_DECREF(pArgs );
Py_DECREF(pModu le);
Py_Finalize();
getchar();
return 0;
}
//the python's script
import hello
from hello import *
def add(a,b,t):
print "a + b = " + str(a+b)
w = hello.World()
w.set("hi from script!")
print w.greet()
print t.greet()
return
but the exception occur when run to "print t.greet()"
How can I solve it?
Comment