python-embedding

How can I check if a thread holds the GIL with sub-interpreters?

How can I check if a thread holds the GIL with sub-interpreters? Question: I am working on some changes to a library which embeds Python which require me to utilize sub-interpreters in order to support resetting the python state, while avoiding calling Py_Finalize (since calling Py_Initialize afterwards is a no-no). I am only somewhat familiar …

Total answers: 1

Why does PyImport_Import fail to load a module from the current directory?

Why does PyImport_Import fail to load a module from the current directory? Question: I’m trying to run the embedding example and I can’t load a module from the current working directory unless I explicitly add it to sys.path then it works: PyRun_SimpleString(“import sys”); PyRun_SimpleString(“sys.path.append(“.”)”); Shouldn’t Python look for modules in the current directory ? Edit1: …

Total answers: 4

How To catch python stdout in c++ code

How To catch python stdout in c++ code Question: 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) …

Total answers: 3

Create an object using Python's C API

Create an object using Python's C API Question: 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? Asked By: …

Total answers: 1

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

Calling a python method from C/C++, and extracting its return value Question: 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 …

Total answers: 10