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

NumPy C-API: convert type object to type number

The function PyObject* PyArray_TypeObjectFromType(int); converts the type number for a NumPy scalar type (NPY_BOOL, NPY_BYTE, ...) to the corresponding type object. How do you do the opposite conversion, from the type object for a NumPy scalar type…
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
7
votes
1 answer

embedding python

Im trying to call python functions from C code, and i followed a sample from here I also have the correct include file directries, library directries, and linked the python32.lib (im using python 32) however the error was that python/C APIs such as…
PeterG
  • 519
  • 2
  • 7
  • 15
7
votes
2 answers

is it possible to overwrite "self" to point to another object inside self.method in python?

class Wrapper(object): def __init__(self, o): # get wrapped object and do something with it self.o = o def fun(self, *args, **kwargs): self = self.o # here want to swap # or some low level C api like #…
pprzemek
  • 2,455
  • 3
  • 25
  • 25
7
votes
0 answers

Isolated Sub-Interpreters in Python without GIL

There are PEP-554 and PEP-684. Both are designed to support multiple interpreters on Thread level. Does anyone know if these PEPs are implemented somewhere at least in experimental or pre-release versions of Python like 3.11? I found out that Python…
Arty
  • 14,883
  • 6
  • 36
  • 69
7
votes
2 answers

Embedding Python into C - importing modules

I am having problems using the Embedded Python for C as per the Documentation - Whenever I try using imported modules I get an : Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation reading location 0x00000004. The…
Shorkaa
  • 123
  • 3
  • 5
7
votes
2 answers

Can you safely change a Python object's type in a C extension?

Question Suppose that I have implemented two Python types using the C extension API and that the types are identical (same data layouts/C struct) with the exception of their names and a few methods. Assuming that all methods respect the data layout,…
nben
  • 646
  • 5
  • 20
7
votes
1 answer

Including and distributing third party libraries with a Python C extension

I'm building a C Python extension which makes use of a "third party" library— in this case, one that I've built using a separate build process and toolchain. Call this library libplumbus.dylib. Directory structure would be: grumbo/ include/ …
trbabb
  • 1,894
  • 18
  • 35
7
votes
3 answers

C extensions - how to redirect printf to a python logger?

I have a simple C-extension(see example below) that sometimes prints using the printf function. I'm looking for a way to wrap the calls to the function from that C-extensions so that all those printfs will be redirected to my python…
Bob Sacamano
  • 699
  • 15
  • 39
7
votes
1 answer

Python C API: How to check if an object is an instance of a type

I want to check if an object is an instance of a certain class. In Python I can do this with isinstance(obj, cls). In C/C++, I found a function named PyObject_IsInstance. But it seems not to work like isinstance. In detail (also described as sample…
Cosmo
  • 836
  • 1
  • 12
  • 27
7
votes
1 answer

A full and minimal example for a class (not method) with Python C Extension?

Everywhere, I can easily find an example about writing a method with Python C Extensions and use it in Python. Like this one: Python 3 extension example $ python3 >>> import hello >>> hello.hello_world() Hello, world! >>> hello.hello('world') Hello,…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
7
votes
2 answers

Python: Generate function stubs from C module

I made a Python module in C/C++ with Python C API. I use setuptools.Extension in my setup.py. It creates one .py file which loads a python module from some compiled .pyd file: def __bootstrap__(): global __bootstrap__, __loader__, __file__ …
Lorin
  • 137
  • 1
  • 7
7
votes
2 answers

Get Python's LIB path

I can see that INCLUDE path is sysconfig.get_path('include'). But I don't see any similar value for LIB. NumPy outright hardcodes it as os.path.join(sys.prefix, "libs") in Windows and get_config_var('LIBDIR') (not documented and missing in Windows)…
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
7
votes
1 answer

Embedded python code in c++ - error when importing python libraries

I am trying to use Python 3.5 interpreter embedded in a C++ program to receive an image from C++, and use it as an input for my trained tensorflow model. First, I convert my image to numpy array and then send it to python. This is my simplified code…
Matin H
  • 870
  • 8
  • 16
7
votes
3 answers

How can I assert from Python C code?

I'm writing a Python class in C and I want to put assertions in my debug code. assert.h suits me fine. This only gets put in debug compiles so there's no chance of an assert failure impacting a user of the Python code*. I'm trying to divide my…
Joe
  • 46,419
  • 33
  • 155
  • 245
7
votes
1 answer

What are the implications of calling NumPy's C API functions from multiple threads?

This is risky business, and I understand the Global Interpreter Lock to be a formidable foe of parallelism. However, if I'm using NumPy's C API (specifically the PyArray_DATA macro on a NumPy array), are there potential consequences to invoking it…
ide
  • 19,942
  • 5
  • 64
  • 106