Python Forum
Multiple calls to Python interpreter embedded in C++ application yield segmentation f
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple calls to Python interpreter embedded in C++ application yield segmentation f
#1
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!
Reply


Messages In This Thread
Multiple calls to Python interpreter embedded in C++ application yield segmentation f - by mmoelle1 - Mar-21-2019, 08:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  How to run detectron2, as python embedded code in C++, on GPU? hassaniqbal931 3 1,039 Nov-02-2023, 04:45 PM
Last Post: blabling2
  yield usage as statement or expression akbarza 5 752 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 587 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  Adding libraries to embedded Python as a ZIP The_Oman 0 1,192 May-05-2023, 04:05 PM
Last Post: The_Oman
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Mac default python interpreter Viewpoint8455 2 874 Oct-13-2022, 06:25 AM
Last Post: perfringo
Bug Embedded Python Memory Leaks Alexei 1 1,001 Sep-16-2022, 01:15 PM
Last Post: Alexei
  Embedded Python in C++ Xeno 19 3,398 Aug-03-2022, 07:29 AM
Last Post: Gribouillis
  Python Interpreter ankitdixit 4 2,620 Jun-10-2022, 12:37 PM
Last Post: katy1234

Forum Jump:

User Panel Messages

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