簡體   English   中英

在C ++中嵌入Python時無回報

[英]No return when embedding Python in C++

我要回到一個項目中進行編程,並且很長時間以來我都沒有從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,worked1”,因此問題必須出在pValue上,由於某種原因該值給出NULL

預先感謝您的幫助

我終於找到了解決方案。 我不是將pValue解析為字符串,而是解析為int。

所以,我在哪里:

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

確實應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM