在C ++中嵌入Python时无法返回

时间:2014-02-26 01:49:32

标签: python c++ embed

我回到了一个项目编程,现在我已经很长时间没有从python脚本返回了。 有趣的是,几个月前我设法让这个工作,现在我不知道什么是错的

C ++代码在哪里:

int CPythonPlugIn::py_embed(int argc, char *argv[]){

ofstream textfile3;
textfile3.open("FP_python_embed.txt");
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

if(argc<3){
    printf("Usage: exe_name python_source function_name\n");
    return 1;
}

//To inform the interpreter about paths to Python run-time libraries
Py_SetProgramName(argv[0]);

// Initialize the Python Interpreter
Py_Initialize();
if( !Py_IsInitialized() ){
    cout<<"Can't initialize"<<endl;
    return -1;
}

// Build the name object
pName = PyString_FromString(argv[1]);
if( !pName ){
    cout<<"Can't build the object "<<endl;
    return -1;
}

// Load the module object
pModule = PyImport_Import(pName);
if( !pModule ){
    cout<<"Can't import the module "<<endl;
    return -1;
}

// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);
if( !pDict ){
    cout<<"Can't get the dict"<<endl;
    return -1;
}


// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, argv[2]);
if( !pFunc || !PyCallable_Check(pFunc) ){
    cout<<"can't get the function"<<endl;
    return -1;
}

if (PyCallable_Check(pFunc)) 
{
    // Prepare the argument list for the call
    if( argc > 3 )
    {
            pArgs = PyTuple_New(argc - 3);
            for (int i = 0; i < argc - 3; i++)
            {
            pValue = PyInt_FromLong(atoi(argv[i + 3]));
                    if (!pValue)
                    {
                        PyErr_Print();
                        return 1;
                    }
                    PyTuple_SetItem(pArgs, i, pValue);    
            }

        pValue = PyObject_CallObject(pFunc, pArgs);
        textfile3<<PyInt_AsLong(pValue)<<endl<<" worked1";


        if (pArgs != NULL)
        {
            Py_DECREF(pArgs);
        }
    } 
    else
    {
        pValue = PyObject_CallObject(pFunc, NULL);
    }

    if (pValue != NULL) 
    {
        printf("Return of call : %d\n", PyInt_AsLong(pValue));
        textfile3<<PyInt_AsLong(pValue)<<endl<<" worked2";
        Py_DECREF(pValue);
    }
    else 
    {
        PyErr_Print();
    }
textfile3.close();
}

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Finalize();

return 0;

};

这就是我称之为这个功能的方式:

char *arg[4]={"PythonPlugIn2","bridge","test_callsign","MAH545"};
py_embed(4,arg);

简单的python脚本:

def test_callsign(b):
fp_txt=open('allomate.txt','w+')
fp_txt.write('WHAT')
fp_txt.write(b)
if b=='MAH545':
    fp_txt.write('MAHHH')
    fp_txt.close()
    return 1
elif b=='MAH544':
    fp_txt.close()
    return 2
elif b=='AFR545':
    fp_txt.close()
    return 3
else:
    fp_txt.write('MAHHH22')
    print 'No such airplane'
    fp_txt.close()
    return 10

创建了allomate.txt,它上面只写着“WHAT”。 FP_python_embed.txt也被创建并且具有“-1,working1”,所以问题需要在pValue上,由于某种原因它给出了NULL

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。我正在解析p值不是作为字符串而是作为int。

所以,我在哪里:

pValue = PyInt_FromLong(atoi(argv[i + 3]));

它应该是:

pValue = PyString_FromString(argv[i+3]);