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
0
votes
0 answers

PyList_New() causing segmentation fault "free():invalidpointer"

I'm trying to covert a 2D C-array to a 2D Python List. But I'm getting segmentation fault when the code tries to call PyList_New(). Size of rows and cols is usually fixed at 50. This happens at a specific index idxR=39. PyList_New() allocates…
dk619
  • 1
  • 1
0
votes
0 answers

How to handle a precompiled C-library API error in Python?

I am using GMSH, a C-library with a Python API. Sometimes, GMSH crashes and it forces my Python script to quit. However, I am doing more than one runs in a for loop and if one of them crashes, I would like to continue my for loop. Obviously, the…
0
votes
2 answers

Packaged Python C Extension is missing PyInit_module() in so File

I have created a C library libgac and then wrote Python module implementing bindings with ctype. I call the python module gazepy. My project file structure is as follows: pyproject.toml setup.py src gazepy gac # repo with the C…
moiri
  • 41
  • 5
0
votes
1 answer

Access application data when extending embedded Python

I am writing an application that embeds a Python interpreter. The application also extends Python with a custom module. This is done along the lines of the "Extending and Embedding the Python Interpreter" part of the Python documentation. In the…
Efternavn
  • 1
  • 1
0
votes
1 answer

Example for Py_BuildValue with O&

I am trying to find simple examples for Py_BuildValue with O& as an argument for reference. Could you please help with any examples or references if any? Especially the usage of the converter and the value we pass along with this. I tried a…
Maverickgugu
  • 767
  • 4
  • 13
  • 26
0
votes
0 answers

trained in python, test in C- is it possible?

I recently work on prediction model with python SVR module. Since I am simuatlion guy - I usually use simulation tool- it is easy to compute temperature with given property, but it is hard when you don't know its property - my situation is: I would…
JuneL
  • 11
  • 1
0
votes
1 answer

Memory Leak Passing Numpy to C on Linux

I've been experiencing a memory leak when calling some C code I wrote from Python 3.10.6 on Ubuntu 22.04.1 LTS. https://github.com/seung-lab/mapbuffer/blob/main/mapbufferaccel.c#L95-L110 When used in isolation, there is no memory leak, but the…
SapphireSun
  • 9,170
  • 11
  • 46
  • 59
0
votes
0 answers

Python C-API: Signal handling, specifically SIGABRT

Some python functions can raise the SIGABRT signal via os.abort(). I am currently embedding a python interpreter in my C++ code but I don't want the execution to crash when this happens. I tried it with a classical signal handling: #include…
0
votes
0 answers

Python C-API: Calling functions from the same module within functions

I am digging my way through the Python C-API and have problems understanding the scopes within a self created module. This code snipped explains my troubles: std::string temp…
0
votes
1 answer

VS Code Intellisense for Python C extensions

I am making a simple wrapper for a C/C++ library using Python C extensions and I am having problems with VS Code's intellisense. To simplify the problem, I pretty much have a the following files: wrapper.cc with the actual code // Function which…
0
votes
0 answers

How to start an additional python thread from C (worker thread) when the main python thread is executing

I have a C++ software that initializes a Python interpreter using the C-API and runs "scripts" (from files) on user demand. Additionally I have defined a number of "callback" functions using SWIG so the python scripts can access data and operations…
TeBe
  • 1
0
votes
0 answers

When using the Python/C API, should local C++ functions be declared as static?

When using the Python/C API to extend C++ to Python, should local C++ functions be declared as static? #include static PyObject* py_addNumbers(PyObject* self, PyObject* args) { int a, b; if (!PyArg_ParseTuple(args, "ii", &a, &b))…
一小木
  • 1
  • 1
0
votes
0 answers

Python C Extension: check if module attributes are read-only using introspection

I am currently trying to write a Python extension module using the Python C API. In some of my objects, I am defining read-only attributes using PyGetSetDef descriptors which look as follows: PyGetSetDef get_set_descriptors[] = { { …
Carmine B
  • 1
  • 1
0
votes
0 answers

How to launch a Python interpreter from a C extension

I have a C script, #include "main.h" static const char *path_here = "./path/to/here"; static PyConfig py_config; extern PyObject *fn_arg; void *init_Python(void *) { pthread_cleanup_push(del_Python, NULL); /* Set-up the Python…
0
votes
0 answers

Error while trying to test a basic scaffoling Python C API example

I am trying to run a basic example for the Python C API where I create a module with a function double_array that will double the values of a numpy array returning a new numpy array. test1.c #define NPY_NO_DEPRECATED_API…
M.E.
  • 4,955
  • 4
  • 49
  • 128