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

Incorporate python code inside a C extension

I'm writing a Python module in C, but I would like the module to also contain functions written in Python. How can Python functions be added to a module written in C?
SU3
  • 5,064
  • 3
  • 35
  • 66
0
votes
1 answer

Failed to load c-extensions in a shiv package

I maintain a project which I bundle into a shiv package and distribute at work. I've included all necessary package dependencies into the shiv package, and this includes numpy and PIL. It is safe to assume everyone is using Windows and python…
0
votes
1 answer

PyArg_ParseTuple doesn't parse a string correctly

So I'm learning how to make Python modules in C and today I wanted to learn how to create a custom type. I wrote a module with one class named "Car" which stores a name, year and maxGas. Problem is, when I try to create an instance of this class in…
0
votes
1 answer

PyArg_Parse for multiple returns of python on c++

I am calling python from c++ using PyObject_CallObject as the python return only a floating point number, i can get it by: float output_of_python; PyObject *pValue,*pArgs; pValue = PyObject_CallObject(pFunc, pArgs); PyArg_Parse(pValue, "f",…
abcd
  • 11
  • 5
0
votes
1 answer

How to Link an additional c .lib file when making a cython library

I am attempting to make a simple python library that calls LogonUser to see if the plaintext password provided matches the password of the username provided, but when I compile, it gives me this error passcheck.obj : error LNK2001: unresolved…
0
votes
1 answer

Include header path to compile C python extension in build pipeline using cibuildwheels

I'm developing a python package that contains a C extension. I want to deploy a release for each operating system (linux, windows, macOS) using cibuildwheel. The C extension uses the arrayobject.h header (from numpy). In order to use this header…
Jorge Morgado
  • 1,148
  • 7
  • 23
0
votes
1 answer

Adding generator functionality to a Python/C class

I have built a class in C with the Python/C API. I now have a requirement to iterate over the data items in the C blob. Returning a PyList is undesirable as length is >50K. Is there a way to provide generator-like functionality? My one solution is…
Adrian
  • 1
0
votes
1 answer

Popen() doesn't redirect the stdout to a file

I have a python program that calls a subprocess using Popen(). This python program is called from a C program, using the Python/C API. Everything works fine except for the fact that the subprocess doesn't redirect the output to a file. If I use just…
user17263319
0
votes
1 answer

Python C API: What does PyList_Append do on error?

This is not documented in the docs. It just says it "returns 0 for success and -1 for error". What does this mean? If we're iterating over some data structure (say a linked list/array) and we're adding elements as we iterate, and suddenly…
Simon Farre
  • 71
  • 1
  • 6
0
votes
0 answers

python c api with multiple file

While writing python c api, I wanted to separate it into multiple files. So, I separated the files (module.cpp and support.cpp) and wrote the function declarations in the separate files (support.h). But how to write setup.py? ├── cpp │ ├──…
tsp
  • 61
  • 7
0
votes
0 answers

Cannot find directory for linker -lgomp

I am on an M1 Mac and I have Homebrew GCC installed. I am trying to multithread using OpenMP. I have a setup.py file containing: sources = ['pyTsetlinMachineParallel/ConvolutionalTsetlinMachine.c',…
0
votes
1 answer

OpenMP linker -lgomp library not found on M1 mac

My issue is with the openmp not working with a C Extension in a python setup.py file. I am running this code and I have a setup.py file as below: from setuptools import * libTM = Extension('libTM', sources =…
0
votes
2 answers

Can't run Python file in C

I have a problem with python api in c. I am trying to run a python script with PyRun_SimpleFile but it fails I get this error: d:/gcc/bin/../lib/gcc/x86_64-w64-mingw32/11.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe:…
0
votes
1 answer

PyPy C API compatibility issues

I'm trying to port an existing extension for cPython to PyPy. It is written using C API. I've got some question which are connected with the compatibility: The extension uses opcodes from cPython's opcode.h header file.…
Dmitrii
  • 88
  • 1
  • 7
0
votes
0 answers

python C API error: undefined reference to PyErr_Occurred

I'm working on a Python 2.7 project and I'm attempting to write a c++ extension to speed up our implementation of the D* lite algorithm. Never played with python C API, I'm struggling a lot in setting up my environment. To check whether I can access…
dc_Bita98
  • 851
  • 2
  • 17
  • 35