Questions tagged [python-c-api]

API used by C and C++ programmers who want to write extension modules or embed Python.

The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. This is probably the most common use. The second reason is to use Python as a component in a larger application; this technique is generally referred to as embedding Python in an application.

Writing an extension module is a relatively well-understood process, where a “cookbook” approach works well. There are several tools that automate the process to some extent. While people have embedded Python in other applications since its early existence, the process of embedding Python is less straightforward than writing an extension.

Many API functions are useful independent of whether you’re embedding or extending Python; moreover, most applications that embed Python will need to provide a custom extension as well, so it’s probably a good idea to become familiar with writing an extension before attempting to embed Python in a real application.

Reference: http://docs.python.org/c-api/intro.html

1096 questions
1
vote
1 answer

Importing python file to c++ program - ModuleNotFoundError

Following this thread Assume I have the following files in the directory prog: main.cpp (located in the directory prog) int main(int argc, char *argv[]){ Py_Initialize(); //Adding current path PyObject* sys =…
linuxbeginner
  • 39
  • 1
  • 7
1
vote
1 answer

How to clone a python class object? (not the instance but the class itself)

Imagine you have the following code: class A: pass NewA = ... # copy A NewA.__init__ = decorator(A.__init__) # but don't change A's init function, just NewA's I am looking for a way to change some of the attributes/methods in the cloned class…
1
vote
0 answers

[cgo]Python C API Error: undefined reference to 'Py_True'

Environment: Ubuntu20.04,Golang 1.9.2,Python 3.7.10 I am trying to use this package: https://github.com/DataDog/go-python3 The problem happened when 'go get github.com/DataDog/go-python3' was executed. By the way, I have noticed that there is a…
WJT
  • 11
  • 2
1
vote
1 answer

Python C API, send a python function pointer to c and execute it

I want to create a function in python, pass it's function pointer to c and execute it there. So my python file: import ctypes import example def tester_print(): print("Hello") my_function_ptr =…
Turgut
  • 711
  • 3
  • 25
1
vote
1 answer

Python C++ API make member private

I'm making a python extension module using my C++ code and I've made a struct that I use to pass my C++ variables. I want some of those variables to be inaccessible from the python level. How can I do that? typedef struct { …
Turgut
  • 711
  • 3
  • 25
1
vote
0 answers

How to transform a python (long) integer to a boost multiprecision int with C API

I want to transform a python integer to a boost multi precision cpp_int in C++, to be able to work with integers of arbitrary size. My code is entirely in C++, so I need to do this using Python API for C, working with PyObject. I am currently doing…
Vlad Keel
  • 372
  • 2
  • 13
1
vote
1 answer

Parse C source code and extract variables and methods from it

I want to parse c source code for extracting variables and functions from it. Is there any library available for this purpose? I tried to achieve it through query language available in tree-sitter parser generator but when I run the program it says…
Ansa
  • 11
  • 2
1
vote
0 answers

Clean dependencies for setup of a Python package with C++ extensions using Torch

I have inherited a Python package with Torch-powered C++ extensions and I am trying to find my way around its installation and build process. I am a bit out of my depth, I haven't worked with compiled extensions before. Currently, its setup.py…
Anaphory
  • 6,045
  • 4
  • 37
  • 68
1
vote
0 answers

Why is my python method call duration periodic and not evenly distributed?

I'm running a python script from C++. I first set up the python script (load module and instantiate class), then call one of the methods of the class N (~1000000) times and measure how much time each method call took using…
Alqio
  • 452
  • 1
  • 5
  • 15
1
vote
1 answer

Exception: access violation reading when calling a C function from Python

I wanted to play around a bit with the Python C Api. But ran into an error. OSError: exception: access violation writing 0x0000000000000020 The error occurs on the lines PyObject_RichCompare(first, second, Py_LT) There are no errors in the first…
Massa7ca
  • 11
  • 2
1
vote
1 answer

Embedded Python: fatal error in sub-interpreter with C++ multi-thread when cleanup

I'm trying to use Sub-interpreter for having distinct environment, and having multi-thread on same environment(interpreter). However, when I tried to cleanup sub-interpreter, Python raise Fatal error with message: Py_EndInterpreter: thread still…
1
vote
1 answer

PyObject_CallObject crashed when called out of main function scope

I'm building a simple module to wrap a C function. The main function of this module (test_wrapper) basically receives a python function and call it: #include static PyObject* test_wrapper(PyObject* self, PyObject* args) { PyObject*…
João Paulo
  • 6,300
  • 4
  • 51
  • 80
1
vote
0 answers

Python C API segmentation fault when launching python soft twice

I'm trying to run a python code from a C code twice but I get a segfault the second time. Here is my c code: //prgm.c #include #include "Python.h" int main(void) { int i; Py_Initialize(); /* add . to the path */ PyObject*…
1
vote
1 answer

Cibuildwheel Fails to Compile With Static Libraries

I have a Python C extension module which relies on static libraries. Below is my file tree, I haven't included all the files because I am trying to simplify the problem. folder/ ├── src/ | ├── main.c | └── other.c ├── include/ | ├──…
GreyHope
  • 102
  • 1
  • 11
1
vote
1 answer

Operate on Numpy array from C extension without memory copy

I'm new to C extensions for NumPy and I'm wondering if the following workflow is possible. Pre-allocate an array in NumPy Pass this array to a C extension Modify array data in-place in C Use the updated array in Python with standard NumPy…