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

Python C-API and PyRun_File: Using "import functools and partial(print, flush=True)"

First a short background: (1) When running this program in Python3.6 I need to wait until it ends for the result to be displayed: import time for i in range(5): print(i, end=" ") time.sleep(1) (2) This program on the other hand…
palle
  • 11
  • 1
0
votes
1 answer

Trying to load pyd with LoadLibraryEx and got failed

I have a pyd pytest.pyd, where declared two functions: say_hello and add_numbers. So I want to load this pyd dynamically in C++ with LoadLibraryEx. But, when I try to call initpytest func, it fails. const char* funcname = "initpytest"; HINSTANCE…
kolwot
  • 3
  • 2
0
votes
0 answers

Is there Python C API equivalent for `float.as_integer_ratio` method?

In plain Python we can write something like >>> (0.5).as_integer_ratio() (1, 2) Is there a way to get this result through Python C API? The only thing I see is an implementation.
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
0
votes
0 answers

Why removing Py_Finalize() from the following code makes it work?

Context and formulation of a question Hello everyone. I have a cpp program that calls python script multiple times in a loop. Everything works fine unless I add Py_Finalize() at the end of the function. In that case at the second iteration of the…
trofchik
  • 87
  • 1
  • 7
0
votes
1 answer

segmentation fault python main.py

I wrote a c++ module witch should be imported into Python. Below are both Codes, the C++ part and the Python part. The C++ function method_sum should return the double of a value to python. module.cpp: #define PY_SSIZE_T_CLEAN #include…
Tillmann
  • 61
  • 1
  • 5
0
votes
0 answers

Why should we almost always reassign an object members before decrementing their reference counts?

I was reading through this tutorial in order to learn how to write C extension module to define new types. At some point the following tp_init implementation was given: static int Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) {    …
sifadil
  • 23
  • 1
  • 4
0
votes
0 answers

Time efficiency python class Vs extension type in C

I am looking to create a custom scalar type that has magnitude and a bunch of sign bits. I have defined operations such as arithmetic and comparison. I want to use this scalar type with numpy extensively. (Eg., Normal operations would involve…
0
votes
0 answers

Nullptr error in Simple Python/C API Test

I'm getting a nullptr error trying to use the Python/C++ API. Most of these errors come in rather long/complicated code where someone tries to reference a pointer without setting it to something first, but I'm able to duplicate my behavior on just a…
0
votes
1 answer

Can we create an .exe file that uses Python calls without having Python installed in the target machine?

I have this C code i took from https://docs.python.org/3/extending/embedding.html: #define PY_SSIZE_T_CLEAN #include int main(int argc, char *argv[]) { wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { …
0
votes
1 answer

How to store PyObject* correctly in C?

I am writing a small library for caching. Python dict doesn't suit me, already tried std::map, got SIGSEGV with quite similar errors. Anyway, the whole point is described in logs below. What am I doing wrong? Is there some other way to store objects…
h3xcode
  • 42
  • 8
0
votes
1 answer

Passing list of lists (or array of arrays) from python to C using swig

I've been searching a while but not able to find an answer even if the problem is pretty simple. In python I declare a list of N places whose elements are list itself: list_of_list = [] for i in range(N): list_of_list.append([]) I then want to…
Marcella
  • 21
  • 3
0
votes
1 answer

Creating a basic PyTupleObject using Python's C API

I'm having difficulty with creating a PyTupleObject using the Python C api. #include "Python.h" int main() { int err; Py_ssize_t size = 2; PyObject *the_tuple = PyTuple_New(size); // this line crashes the program if (!the_tuple) …
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
0
votes
2 answers

Using embedded python, loading a *.pyd outside of DLLs directory fails

I have a C++ application (X-Plane) for which there is a plugin which permits the use of python scripts (XPPython3 plugin). The plugin is written in C, using python CAPI, and works great, allowing me to write python scripts which get executed within…
pbuck
  • 4,291
  • 2
  • 24
  • 36
0
votes
1 answer

using data from PyObjects in Python C Extension without holding the GIL

In my Python C Extension I am performing actions on an iterable of strings. So in a first step I call PySequence_Fast to convert it to a list and then iterate over the elements. For each string I use PyUnicode_DATA and then compare the strings using…
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
0
votes
1 answer

Is PyRun_SimpleString() function slow?

I have C++ app where it does many calculations, but the most of time it needs to execute simple scripts by PyRun_SimpleString() so I'm curious if is there something in Python/C API, what would do faster following tasks: do simple assignents like x…
IzZy
  • 364
  • 3
  • 16