python-c-api

Python C API crashes on 'import numpy' when initilizing multiple times

好久不见. 提交于 2021-02-11 12:13:30
问题 While working with the Python C API, I found that the python interpreter crashes when initializing it a second time and executing import numpy after each initilization. Any other command (e.g. import time ) will do just fine. #include <Python.h> int main(int argc, char ** argv) { while(1){ printf("Initializing python interpreter...\n"); Py_Initialize(); if(PyRun_SimpleString("import numpy")) { exit(1); } printf("Finalizing python interpreter...\n"); Py_Finalize(); } return 0; } The above

Return an array from python to C++

独自空忆成欢 提交于 2021-02-08 07:41:06
问题 I'm writing a c++ code to call a python function and the returned array from the python function will be store in an array in c++. I am able to call the python function in c++ but I am able to return only one value from Python to C++ and what I want to return is an array. Below is my C++ code: int main(int argc, char *argv[]) { int i; PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue; if (argc < 3) { printf("Usage: exe_name python_source function_name\n"); return 1; } // Initialize

Getting undefined reference when trying to build a pybind11 project using gcc

Deadly 提交于 2021-02-08 06:40:54
问题 I am trying to build a C++ static library in Linux (Ubuntu 18.04 in my case) using GCC using a Makefile . I noticed the issue is not with the makefile itself but the way I'm trying to compile and build with GCC. Before I explain a bit more on the GCC side, here is how my current project hierarchy looks like. The project uses Pybind11 header only library which resides in the External_Libraries directory. My class definition and implementation, naming Core.h and Core.cpp reside in Internal

binary using both Python C API version 2 and 3

混江龙づ霸主 提交于 2021-02-05 08:28:06
问题 In an open source project1 we have Python/Cython and C/C++ modules mixed with one C++ library using the Python C API. The API changed only a few function's names from 2 to 3. Assume the library is written without those functions. Will it link to Python3 if compiled with Python2, and vice versa? Is this prevented by macros in the API headers? Having a library binary that may link to both would spare us major packaging hassles. 回答1: No, it wouldn't work. Don't try it. Binary modules are not

Specifying Python function signature in C/API

与世无争的帅哥 提交于 2021-01-27 13:01:35
问题 When defining a function in pure Python, its signature is visible when calling help . For example: >>> def hello(name): ... """Greet somebody.""" ... print "Hello " + name ... >>> help(hello) Help on function hello in module __main__: hello(name) Greet somebody. >>> When defining a Python function in C/API, though, its signature lacks basic information: static PyObject* mod_hello(PyObject* self, PyObject* args) { const char* name; if (!PyArg_ParseTuple(args, "s", &name)) return NULL; printf(

How to list all function names of a Python module in C++?

倾然丶 夕夏残阳落幕 提交于 2021-01-27 04:43:09
问题 I have a C++ program, I want to import a Python module and list all function names in this module. How can I do it? I used the following code to get the dict from a module: PyDictObject* pDict = (PyDictObject*)PyModule_GetDict(pModule); But how to list the functions names? 回答1: Out of curiosity, I tried to puzzle this out. First, a minimal Python module testModule.py : def main(): print("in test.main()") def funcTest(): print("in test.funcTest()") Second, a minimal C++ sample testPyModule.cc

Python C Api transfer a PyObject * into c array

时间秒杀一切 提交于 2020-12-10 07:57:08
问题 I used python c api and wish to get an array back from python. I returned a python array from the python side and want to transfer the PyObject* result into a c array so I can use it. Is there anyway I can do that? Side questions: In what circumstance trying to return an element in the array like return arr[3] will cause the PyObject_callobject return NULL? return arr does give me something 来源: https://stackoverflow.com/questions/35141827/python-c-api-transfer-a-pyobject-into-c-array

True *args and **kwargs in Python C extension

狂风中的少年 提交于 2020-12-05 12:15:28
问题 I am developing Python 3 C extension. Can I get the equivalent or arbitrary positional or keyword arguments? For instance, in Python, I can write: def fun(name, parent, *args, **kwargs): # do something with name and parent # do something with args and kwargs pass But I cannot find of a simple equivalent in C. While we can perfectly write functions with PyObject* args and PyObject* kwargs , I cannot easily "parse out" name and parent from whichever (args/kwargs) it came. Take: static PyObject*