Python Forum
calling python function in c++ callback getting segmentation fault error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling python function in c++ callback getting segmentation fault error
#1
Please check the below link I have explained all
https://stackoverflow.com/questions/4690...c-callback
Reply
#2
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.
Reply
#3
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();
}
Reply
#4
Please re-post using code tags, see BBCODE
also, when posting code, use shift-ctrl-v to preserve indentation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 517 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  calling external function with arguments Wimpy_Wellington 7 1,436 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 819 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  pyscript index error while calling input from html form pyscript_dude 2 980 May-21-2023, 08:17 AM
Last Post: snippsat
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,044 Sep-19-2022, 02:32 AM
Last Post: Xeno
  make: *** [Makefile:29: all] Segmentation fault Anldra12 2 1,881 May-01-2022, 06:17 PM
Last Post: Anldra12
Sad Iterate randint() multiple times when calling a function Jake123 2 2,048 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,815 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  [Solved] TypeError when calling function Laplace12 2 2,888 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Segmentation fault (core dumped) hobbyist 1 10,497 Jun-07-2021, 12:56 PM
Last Post: supuflounder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020