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

How to write a python string to a C struct's (char *) member in python?

I have a C struct passed from C to python, and I want to change the content of a (char *)member in python. little.c #include typedef struct little littleStruct; struct little{ char *memberStr; PyObject…
Jack
  • 55
  • 2
  • 7
1
vote
0 answers

Is it safe to call `Py_DecRef` on an object created before the last Py_Initialize?

Assuming all participating modules correctly handle Py_Finalize, does C API allow this: Py_Initialize(); ... PyObject* myObj = PyObject_Call(simple_class, empty_tuple, NULL); Py_Finalize(); ... Py_Initialize(); Py_DecRef(myObj); // <--- is this safe…
LOST
  • 2,956
  • 3
  • 25
  • 40
1
vote
1 answer

My Python program crashes with "Fatal Python error: deallocating None". What does this error mean?

I've been trying to track down an issue with some Python code compiled from C++ (using swig). The following information was retrieved by executing gdb using python3-dbg (python version 3.8). Python crashes with this error: Fatal Python error:…
Tails86
  • 507
  • 5
  • 19
1
vote
1 answer

Are "in place" Cython functions possible?

Suppose I have the C function which returns void void mysub_wrapper(int *a, int *b) { *b = *a + 1; } Can I make a wrapper with Cython which passes a and b by reference: cdef extern void mysub_wrapper(int *a, int *b) def mysub(int a, int b): …
nicholaswogan
  • 631
  • 6
  • 13
1
vote
1 answer

Cython module property

In Cython, you can define getter and setter functions for class properties: cdef class module property a: def __get__(self): cdef int a get_a(&a) return a def __set__(self, int a): set_a(&a) Is there a way to…
nicholaswogan
  • 631
  • 6
  • 13
1
vote
0 answers

setting environment variable for python c extension

I have a python package that uses a C extension. The C code has preprocessor directives, like #ifdef FLAG printf("with FLAG\n"); #else printf("without FLAG\n"); #endif In setup.py this is then defined as an extension like this cmodule =…
John Smith
  • 1,059
  • 1
  • 13
  • 35
1
vote
1 answer

Why does my Python Extension type have two ref counts rather than one?

I'm maintaining a Python type that has been built as a Python extension using the Python C API. My question is regarding the lifetime of this "NamedArray" object. Essentially my test code looks like this: def test_init_from_constructor(self): …
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
1
vote
1 answer

Python C API function arguments

How can I define a function, or a contructor for a class with specified arguments using the C API? By default function takes PyObject* args, and later in the code the linter won’t show real arguments. If it’s not possible, is there a way to bypass…
kebabdubai
  • 67
  • 6
1
vote
1 answer

Python C API: how to flush stdout and stderr?

When writing a Python extension with C code, one can print to the python standard output and standard error streams using functions PySys_WriteStdout and PySys_WriteStderr, which work similarly to C's printf…
anymous.asker
  • 1,179
  • 9
  • 14
1
vote
1 answer

How to make simple Hello World c module for python?

For the past few hours, I've been trying to build and run a simple c module that prints "hello world!!". I've run into a problem that I can't seem to work out: importing the module: import hi Traceback (most recent call last): File…
user15995231
1
vote
0 answers

Writing my own ufunc; ufunc not supported for the input types

I'm trying to write a C function that coverts numpy string array to a float array. How can I receive numpy's array in C as const char *? static void double_logitprod(char **args, npy_intp *dimensions, npy_intp* steps,…
1
vote
1 answer

Transfer C-API partial(print, flush=True) function to imported modules when issuing PyRun_File

I'm doing a functools.partial(print, flush=True) in the C-API before calling PyRun_File (please see Python C-API and PyRun_File: Using "import functools and partial(print, flush=True)"). The print works in the __main__ module, but isn't available in…
palle
  • 11
  • 1
1
vote
1 answer

call a python function

I need to call python from qt c++ (qmake) and share data between them. So I wrote this code but the error is logical, I mean no error but it stops unexpectedly. I tried this with python2.7 too but the same error. I want to pass a to someFunction…
mahya
  • 51
  • 4
1
vote
1 answer

How do I represent "inf" (floating-point infinity) using the Python C API?

I am writing a Python extension in C/C++ that works with numeric values. When a user passes in a value that is too large to fit into a float, I want to return the value that is represented in Python code as inf. How can I do that in C/C++? The…
Alan
  • 1,889
  • 2
  • 18
  • 30
1
vote
0 answers

Best way to iterate boost::python::dict

I am looking for a way to iterate through python dict (without creating tmp list object). After some little research I came to this. boost::python::object dictItemsView(const boost::python::dict& dict) { #if PY_MAJOR_VERSION >= 3 return…
ArmanHunanyan
  • 905
  • 3
  • 11
  • 24