read_ini/config/login.cfg
read_ini/script/readcfg.py
read_ini/src/pytest.cpp
遇到并解决的问题:
1,不要把脚本命名位test.py,test.py 是另外一个文件
import test
print test.__file__
2,如果PyImport_Import 脚本文件失败,可能是脚本内容本身有问题,应该首先确保python readcfg.py 执行没有问题。
待解决的问题:
1,
pReturn = PyObject_CallObject(pFunc, NULL);
int listSize = PyList_Size(pReturn);
char *pUserName = PyString_AsString(pReturn);
listSize为-1, 能够得到pUserName。
这样做,脚本函数只能返回一个值,与参考2不一样。
参考:
1,在调用python函数时两个API:
PyObject* PyObject_CallObject( PyObject *callable_object, PyObject *args)
已经获得了一个可以调用的python对象,比如一个函数,提供一个元组格式的参数调用,元组里面是按顺序的参数,apply(callable_object, args) 或者callable_object(*args)。
PyObject* PyObject_CallMethod( PyObject *o, char *method, char *format, ...)
调用一个对象里的一个函数,相当于python语法的o.method(args),其中的格式format可以参照文档里Py_BuildValue的参数。
2,脚本函数返回多个值:
[
Account
]
username = jason
password = 123
username = jason
password = 123
read_ini/script/readcfg.py
#
read .ini
import ConfigParser
def getUserName():
config = ConfigParser.ConfigParser()
config.readfp(open( ' ../config/login.cfg ' ))
userName = config.get( " Account " , " username " )
return userName
def getPassword():
config = ConfigParser.ConfigParser()
config.readfp(open( ' ../config/login.cfg ' ))
password = config.get( " Account " , " password " )
return password
# print getUserName()
import ConfigParser
def getUserName():
config = ConfigParser.ConfigParser()
config.readfp(open( ' ../config/login.cfg ' ))
userName = config.get( " Account " , " username " )
return userName
def getPassword():
config = ConfigParser.ConfigParser()
config.readfp(open( ' ../config/login.cfg ' ))
password = config.get( " Account " , " password " )
return password
# print getUserName()
read_ini/src/pytest.cpp
#include
"
Python.h
"
int main( int argc, char ** argv)
{
Py_Initialize();
if ( ! Py_IsInitialized())
{
return - 1 ;
}
PyRun_SimpleString( " import sys " );
PyRun_SimpleString( " sys.path.append('../script/') " );
PyObject * pName, * pModule, * pDict, * pFunc, * pReturn;
pName = PyString_FromString( " readcfg " );
pModule = PyImport_Import(pName);
if ( ! pModule)
{
printf( " Not find readcfg.py\n " );
return - 1 ;
}
pDict = PyModule_GetDict(pModule);
if ( ! pDict )
{
return - 1 ;
}
// to call getUserName
pFunc = PyDict_GetItemString(pDict, " getUserName " );
if ( ! pFunc || ! PyCallable_Check(pFunc))
{
printf( " Not find function [getUserName]\n " );
return - 1 ;
}
pReturn = PyObject_CallObject(pFunc, NULL);
char * pUserName = PyString_AsString(pReturn);
if ( ! pUserName)
{
printf( " Not get userName.\n " );
return - 1 ;
}
printf( " login:userName=%s\n " , pUserName);
// to call getPassword
pFunc = PyDict_GetItemString(pDict, " getPassword " );
if ( ! pFunc || ! PyCallable_Check(pFunc))
{
printf( " Not find function [getPassword]\n " );
return - 1 ;
}
pReturn = PyObject_CallObject(pFunc, NULL);
char * pPassword = PyString_AsString(pReturn);
if ( ! pPassword)
{
printf( " Not get password.\n " );
return - 1 ;
}
printf( " login:password=%s\n " , pPassword);
Py_DECREF(pName);
Py_DECREF(pReturn);
Py_DECREF(pModule);
Py_Finalize();
return 0 ;
}
int main( int argc, char ** argv)
{
Py_Initialize();
if ( ! Py_IsInitialized())
{
return - 1 ;
}
PyRun_SimpleString( " import sys " );
PyRun_SimpleString( " sys.path.append('../script/') " );
PyObject * pName, * pModule, * pDict, * pFunc, * pReturn;
pName = PyString_FromString( " readcfg " );
pModule = PyImport_Import(pName);
if ( ! pModule)
{
printf( " Not find readcfg.py\n " );
return - 1 ;
}
pDict = PyModule_GetDict(pModule);
if ( ! pDict )
{
return - 1 ;
}
// to call getUserName
pFunc = PyDict_GetItemString(pDict, " getUserName " );
if ( ! pFunc || ! PyCallable_Check(pFunc))
{
printf( " Not find function [getUserName]\n " );
return - 1 ;
}
pReturn = PyObject_CallObject(pFunc, NULL);
char * pUserName = PyString_AsString(pReturn);
if ( ! pUserName)
{
printf( " Not get userName.\n " );
return - 1 ;
}
printf( " login:userName=%s\n " , pUserName);
// to call getPassword
pFunc = PyDict_GetItemString(pDict, " getPassword " );
if ( ! pFunc || ! PyCallable_Check(pFunc))
{
printf( " Not find function [getPassword]\n " );
return - 1 ;
}
pReturn = PyObject_CallObject(pFunc, NULL);
char * pPassword = PyString_AsString(pReturn);
if ( ! pPassword)
{
printf( " Not get password.\n " );
return - 1 ;
}
printf( " login:password=%s\n " , pPassword);
Py_DECREF(pName);
Py_DECREF(pReturn);
Py_DECREF(pModule);
Py_Finalize();
return 0 ;
}
遇到并解决的问题:
1,不要把脚本命名位test.py,test.py 是另外一个文件
import test
print test.__file__
2,如果PyImport_Import 脚本文件失败,可能是脚本内容本身有问题,应该首先确保python readcfg.py 执行没有问题。
待解决的问题:
1,
pReturn = PyObject_CallObject(pFunc, NULL);
int listSize = PyList_Size(pReturn);
char *pUserName = PyString_AsString(pReturn);
listSize为-1, 能够得到pUserName。
这样做,脚本函数只能返回一个值,与参考2不一样。
参考:
1,在调用python函数时两个API:
PyObject* PyObject_CallObject( PyObject *callable_object, PyObject *args)
已经获得了一个可以调用的python对象,比如一个函数,提供一个元组格式的参数调用,元组里面是按顺序的参数,apply(callable_object, args) 或者callable_object(*args)。
PyObject* PyObject_CallMethod( PyObject *o, char *method, char *format, ...)
调用一个对象里的一个函数,相当于python语法的o.method(args),其中的格式format可以参照文档里Py_BuildValue的参数。
2,脚本函数返回多个值:
pArgs
=
PyTuple_New(
1
);
PyObject * reVal;
PyTuple_SetItem(pArgs,0,Py_BuildValue( " s " , " e:\\v " ));
// 函数调用
PyObject * pRetVal = PyEval_CallObject(pFunc, pArgs);
long temp1;
temp1 = PyList_Size(pRetVal);
// 解析返回值
for (int index = 0;index < temp1;index ++ )
{
// vSValue.
PyObject * tempPyObject = PyList_GetItem(pRetVal,index);
int flag = PyString_Check(tempPyObject);
vSValue.push_back(PyString_AsString(tempPyObject));
}
PyObject * reVal;
PyTuple_SetItem(pArgs,0,Py_BuildValue( " s " , " e:\\v " ));
// 函数调用
PyObject * pRetVal = PyEval_CallObject(pFunc, pArgs);
long temp1;
temp1 = PyList_Size(pRetVal);
// 解析返回值
for (int index = 0;index < temp1;index ++ )
{
// vSValue.
PyObject * tempPyObject = PyList_GetItem(pRetVal,index);
int flag = PyString_Check(tempPyObject);
vSValue.push_back(PyString_AsString(tempPyObject));
}