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

How can I invoke python' class dunder method in C?

I'm writing some Python Module using the C extension API. And I want to implement insert() and is_greater() methods. both methods should take an argument of int type or any user-defined object which has implemented __gt__() method. To make it…
agongji
  • 117
  • 1
  • 7
0
votes
0 answers

Development of Python packages compatible with a wide range of Numpy versions

In a python extension using setup.py I have C code linked with the Numpy API. If I compile the project with a current Numpy version I cannot use the built extension with an older Numpy version (see…
Andreas
  • 159
  • 1
  • 7
0
votes
0 answers

python: setup.py: how NOT to install C extensions used only by tests

I have a module that has a tests/ directory which contains a C extension that's only used by test cases. I don't want to install it. I'm building it as a setuptools.Extension() added to setup() using the ext_modules argument. The test directory is…
0
votes
1 answer

How do I pass a variable through Python using C++ in Python.h

I wanted to try out embedding Python into C++. I was able to get that to work but I wanted to start writing prints with variables which are in declared in c++. For example: (C++) int num = 43; PyRun_SimpleString("print("+num+")"); char…
0
votes
1 answer

How to run a python program as if from command line in a C++ project

I'm writng a C++ audio plug-in that uses this python neural network to analyse the sentiment of the image: https://github.com/fabiocarrara/visual-sentiment-analysis. When using the python script as directed in the read.me from my terminal it runs…
MaxEllis9
  • 1
  • 1
0
votes
1 answer

How can I get a custom python type and avoid importing a python module every time a C function is called

I am writing some functions for a C extension module for python and need to import a module I wrote directly in python for access to a custom python type. I use PyImport_ImportModule() in the body of my C function, then PyObject_GetAttrString() on…
qbizzle68
  • 25
  • 6
0
votes
0 answers

How do I migrate this Python C-Extension using IO Kit from Python 2 to 3?

I am moderately familiar with Python, but I am a beginner to C, C-extensions and IO Kit. I have read the C-extensions section of "Supporting Python 3", but might still be misunderstanding something fundamental. I have a C-extension which works in…
79037662
  • 117
  • 5
0
votes
0 answers

Why is PyBytes_AsStringAndSize() writing the wrong size byte array?

I am working on a research project calling some python functions from C and am trying to return a 256-byte bytearray from a python script to my C program using the python/C API. I am trying to store the returned byte array as a char array in my C…
0
votes
0 answers

Recursive Datatypes w/ Numpy C-API?

I'd like to use the numpy c-api to write a datatype that's a tree structure, with pointers to children. I initially thought this possible without being a flexible datatype, since I don't need a variable number of direct children, and I'm fine with…
bockyboh
  • 11
  • 1
  • 2
0
votes
1 answer

How to construct a Python object that has an underlying non-contiguous buffer (as obtained by `PyObject_GetBuffer`)?

The Python C API includes a function, PyBuffer_IsContiguous for verifying if the returned buffer is contiguous. How to construct an object such as that function returns a false value? Here are a few examples that do not work (all tried with Python…
mmomtchev
  • 2,497
  • 1
  • 8
  • 23
0
votes
1 answer

How to use embeddable Python version in C++ project?

I am currently running some Python scripts in a Visual Studio C++ application using the Python/C api. The application is using Python that is installed on my system. However, I'd like to distribute the application without requiring the user to…
levente.nas
  • 83
  • 2
  • 10
0
votes
1 answer

Python-C-api, reference count and read access violation

I have a c++ piece a code, included in a larger native python project, that triggers various random read access violation. I suspect there is an issue with the handling of the reference count but I cannot figure it. The code features a C++ class…
m3shoot
  • 23
  • 3
0
votes
1 answer

what is the python C api function that returns a PyObject using the object name

I'm embedding Python into my C++ and creating PyObjects to represent my data/objects (ints, doubles, strings, etcetera). I've put in several hours trying to find the answer to the above question, I expected there'd be a "name" property or "name()"…
Danny Holstein
  • 144
  • 1
  • 14
0
votes
1 answer

PyUnicode_FromStringAndSize: Very terse documentation

Apologies if this is a stupid question which I suspect it may well be. I'm a Python user with little experience in C. According to the official Python docs (v3.10.6): PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) Return…
loopy walt
  • 868
  • 2
  • 6
0
votes
1 answer

Add TimeDelta to Date in Python C API

I have two objects: #include #include .... PyObject *first_date = PyDate_FromDate(2022, 08, 31); PyObject *time_delta = PyDelta_FromDSU(2, 0, 0); How can i subtract time_delta from first_date? As i found, there is…