python-c-api

How To catch python stdout in c++ code

房东的猫 提交于 2019-11-27 03:12:26
I have a program which during it's run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file. This is a declaration of the function pythonCallBackFunc(const char* pythonInput) My problem is to catch all the python output for a given command (pythonInput). I have no experience with python API and I don't know what is the right technique to do this. First thing I've tried is to redirect python's sdtout and stderr using Py_run_SimpleString this is some example of the code i've written. #include "boost

Python C API doesn't load module

半腔热情 提交于 2019-11-26 23:26:44
问题 I'm trying to load a python module that contains a math and numpy import in C, using the C API. I can load and run the module but, if I import the math module it doesn't work. I'm using Arch Linux, Python 2.7.2 and gcc. Here the codes: #include <stdio.h> #include <stdlib.h> #include <python2.7/Python.h> int main(int argc, char **argv) { PyObject *pName, *pModule, *pFunc, *pArg, *pDict, *pReturn, *pT1, *pT2, *pX, *pY; int i; double x, y; Py_Initialize(); PySys_SetPath("."); pName = PyString

Stopping embedded Python

岁酱吖の 提交于 2019-11-26 22:53:21
问题 I'm embedding Python interpreter to a C program. However, it might happen that while running some python script via PyRun_SimpleString() will run into infinite loop or execute for too long. Consider PyRun_SimpleString("while 1: pass"); In preventing the main program to block I thought I could run the interpreter in a thread. How do I stop executing the python script in embedded interpreter running in a thread without killing the whole process? Is it possible to pass an exception to the

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

无人久伴 提交于 2019-11-26 20:04:25
Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API calls are needed. The official Python documentation is unfortunately very ambiguous. There are already many questions on stackoverflow regarding this topic, and indeed, I've personally already asked a question almost identical to this one, so I won't be particularly surprised if this is closed as a duplicate; but consider that there seems to be no definitive answer to this question. (Sadly, I don't have Guido Van Rossum on speed-dial.) Firstly, let

How to pass flag to gcc in Python setup.py script?

天大地大妈咪最大 提交于 2019-11-26 16:40:52
问题 I'm writing a Python extension in C that requires the CoreFoundation framework (among other things). This compiles fine with: gcc -o foo foo.c -framework CoreFoundation -framework Python ("-framework" is an Apple-only gcc extension, but that's okay because I'm using their specific framework anyway) How do I tell setup.py to pass this flag to gcc? I tried this, but it doesn't seem to work (it compiles, but then complains of undefined symbols when I try to run it): from distutils.core import

Calling a python method from C/C++, and extracting its return value

随声附和 提交于 2019-11-26 15:04:32
I'd like to call a custom function that is defined in a python module from C. I have some preliminary code to do that, but it just prints the output to stdout. mytest.py import math def myabs(x): return math.fabs(x) test.cpp #include <Python.h> int main() { Py_Initialize(); PyRun_SimpleString("import sys; sys.path.append('.')"); PyRun_SimpleString("import mytest;"); PyRun_SimpleString("print mytest.myabs(2.0)"); Py_Finalize(); return 0; } How can I extract the return value into a C double and use it in C? As explained before, using PyRun_SimpleString seems to be a bad idea. You should

Create an object using Python&#39;s C API

独自空忆成欢 提交于 2019-11-26 13:01:35
问题 Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; ...and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // ... pyfoo_new, }; How do I create a new instance of pyfoo somewhere within my C extension? 回答1: Call PyObject_New(), followed by PyObject_Init(). EDIT: The best way is to call the class object, just like in Python itself: /* Pass two arguments, a string and an int. */ PyObject *argList = Py_BuildValue("si",

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

Deadly 提交于 2019-11-26 06:32:46
问题 Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API calls are needed. The official Python documentation is unfortunately very ambiguous. There are already many questions on stackoverflow regarding this topic, and indeed, I\'ve personally already asked a question almost identical to this one, so I won\'t be particularly surprised if this is closed as a duplicate; but consider that there seems to be

Calling a python method from C/C++, and extracting its return value

时光怂恿深爱的人放手 提交于 2019-11-26 03:49:37
问题 I\'d like to call a custom function that is defined in a python module from C. I have some preliminary code to do that, but it just prints the output to stdout. mytest.py import math def myabs(x): return math.fabs(x) test.cpp #include <Python.h> int main() { Py_Initialize(); PyRun_SimpleString(\"import sys; sys.path.append(\'.\')\"); PyRun_SimpleString(\"import mytest;\"); PyRun_SimpleString(\"print mytest.myabs(2.0)\"); Py_Finalize(); return 0; } How can I extract the return value into a C

fatal error: Python.h: No such file or directory

自古美人都是妖i 提交于 2019-11-25 22:27:47
问题 I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below: gcc -Wall utilsmodule.c -o Utilc After executing the command, I get this error message: utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated. in fact I have tried all the suggested solutions over the internet but the problem still exists ... also I have no problem with Python.h . I managed to locate the file on my machine .