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

Embedded Python: Multiple Sub-Interpreters not working

I'm trying to understand sub-interpreters and GIL. But my experiment is failing often(The same code rarely works). Gathering info from SO questions and few sites, I have the following code which spawns 2 non-python threads. Each of these threads are…
1
vote
1 answer

python pass to C-API structure with a pointer to another structure as an element

What is the best way to pass from python 3.6.9 to C-API struct I have a C library, which I'm trying to create a Python interface for it, but the library expects to initialize a struct beforehand, the main problem is that one element of a struct is a…
Rami Hassan
  • 149
  • 12
1
vote
0 answers

Does python multiprocessing call C++ destructors of static variables when the process exits?

Simplified Toy Scenario : NOTE : This example uses a singleton. One might justifiably question the wisdom in using that, however, for the purposes of this question I would appreciate it if we could accept the burdens of legacy and ignore that.…
XAR
  • 11
  • 3
1
vote
1 answer

C-Extension: Ref count of generators

I'm trying to get the Py_INCREF's and Py_DECREF's for my c extension right. Whilst doing so I've been stumbling about really high values for generators. What I've been doing is the following: // Forgive me for leaving out the NULL checks PyObject…
Nima Mousavi
  • 1,601
  • 2
  • 21
  • 30
1
vote
0 answers

Python C API - running all python threads in the main thread (or faking it)

I'm adding python scripting support to an application. This application has an API which is not thread safe, and I cannot change this aspect. One requirement I have is being able to run multiple independent scripts, thus I have to run…
fferri
  • 18,285
  • 5
  • 46
  • 95
1
vote
1 answer

Creating a PyMemoryView with a specific format

I am exposing a PyMemoryView in C on a set of data, like so: PyMemoryView_FromMemory((char *)ibuf->rect, pixels * sizeof(uint), PyBUF_WRITE); That data is floating-point data, however, so attempting to do this: mv = get_my_memory_view() mv[0] =…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
1
vote
1 answer

C-API: PySys_SetObject Reference Count

Does PySys_SetObject steal the reference to the object v or should I decrement its reference counter? PyObject *my_obj = PyUnicode_FromString("my_string_path"); int ret = PySys_SetObject("path", my_obj); Py_XDECREF(my_obj); // Should I decref…
dalmago
  • 106
  • 6
1
vote
0 answers

How to properly send a CuPy array to a Python C-extension module

I'm trying to write a custom CXX extension for Python which takes CuPy arrays as input. What's the recommended way to hand off and return CuPy arrays from a CXX extension? My current approach is to pass a memory pointer to the extension as an…
Daniel Ching
  • 443
  • 5
  • 9
1
vote
1 answer

Is it possible in pybind11 to use py::cast to access an abstract base class?

I have include a minimal working example below - it can be compiled using the typical pybind11 instructions (I use cmake). I have an abstract base class, Abstract, which is pure virtual. I can easily wrap this in pybind11 using a "trampoline class"…
wesanyer
  • 982
  • 1
  • 6
  • 27
1
vote
1 answer

Python CExtension gives ModuleNotFoundError

I am using Python 3 with Anaconda Spyder on CentOS 7. I am trying to get the Python CExtension working by following this video. https://www.youtube.com/watch?v=a65JdvOaygM I have all of my files in /home/peter/pythonCExtensions which is in the…
OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81
1
vote
3 answers

Python C-API Object Initialisation

What is the correct way to initialise a python object into already existing memory (like the inplace new in c++) I tried this code however it causes an access violation with a debug build because the _ob_prev and _ob_next are not set.. //PyVarObject…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
1
vote
1 answer

numpy nd array layout in memory

Transposing an ndarry does not change the actually layout in memory i.e. after transpose the ndarry is not 'C' order anymore I wrote a cpp extension for numpy tasks thus if the order the dimensions given are not in the order the cpp expects I use…
Hillel
  • 301
  • 1
  • 11
1
vote
1 answer

Import module after Py_Initialize

Today, I use PyImport_AppendInittab to append python modules built in C. PyImport_AppendInittab needs to be called before Py_Initialize. I can't finalize and then initialize the engine again. The problem is that now I need to append some modules…
João Paulo
  • 6,300
  • 4
  • 51
  • 80
1
vote
1 answer

Iterating over ndarray columns in C/C++

How would one get a view of a PyArrayObject* similar to the following python code? # n-column array x # d is the length of each column print(x.shape) # => (d, n) by_column = [x[::,i] for i in range(x.shape[1])] assert len(by_column) ==…
Martmists
  • 152
  • 1
  • 6
1
vote
2 answers

PyArg_ParseTupleAndKeywords throws warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

Search for examples on how to use the PyArg_ParseTupleAndKeywords I found these questions: How can one use PyArg_ParseTupleAndKeywords to parse a tuple with optional arguments as well as keywords? How does PyArg_ParseTupleAndKeywords work? They…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144