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
2
votes
1 answer

Check if object is iterable in python c extension

I need to write a function that does different things depending on whether a python argument is iterable or not. Here's a code example: PyObject *iter = PyObject_GetIter(arg); // arg is a PyObject* if (iter) { // do iterable things } else { //…
SU3
  • 5,064
  • 3
  • 35
  • 66
2
votes
3 answers

Preventing one of many independent Python objects from decrefing to 0 and destroying the C pointer upon which the other Python objects depend

I'm working with a legacy C library that I've wrapped with a Python C Extension. The C library has a recursive data structure Foo with an API similar to below: Foo *Foo_create(void) /* Create new Foo memory */ int Foo_push(Foo *parent, int field,…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
2
votes
1 answer

Can't resize a tuple using ctypes.pythonapi

Only for testing, I tried to resize a tuple using ctypes, with terrible results: Python 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from ctypes import…
Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
2
votes
0 answers

Python C Extension - How to map a Python string to a C Macro Constant?

I'm wrapping a legacy C library. There are a few header files that contains thousands of macro constants like: #define FOO 1 #define BAR 2 #define BAZ 3 I would like for the user in Python to be able to specify the string "FOO", which will then be…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
2
votes
1 answer

Subdirectory structure breaks C++ extension build

I can't seem to get around a problem where importing a C++ extension module no longer works when a subdirectory structure is used. The two cases below present a simple working case and a slightly altered case that I cannot for the life of me get to…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
2
votes
1 answer

Member assignment and ref counting in __init__

I'm writing a C extension for Python and going through the documentation, I'm having a hard time understanding member assignment in the __init__ function. So there, in section 2.2, member assignment is done as follow: if (first) { tmp =…
StinGer
  • 76
  • 1
  • 5
2
votes
2 answers

INCREF needed when returning argument from Python C Extension function?

This question is pretty simple but will help cement my understanding. I know that arguments to C extension functions are guaranteed to be live references for the duration of the C code (unless manually DECREFed). However, if I have a C extension…
Michael Carilli
  • 371
  • 2
  • 12
2
votes
0 answers

Link against another python C extension

I have two python extensions written in C: foo.bar.libbar defined in foo/bar/bar.c foo.baz.libbaz defined in foo/baz/baz.c libbar depends on symbols from libbaz, namely a non-python-exported function called doubleint. The setup.py for the library…
reox
  • 5,036
  • 11
  • 53
  • 98
2
votes
1 answer

What does 'PYVERNODOTS' in Python C-extension modules mean?

When I want to compile Python C-extension modules on Linux, the file name of the compiled modules is for example .cpython-@PYVERNODOTS@m-x86_64-linux-gnu.so. However this only happens, when I compile for Python 3.7, in Python 3.6 the…
Padix Key
  • 857
  • 6
  • 15
2
votes
1 answer

C-Numpy: How to create fixed-width ndarray of strings from existing data

I'm writing a Python extension module in C++ with Boost Python. I want to return numpy arrays from the module to Python. It works well with numeric datatypes like double but at one point I need to create a string array from existing data. For…
Konstantin
  • 33
  • 5
2
votes
1 answer

NumPy C API extension causes excessive memory usage

I have written a C extension to NumPy to speed up some of my calculations, but I am getting increasing memory usage as I call the function repeatedly. I have trimmed down the function into a minimum example: PyObject* memory_test_function(PyObject*…
Anger Density
  • 332
  • 1
  • 3
  • 17
2
votes
0 answers

embedded python hangs on import site-package

I'm trying to embed python in a c/c++ application, using the embedded python zip file. I also want to use site-packages, like numpy. I manipulate the sys path myself, so it'll include all paths necessary. What I have found out for now is: If I use…
wickermoon
  • 171
  • 1
  • 10
2
votes
2 answers

C Python API Extensions is ignoring open(errors="ignore") and keeps throwing the encoding exception anyways

Given a file /myfiles/file_with_invalid_encoding.txt with invalid UTF8 as: parse this correctly Føö»BÃ¥r also parse this correctly I am using the builtin Python open function from the C API as follows the minimal example (excluding C Python setup…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
2
votes
1 answer

VSCode Itellisense with python C extension module (petsc4py)

I'm currently using a python module called petsc4py (https://pypi.org/project/petsc4py/). My main issue is that none of the typical intellisense features seems to work with this module. I'm guessing it might have something to do with it being a C…
Miguel
  • 1,293
  • 1
  • 13
  • 30
2
votes
1 answer

Python C api - function overloading

I have a number of C functions that accept different arguments, e.g. foo_i(int a) foo_c(char c) Is it possible to overload these functions in python C api? I tried to use the following methods table: static PyMethodDef test_methods[] = { …
PintoDoido
  • 1,011
  • 16
  • 35