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

The python module i am importing in C++ with the Python/C API can’t import torch

OS: Windows 10 C++ environment was created in Visual Studio Community 2017 with CMake. I have a C++ file importing a Python module and calling a function from it, everything seems to properly work until i do an “import torch” in the Python file. **…
0
votes
0 answers

Segmentation fault when using dsent in GEM5

I have successfully executed the cmake and make command to build target in dsent of gem5 But it comes a segmentation fault as below when I execute import dsent Python ver: 3.10 Segfault happened at: 0x557878f5cc15: testb…
WITHER
  • 1
  • 1
0
votes
0 answers

"module has no attribute" in Python C module

I am trying to build this old project but ./setup.py build shows this warning (not sure if related, could be normal): WARNING: the following files are not recognized by DistUtilsExtra.auto: and when I run…
Alex P.
  • 3,697
  • 9
  • 45
  • 110
0
votes
1 answer

How to create a Python C extension module proposing multiple sub-modules?

I'm attempting to create some complex C extension module for Python. I have somewhat good results using the official documentation and can import my module with a syntax like import my_custom_module But I would also like to have submodules in my C…
Nicolas V
  • 31
  • 5
0
votes
0 answers

Serialization of PyObjects in Cpython

I am trying to write a PyObject in a file(in bytes) and then access it via other instance of Python interpretor. Although my main goal is to somehow have some PyObjects in a shared memory so that other python vm instance can access these objects in…
daniel
  • 35
  • 8
0
votes
1 answer

How to inherit one class implement by C

I want to add method for one class implemented by C from lru import LRU class A(LRU): def foo(self): print("a") lru is a lib from https://github.com/amitdev/lru-dict error:“type ‘type’ is not an acceptable base type”
Cherrymelon
  • 412
  • 2
  • 7
  • 17
0
votes
0 answers

Why dynamically loadable objects should not contain static variables?

I'm writing a Linux PAM module which embeds the Python interpreter using the Python/C API. This basically allows you to write PAM modules in Python. I'm following the The Linux-PAM Module Writers' Guide where in section 2 it says that The [PAM]…
TomasR
  • 45
  • 3
  • 4
0
votes
1 answer

Weird error of Python C Extension using Pthreads that RARELY happens

I am writing a Python C Extension for Graph-related computations. One routine computes the edge list of a certain class of Graphs and since this is "embarassingly" parallelizable, I rewrote it to use a Threadpool. At first I used OpenMP which was…
0
votes
0 answers

How to convert small integers in C with `PyLong_FromLong`?

I have been writing C code that I need to call from Python3. The C file compiles fine and I can import with ctypes CDLL command. Most functions work fine and I can call them from Python. I am using Python version 3.9.16. However, I am having…
zhanmusi
  • 161
  • 9
0
votes
0 answers

Including iostream with Python C API causes Python to error: DLL load failed while importing The specified module could not be found

I took a very simple Python C API example off the web. When I remove #include , I can properly import it into Python. When I keep iostream in and try to import the DLL, Python gives me this error: ImportError: DLL load failed while…
0
votes
0 answers

How to use PyArg_ParseTuple to get an unconverted PyObject

I am trying to pass a python object from python to a C module without converting it, but I get a weird error. I copied most of the code from an example in the python documentation and only changed the spam_system method in spammodule.cpp. I am…
0
votes
1 answer

Getting result of PyRun_String when python code returns an object

i have a problem with my code. i have a python file for the capturing of mavlink messages(i'm using pymavlink library) and i need to create a library for interfacing python results with c/c++ code. this is my python code from .py file from pymavlink…
MaryG
  • 3
  • 4
0
votes
0 answers

Objects missing when packaging Cython module with submodules

I'm attempting to build a Python C extension using Cython. The problem is that when I structure the project in submodules, the defined objects in Cython are no longer available. This is a simple project that I created to reproduce the issue I'm…
popeye
  • 81
  • 2
  • 5
0
votes
1 answer

PyArg_ParseTuple causes segfault when passing pointer instead of address

While learning Python C extension modules utilizing CPython's C API, I have encountered a curious segfault bug (disclaimer: I have only a passing fluency of C). A typical example module method, written in C (which can then be imported into python)…
Aaron
  • 1
  • 1
0
votes
0 answers

Application crashes using Py_DECREF on string PyObject in .dll built with Python C API

I am building a DLL to embed Python 2.7 (2.7.18 to be specific) in a larger application largely following python.org's C API documentation. When I believe I am applying Py_DECREF correctly to a PyString PyObject, any linking application crashes…