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

Why am I unable to access my Python function after importing its module in C++?

I am attempting to call a function print_stuff which is a member of the class Spam from a user-defined Python module Spam in C++ #define PY_SSIZE_T_CLEAN #include int main() { Py_Initialize(); //modify python module search path …
GWright
  • 13
  • 3
1
vote
0 answers

Python: Make `input` read and write to custom file descriptors

I want input(prompt) to: read from /dev/tty write its prompt to stderr I found this by searching around, but I could not find where readline_until_enter_or_signal is defined. It seems to only be defined in the C code, and inaccessible in normal…
HappyFace
  • 3,439
  • 2
  • 24
  • 43
1
vote
1 answer

PyLongObject memory leak

There is a memory leak in my encode method. static const size_t _bits_per_digit = sizeof(digit) * CHAR_BIT; /** * Returns a Python int storing the bitstream of the Elias gamma encoded input list. */ static PyObject * encode(PyObject *self,…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
1
vote
1 answer

Use PyObject_Realloc correctly

https://github.com/python/cpython/blob/master/Include/objimpl.h#L83 says PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory at p. PyObject_Realloc doesn't free the memory. I've noticed a memory leak in my program which uses…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
1
vote
0 answers

Handling fatal errors in Python extension modules

Does Python's C API have any means to directly return to the Python interpreter from an arbitrary point in the C code, and ideally raise an exception? Internally this would likely use some sort of longjmp. Use case: This would be used to handle…
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
1
vote
2 answers

How to construct a decimal.Decimal object using the C API?

I'm interested in creating decimal.Decimal objects in an extension in C. I cannot find any documentation about this in the docs Does documentation for this exist? Are there any examples anywhere? I've found the source code that implements the object…
Bryant
  • 3,011
  • 1
  • 18
  • 26
1
vote
2 answers

Python.h: No such file or directory on Amazon Linux Lambda Container

I am trying to build this public.ecr.aws/lambda/python:3.6 based Dockerfile with a requirements.txt file that contains some libraries that need gcc/g++ to build. I'm getting an error of a missing Python.h file despite the fact that I installed the…
mmachenry
  • 1,773
  • 3
  • 22
  • 38
1
vote
1 answer

How to annotate lines with code when running PyRun_String() in Python C API?

I'm using PyRun_String() from Python C API to run python code. Passing Py_file_input for start and for globals and locals I pass dictionary created with PyDict_New(), and for string of code str I pass my code. For example I'm having next code: def…
Arty
  • 14,883
  • 6
  • 36
  • 69
1
vote
2 answers

Using all CPU cores in Python C API

Is it possible in latest Python's C API to anyhow use all CPU cores? Because of GIL Python can use only one CPU core at a time, thus performance is low on multi-core machine. But C API has not-well-documented possibility to have several Interpreters…
Arty
  • 14,883
  • 6
  • 36
  • 69
1
vote
1 answer

Adding new command to module through C-API?

How would I dynamically add methods to my module through the C-API? I have many functions I need to register, and they are not in the same array. I assume I can initialize the module with a NULL method table, as the documentation says that's…
Juan
  • 3,667
  • 3
  • 28
  • 32
1
vote
1 answer

Python C API - PyDict - Do values and keys need to be reference counted?

I have some code that is using PyDict internally to store data. A user sets the data like you would with a normal python dictionary, and the data is stored by the C code in the PyDict object. My question is this: Do I need to reference count…
Bryant
  • 3,011
  • 1
  • 18
  • 26
1
vote
2 answers

Diffrence between instance variables and attributes in python?

So, the python docs for writing extension says this: "We want to expose our instance variables as attributes. There are a number of ways to do that. The simplest way is to define member definitions: static PyMemberDef Noddy_members[] = { …
theRealWorld
  • 1,188
  • 2
  • 8
  • 23
1
vote
1 answer

Python C extension packaging DLL along with pyd

My project exposes a static library (call it static.lib) to CPython (3.8) interpreter. It consists of a static library that is in turn dependent on a DLL FTDI driver. After reading this thread it appears that optimal solution to providing third…
keyermoond
  • 65
  • 7
1
vote
1 answer

Call Python function with *args and **kwargs from C API

I'm writing some C level function, it should accept a callable, *args and **kwargs, print something and call the callable with arguments. It's signature should be reflecting this one from Python: def my_function(callable, *args, **kwargs):…
Djent
  • 2,877
  • 10
  • 41
  • 66
1
vote
1 answer

How to pass C++ function as callback to Python?

I am using the Python 3 C API to interface with some Python code. I have instantiated a Python object from C++ using PyObject_Call and I have been able to modify some of its attributes using PyObject_SetAttrString. One the attributes I would like to…