2014-02-26 4 views
0

프로젝트로 돌아가서 파이썬 스크립트에서 오랜 시간 동안 돌아 오지 않을 것입니다. 재미있는 것은 몇 달 전에 나는 이제 내가 잘못 알고하지 않습니다,이 작업을 얻을 수 있었다이다C++에서 파이썬을 임베드 할 때 리턴 할 수 없음

는 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); 

간단한 파이썬 스크립트 :

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가 생성하고 "무엇"에 쓰여진 만이

는이 내가이 함수를 호출하는 방법입니다. FP_python_embed.txt는 또한 생성하고 문제가 드디어 해결책을 찾았

답변

0

가 도움을 사전에 감사 어떤 이유로 NULL을주고있다 유의 확률에 있어야하므로 "-1 worked1"을 가지고있다 . pValue를 문자열이 아니라 int로 구문 분석했습니다.

그래서, 내가 가지고 여기서

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

정말해야합니다 :

pValue = PyString_FromString(argv[i+3]); 
관련 문제