Oct-25-2017, 02:00 PM
Oct-25-2017, 03:38 PM
It's up to you to explain your reasons for posting, not sending us off following links!
please state your problem, and a question on same.
please state your problem, and a question on same.
Oct-26-2017, 07:31 AM
when I call the python function outside the callback its working and same function when I call inside my c++ callback function its giving segmentation fault error.
In following files, I am not mentioning my c++ callback function, because it's complicated but believe me, its c++ callback function only, where I am calling " call_python("discoveryRES"); ", and I am getting segmentation fault error.
please give me some suggestion how to create the python object in c++ to avoid the segmentation fault error.
this is my request.py file
import sys
def discoveryRES(args):
sys.stdout.write('discovery Response completed !\n');
if args:
sys.stdout.write('{0}\n'.format(args))
this is my pythonobj.cpp file
void call_python(string kFuncName) {
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString("request");
// Load the module object
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, kFuncName.c_str());
if (pFunc && PyCallable_Check(pFunc)) {
PyObject* main_args = PyTuple_New(1);
PyObject* main_args_0 = PyString_FromString("Successfully_Completed");
PyTuple_SetItem(main_args, 0, main_args_0);
pValue = PyObject_CallObject(pFunc, main_args);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
printf("Cannot find function ");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
printf("Failed to load ");
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
Py_DECREF(pDict);
Py_DECREF(pFun);
// Finish the Python Interpreter
// Py_Finalize();
}
this is my pythonobj.h file
#include <Python.h>
#include <cassert>
#include <iostream>
using namespace std;
void call_python(string kFuncName);
This is my main.cpp file
#include pythonobj.h
int main(){
call_python("discoveryRES");
return 0;
}
i have tried both ways please go through the following link( point 1.6)
[1]
https://docs.python.org/2/extending/exte...ons-from-c
[2]Here the kFuncName will be the function wnat to call form request.py file
void call_python(string kFuncName) {
Py_Initialize();
assert(Py_IsInitialized());
const char* kModuleName = "request";
module_name = PyString_FromString(kModuleName);
module = PyImport_Import(module_name);
PyObject* dic = PyModule_GetDict(module);
// const char* kFuncName = "main";
PyObject* main_func = PyDict_GetItemString(dic, kFuncName.c_str());
assert(PyCallable_Check(main_func));
PyObject* main_args = PyTuple_New(1);
PyObject* main_args_0 = PyString_FromString("Successfully");
PyTuple_SetItem(main_args, 0, main_args_0);
PyObject_CallObject(main_func, main_args);
// raise Exception
PyObject_CallObject(main_func, NULL);
PyErr_Print();
Py_Finalize();
}
In following files, I am not mentioning my c++ callback function, because it's complicated but believe me, its c++ callback function only, where I am calling " call_python("discoveryRES"); ", and I am getting segmentation fault error.
please give me some suggestion how to create the python object in c++ to avoid the segmentation fault error.
this is my request.py file
import sys
def discoveryRES(args):
sys.stdout.write('discovery Response completed !\n');
if args:
sys.stdout.write('{0}\n'.format(args))
this is my pythonobj.cpp file
void call_python(string kFuncName) {
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString("request");
// Load the module object
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, kFuncName.c_str());
if (pFunc && PyCallable_Check(pFunc)) {
PyObject* main_args = PyTuple_New(1);
PyObject* main_args_0 = PyString_FromString("Successfully_Completed");
PyTuple_SetItem(main_args, 0, main_args_0);
pValue = PyObject_CallObject(pFunc, main_args);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
printf("Cannot find function ");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
printf("Failed to load ");
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
Py_DECREF(pDict);
Py_DECREF(pFun);
// Finish the Python Interpreter
// Py_Finalize();
}
this is my pythonobj.h file
#include <Python.h>
#include <cassert>
#include <iostream>
using namespace std;
void call_python(string kFuncName);
This is my main.cpp file
#include pythonobj.h
int main(){
call_python("discoveryRES");
return 0;
}
i have tried both ways please go through the following link( point 1.6)
[1]
https://docs.python.org/2/extending/exte...ons-from-c
[2]Here the kFuncName will be the function wnat to call form request.py file
void call_python(string kFuncName) {
Py_Initialize();
assert(Py_IsInitialized());
const char* kModuleName = "request";
module_name = PyString_FromString(kModuleName);
module = PyImport_Import(module_name);
PyObject* dic = PyModule_GetDict(module);
// const char* kFuncName = "main";
PyObject* main_func = PyDict_GetItemString(dic, kFuncName.c_str());
assert(PyCallable_Check(main_func));
PyObject* main_args = PyTuple_New(1);
PyObject* main_args_0 = PyString_FromString("Successfully");
PyTuple_SetItem(main_args, 0, main_args_0);
PyObject_CallObject(main_func, main_args);
// raise Exception
PyObject_CallObject(main_func, NULL);
PyErr_Print();
Py_Finalize();
}