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
1797
votes
35 answers

fatal error: Python.h: No such file or directory

I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below: gcc -Wall utilsmodule.c -o Utilc After executing the command, I get this error message: > utilsmodule.c:1:20: fatal…
Mohanad Y.
  • 18,278
  • 3
  • 12
  • 12
153
votes
6 answers

Python (and Python C API): __new__ versus __init__

The question I'm about to ask seems to be a duplicate of Python's use of __new__ and __init__?, but regardless, it's still unclear to me exactly what the practical difference between __new__ and __init__ is. Before you rush to tell me that __new__…
Channel72
  • 24,139
  • 32
  • 108
  • 180
90
votes
10 answers

Calling a python method from C/C++, and extracting its return value

I'd like to call a custom function that is defined in a Python module from C. I have some preliminary code to do that, but it just prints the output to stdout. mytest.py import math def myabs(x): return math.fabs(x) test.cpp #include…
D R
  • 21,936
  • 38
  • 112
  • 149
74
votes
3 answers

How to dynamically create a derived type in the Python C-API

Assume we have the type Noddy as defined in the tutorial on writing C extension modules for Python. Now we want to create a derived type, overwriting only the __new__() method of Noddy. Currently I use the following approach (error checking…
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
52
votes
1 answer

Create an object using Python's C API

Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; ...and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // ... pyfoo_new, }; How do I create a new…
detly
  • 29,332
  • 18
  • 93
  • 152
47
votes
2 answers

How to create a generator/iterator with the Python C API?

How do I replicate the following Python code with the Python C API? class Sequence(): def __init__(self, max): self.max = max def data(self): i = 0 while i < self.max: yield i i += 1 So far, I…
Michael
  • 11,612
  • 10
  • 41
  • 43
46
votes
1 answer

Py_INCREF/DECREF: When

Is one correct in stating the following: If a Python object is created in a C function, but the function doesn't return it, no INCREF is needed, but a DECREF is. [false]If the function does return it, you do need to INCREF, in the function that…
Izz ad-Din Ruhulessin
  • 5,955
  • 7
  • 28
  • 28
45
votes
6 answers

ImportError: dynamic module does not define init function (initfizzbuzz)

I tried to compile fizzbuzz.c, in order to import it by python. For building fizzbuzz.c,I used python setup.py build_ext -i. After building it, I tried to import fizzbuzz.c but the error below occurred. How can I solve this problem ? Error >>>…
SamuraiT
  • 992
  • 3
  • 12
  • 31
30
votes
3 answers

How To catch python stdout in c++ code

I have a program which during it's run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file. This is a declaration of the function …
alexpov
  • 1,158
  • 2
  • 16
  • 29
30
votes
7 answers

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API calls are needed. The official Python documentation is unfortunately very ambiguous. There are…
Channel72
  • 24,139
  • 32
  • 108
  • 180
28
votes
2 answers

Python multi-thread multi-interpreter C API

I'm playing around with the C API for Python, but it is quite difficult to understand some corner cases. I could test it, but it seems a bug-prone and time consuming. So I come here to see if somebody has already done this. The question is, which is…
MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
27
votes
3 answers

What is the "correct" way to pass a boolean to a Python C extension?

This is a simple example from the python documentation (http://docs.python.org/extending/extending.html): static PyObject * spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s",…
shygon
  • 273
  • 1
  • 3
  • 6
27
votes
3 answers

Stopping embedded Python

I'm embedding Python interpreter to a C program. However, it might happen that while running some python script via PyRun_SimpleString() will run into infinite loop or execute for too long. Consider PyRun_SimpleString("while 1: pass"); In preventing…
Anton L.
  • 2,397
  • 2
  • 17
  • 8
24
votes
5 answers

Allowing Ctrl-C to interrupt a python C-extension

I'm running some computationally heavy simulation in (home-made) C-based python extensions. Occasionally I get stuff wrong and would like to terminate a simulation. However, Ctrl-C doesn't seem to have any effect (other than printing ^C to the…
Michael Clerx
  • 2,928
  • 2
  • 33
  • 47
23
votes
1 answer

How to pass flag to gcc in Python setup.py script?

I'm writing a Python extension in C that requires the CoreFoundation framework (among other things). This compiles fine with: gcc -o foo foo.c -framework CoreFoundation -framework Python ("-framework" is an Apple-only gcc extension, but that's okay…
Michael
  • 4,700
  • 9
  • 35
  • 42
1
2 3
73 74