python-embedding

Does the Python 3 interpreter leak memory when embedded?

眉间皱痕 提交于 2019-12-03 16:55:31
This bug report states that the Python interpreter, as of June 2007, will not clean up all allocated memory after calling Py_Finalize in a C/C++ application with an embedded Python interpreter. It was recommended to call Py_Finalize once at application termination. This bug report states that as of version 3.3 and March 2011 the interpreter still leaks memory. Does anyone know the current state of this issue? I am concerned because I have an application in which the interpreter is called multiple times per running instance and I am experiencing memory leaks. I am already using boost::python to

PyObject_CallMethod with keyword arguments

半城伤御伤魂 提交于 2019-12-03 13:46:06
I'm trying to embed a Python (2.7) library in my C application and I'm using the Python/C API to call Python code from C. I need to call a Python method that takes keyword arguments. Semantically, I'm trying to achieve the equivalent of the following line in Python: myobject.dosomething('blahdy blah', somearg=True) By reading the documentation , I have managed to get as far as the following, but this doesn't pass in the keyword arguments: PyObject_CallMethod(myobject, "dosomething", "s", "blahdy blah"); I'm not super familiar with Python and I'm kind of stuck at this point as the documentation

Embedding a matplotlib chart into Qt/C++ application

孤街醉人 提交于 2019-12-03 12:58:25
I am developing an math-oriented GUI application in Qt/C++ and would like to embed a Python scripting, including NumPy and Matplotlib. Using Python C API, I finally managed to run a script, retrieve the values from the Python variables, including NumPy arrays etc. But I failed at drawing Matplotlib charts into my Qt/C++ application. It is better to say, I have managed to save the chart's RGBA buffer to a variable using Python script, then get the value of the variable as a PyObject of buffer type, get the buffer and transform it to QImage then to QPixmap and finally put it into QLabel and

Embedding Python into C - importing modules

孤街醉人 提交于 2019-12-03 08:16:44
I am having problems using the Embedded Python for C as per the Documentation - Whenever I try using imported modules I get an : Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation reading location 0x00000004. The error occurs in the PyObject_GetAttrString() method and the documentation isn't much help. I have also tried using tutorials as in an Example from IBM, but always get the same access violation. The following is the example code from one of the tutorials which I can't seem to get to work, what is wrong here? C-Code (in one main file): #include <Python.h>

Deadlock when QThread tries to acquire Python GIL via PyGILState_Ensure()

偶尔善良 提交于 2019-12-02 19:22:28
问题 I have a C++/Qt application in which I want to embed the Python interpreter. I want to call Python from a QThread, but I'm getting a deadlock at the line where I call PyGILState_Ensure() in order to try to acquire the global interpreter lock (GIL). I'll provide a minimal and straight-forward example below, which follows the recommendations given here: //main.cpp: #include <QCoreApplication> #include <QThread> #include "Worker.h" void startThread() { QThread* thread = new QThread; Worker*

Deadlock when QThread tries to acquire Python GIL via PyGILState_Ensure()

我只是一个虾纸丫 提交于 2019-12-02 09:58:40
I have a C++/Qt application in which I want to embed the Python interpreter. I want to call Python from a QThread, but I'm getting a deadlock at the line where I call PyGILState_Ensure() in order to try to acquire the global interpreter lock (GIL). I'll provide a minimal and straight-forward example below, which follows the recommendations given here : //main.cpp: #include <QCoreApplication> #include <QThread> #include "Worker.h" void startThread() { QThread* thread = new QThread; Worker* worker = new Worker(); worker->moveToThread(thread); QObject::connect(thread, SIGNAL(started()), worker,

ImportError and PyExc_SystemError while embedding Python Script within C for PAM modules (.so files)

删除回忆录丶 提交于 2019-12-01 23:06:58
问题 I'm trying to write a demo PAM module in C, which uses Embedding Python in C concept to run a script written in python (2.7), inside pam_sm_authenticate() function, which is written in C file (pam_auth.c). This is the python script: test.py import math import numpy def test_func(): a = "test" return a The path for test.py is /usr/lib/Python2.7/ so that I can easily import it. This is the C file: #define PAM_SM_AUTH #define PAM_SM_ACCOUNT #define PAM_SM_SESSION #include <security/pam_modules.h

(Python C API) PyRun_StringFlags missing builtin functions?

好久不见. 提交于 2019-11-30 18:29:41
I am trying to embed some python in my pet project. I have reduced my problem to the following code: #include <Python.h> #include "iostream" int main(int argc, char *argv[]) { Py_Initialize(); PyObject *globals = Py_BuildValue("{}"); PyObject *locals = Py_BuildValue("{}"); PyObject *string_result = PyRun_StringFlags( "a=5\n" "s='hello'\n" "d=dict()\n" , Py_file_input, globals, locals, NULL); if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();return 1;} return 0; } (I know I'm not cleaning up any references. This is an example.) it can be compiled by c++ $(python-config --includes) $(python

embedding python

自古美人都是妖i 提交于 2019-11-30 15:14:21
问题 Im trying to call python functions from C code, and i followed a sample from here I also have the correct include file directries, library directries, and linked the python32.lib (im using python 32) however the error was that python/C APIs such as PyString_FromString, PyInt_FromLong, PyInt_AsLong are undefined (error in the debugger) this is strange because im also using other APIs, but they're all fine... whats the problem here?? int main(int argc, char *argv[]) { PyObject *pName, *pModule,

Numpy import fails on multiarray extension library when called from embedded Python within a C++ application

爱⌒轻易说出口 提交于 2019-11-30 07:01:37
I'm running a C++ application which tries to run python using the https://docs.python.org/3.5/extending/embedding.html function calls. This is the error that the application error message pipes are giving me. class 'ImportError': Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try git clean -xdf (removes all files not under version control). Otherwise reinstall numpy. Original error was: /usr/local/lib/python3.5/site-packages/numpy/core/multiarray.cpython-35m-x86_64-linux-gnu.so: