您的位置:首页 > 编程语言 > C语言/C++

c++调用python封装接口

2014-09-26 17:03 585 查看
xufangfa.h 文件

#include <iostream>

#include <Python.h>

#include <string>

#include <map>

using namespace std;

class CPyScriptRunner

{

public:
CPyScriptRunner()
{
Initialize();
}
~CPyScriptRunner()
{
Finalize();
}

public:
static bool CallEx(string FileName , string funcName , string fm = "" , void *returnVal = NULL, ...);

private:
static bool Initialize();
static bool Finalize();
static PyObject* LoadScript(string name);
//static map<string, PyObject*> m_mapScript;
//存取python字典的map
};

xufangfa.cpp 文件

#include <iostream>

#include "xufangfa.h"

#include <string>

using namespace std;

static CPyScriptRunner g;

bool CPyScriptRunner::Initialize()

{
Py_Initialize();
if( !Py_IsInitialized() )
{
return false;
}
int n = PyRun_SimpleString("import sys");
n = PyRun_SimpleString("import sys");
PyRun_SimpleString("print sys.path");
string strPath = "D:/testPy";
char *cPath = const_cast<char *>( strPath.c_str() );
string cmd = string("sys.path.append('");
cmd+=cPath;
cmd+="')";
int nRet = PyRun_SimpleString(cmd.c_str());
if( -1 == nRet)
{
return false;
}
return true;

}

bool CPyScriptRunner::Finalize()

{
if(Py_IsInitialized())
{
return true;
}
return false;

}

PyObject* CPyScriptRunner::LoadScript(string strFileName)

{
PyObject *pModuleDict = NULL;
PyObject *pScriptModule = NULL;
pScriptModule = PyImport_ImportModule(strFileName.c_str());
if( !pScriptModule )
{
return NULL;
}
pModuleDict = PyModule_GetDict(pScriptModule);
if(!pModuleDict)
{
return NULL;
}
//m_mapScript[strFileName] = pModuleDict;
return pModuleDict;

}

bool CPyScriptRunner::CallEx(string filename , string funcname , string fmt , void* returnVal, ...)

{
PyObject* pModuleDict = NULL;
//map<string , PyObject*>::iterator iter = m_mapScript.find(filename);
//if(iter == m_mapScript.end())
//{
pModuleDict = CPyScriptRunner::LoadScript(filename);
if( pModuleDict == NULL )
{
return false;
}

//else 
// pModuleDict = iter->second;
if( pModuleDict == NULL )
{
return false;
}
PyObject *pArgs =NULL;
int nParamCount = fmt.size();
if( nParamCount > 1 )
{
va_list marker;
va_start(marker , returnVal);
pArgs = PyTuple_New(nParamCount -1);
for( int i = 1 ; i < nParamCount ; ++i )
{
PyObject *pValue = Py_BuildValue(const_cast<char *>(fmt.substr(i , 1).c_str()) , va_arg(marker , PyObject*));
PyTuple_SetItem(pArgs , i-1 , pValue);
}
va_end(marker);
}
PyObject* pFunc = PyDict_GetItemString(pModuleDict , funcname.c_str());
if(!pFunc || !PyCallable_Check(pFunc))
{
Py_XDECREF(pArgs);
return false;
}
PyObject*pResult = PyObject_CallObject(pFunc , pArgs);
if( pResult )
{
if( nParamCount >= 1)
{
if( pResult != Py_None )
{
if( !PyArg_Parse(pResult ,const_cast<char*>(fmt.substr(0,1).c_str()),returnVal))
{

}
else
{

}
}
}
Py_XDECREF(pArgs);
return true;
}
else
{
Py_XDECREF(pArgs);
return false;
}
return true;

}

int main()

{
PyObject *pyNode = NULL ;
if(CPyScriptRunner::CallEx("test" , "testaa" , "O" ,pyNode  ))
{
int a = PyInt_AsLong(pyNode);
return a;
}
else
cout<<"没有输出成功!!!"<<endl;

}

/*
set_new_handler(nomorememory);
try
{
while(1)
{
new int[100000000];
cout<<"Allocating 100000000 ints"<<endl;
}
}
catch( exception e)
{
cout<<e.what()<<"xxx"<<endl;
}
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python