Python Forum

Full Version: Multiple calls to Python interpreter embedded in C++ application yield segmentation f
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Forum,

I have asked this question before at stackoverflow but did not get any answer.

Please consider the following mini application that creates a string containing QASM code in the host application and invokes the Python interpreter:

#include <Python.h>
#include <sstream>

static void run()
{
  std::stringstream ss;

  ss << "import pyquil\n";

  Py_Initialize();
  PyRun_SimpleString(ss.str().c_str());
  Py_Finalize();
}

int main()
{
  run();
  run();
  return 0;
}
Obviously, this code does not do anything useful but it yields a segmentation fault in the second call to the run() function when the pyquil module is imported. I have been using the same approach (multiple calls to embedded Python interpreter) with other Python modules and it works without problems.

The developers of pyquil consider this a Python problem and/or wrong usage of the embedded Python interpreter. Running the program through gdb yields
Error:
Program received signal SIGSEGV, Segmentation fault. 0x00007fffef7369e8 in PyArray_Item_INCREF (data=data@entry=0x7fffd9319ec0 "\n", descr=descr@entry=0x7fffefb692a0 <OBJECT_Descr>) at numpy/core/src/multiarray/refcount.c:39 39 numpy/core/src/multiarray/refcount.c: No such file or directory. Missing separate debuginfos, use: debuginfo-install libuuid-2.23.2-59.el7_6.1.x86_64 openssl-libs-1.0.2k-16.el7_6.1.x86_64 (gdb) where #0 0x00007fffef7369e8 in PyArray_Item_INCREF (data=data@entry=0x7fffd9319ec0 "\n", descr=descr@entry=0x7fffefb692a0 <OBJECT_Descr>) at numpy/core/src/multiarray/refcount.c:39 #1 0x00007fffef739f5b in PyArray_FromScalar (scalar=<optimized out>, outcode=outcode@entry=0x0) at numpy/core/src/multiarray/scalarapi.c:335 #2 0x00007fffef73b98c in gentype_nonzero_number (m1=<optimized out>) at numpy/core/src/multiarray/scalartypes.c.src:350 #3 0x00007ffff79abfc7 in PyObject_IsTrue (v=v@entry=0x7fffd9319eb0) at /usr/src/debug/Python-3.6.7/Objects/object.c:1243 #4 0x00007ffff7a1f981 in _PyEval_EvalFrameDefault (f=<optimized out>, throwflag=<optimized out>) at /usr/src/debug/Python-3.6.7/Python/ceval.c:2960 #5 0x00007ffff7a1d5e0 in _PyFunction_FastCall (co=<optimized out>, args=<optimized out>, nargs=2, globals=<optimized out>) at /usr/src/debug/Python-3.6.7/Python/ceval.c:4919 #6 0x00007ffff7a1e18e in fast_function (func=<optimized out>, stack=<optimized out>, nargs=<optimized out>, kwnames=<optimized out>) at /usr/src/debug/Python-3.6.7/Python/ceval.c:4961 #7 0x00007ffff7a1e2f6 in call_function (pp_stack=pp_stack@entry=0x7fffffff46f0, oparg=<optimized out>, kwnames=kwnames@entry=0x0) at /usr/src/debug/Python-3.6.7/Python/ceval.c:4858 #8 0x00007ffff7a224c4 in _PyEval_EvalFrameDefault (f=<optimized out>, throwflag=<optimized out>) at /usr/src/debug/Python-3.6.7/Python/ceval.c:3335 #9 0x00007ffff7a1d5e0 in _PyFunction_FastCall (co=co@entry=0x7fffda5a49c0, args=<optimized out>, args@entry=0x7fffffff4910, nargs=nargs@entry=2, globals=globals@entry=0x7fffd5c86ea0) at /usr/src/debug/Python-3.6.7/Python/ceval.c:4919 #10 0x00007ffff7a27576 in _PyFunction_FastCallDict (func=func@entry=0x7ffff7eedbf8, args=args@entry=0x7fffffff4910, nargs=2, kwargs=kwargs@entry=0x0) at /usr/src/debug/Python-3.6.7/Python/ceval.c:5021
Any help how to further debug this problem and decide whether its a problem with pyquil or my wrong usage of the embedded Python interpreter is highly appreciated. Thank you very much!