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
0 answers

How fast is PyObject_CallMethod compared to pure python?

I'm trying to optimize some python code by isolating part of the code into a C++ extension. However, since the code is heavily object-oriented, I'll need to set some attributes as PyObject*. My question is how fast is this compared to pure python…
qwerty_99
  • 640
  • 5
  • 20
1
vote
1 answer

C_Python not releasing buffer memory

I'm writing C code for python (Python C API), and I noticed that python is not releasing the memory of the file, I'm wondering if the issue is in my code. I want to simplify as much as passable, but I hope that no details will be missing. The file…
Rami Hassan
  • 149
  • 12
1
vote
1 answer

cast void* based on enum value in C++

I am writing a Python Library in C++ using the python C Api. There I have about 25 functions, that all accept two strings. Since Python might save strings in utf8/16/32 (the moment on char requires a bigger size the whole string will use the bigger…
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
1
vote
0 answers

Py_TYPE(self)->tp_free(self)

#include #include "structmember.h" typedef struct { PyObject_HEAD PyObject *first; /* first name */ PyObject *last; /* last name */ int number; } Noddy; static void Noddy_dealloc(Noddy* self) { …
yin
  • 97
  • 1
  • 4
1
vote
1 answer

Configuring Python C extensions for pip

I wrote a Python C extension, and it works great. Installing via python setup.py install works. However, pip cannot find my header files - so a pip installation doesn't work. > pip install Collecting jcalg1==1.0.1 Downloading…
Alex Osheter
  • 589
  • 5
  • 22
1
vote
1 answer

PyObject_CallObject is returning NULL when calling Show() or Save() function from Matplotlibcpp

I have integrated Matplotlibcpp in my C++ project from : https://github.com/lava/matplotlib-cpp I am using Clion to build by project and here's how I did the linkage: cmake_minimum_required(VERSION 3.13) project(beep) …
Khubaib Ahmad
  • 141
  • 10
1
vote
1 answer

Convert an iterable object to tuple in python C api

I have an iterable PyObject that I need to pass as the list of arguments to a Python callable, ala xs = [1,2,5,7] some_function(*xs) However, PyObject_CallObject only allows tuples to be passed as the arguments' container. I'm not sure, though,…
SU3
  • 5,064
  • 3
  • 35
  • 66
1
vote
0 answers

How to get all of the exceptions of Python3 C API?

I have a script named main_test.py, which has a syntax_error, is what I want to catch and show. f = open(path, "r+"# ) And I try to import this in C++, code looks like below, already set path: PyObject* pModule =…
Leeafay
  • 39
  • 8
1
vote
0 answers

Unable to PyImport_Import modules which imports modules

This is the first time I post a question here, so I hope, I am doing it correctly otherwise please let me know. I am trying to make a wrapper with c++ and call a python module. I am able to call a simple python module, however, as soon as I include…
Passbro
  • 11
  • 2
1
vote
0 answers

faulthandler.enable() in a library for production use?

I have a Python C Extension wrapped with a Python library. While testing, I've been using faulthandler.enable() in the Python library to debug segfaults in the C Extension. Now that I'm ready to put this code into production, should I remove this…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
1
vote
0 answers

Passing double arrays from Python to C++ extensions

I understand a basic C++ function wrapped for Python looks like this: int square(int n) { return n*n; } static PyObject* square_wrapper(PyObject* self, PyObject* args) { int n = 0; if(!PyArg_ParseTuple(args, "i", &n)) return…
1
vote
1 answer

Passing byte string from Python to C

I am writing a python extension in C and I am trying to pass a bytes object to my function. Obviously the 's' token is for strings; I have tried 'O', 'N', and a few others with no luck. Is there a token I can use to parse a bytes object? If not is…
Daniel
  • 547
  • 5
  • 25
1
vote
1 answer

PyImport_Import failing when submodules are imported in a python module

I have a cpp code like this: void callPython() { Py_Initialize(); PyObject* sysPath = PySys_GetObject("path"); PyList_Append(sysPath, PyUnicode_FromString("/jarvis_repo/src/cpp/packages/jarvis/nlp/")); // Load the module …
1
vote
1 answer

Python C Extension Exposing a Capsule to ctypes in order to use third party C code

I have a Python C Extension that wraps the library for a proprietary product. Our company has a large amount of C code that uses the proprietary product. Instead of rewriting this in Python using my C Extension, I figured I could simply return a…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
1
vote
1 answer

Embedding Python in a Qt Creator project

I'm working on a project that requires C++ to call a program written in Python that relies on Python exclusive modules. The project is handled using Qt Creator, and Python 3.7.5 and its packages are installed via Miniconda. I've gotten a basic…
brentnallt
  • 11
  • 3