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

makecython++ causes fatal error: Python.h: No such file or directory despite python3-dev installed

My makefile: SHELL := /bin/bash .PHONY: all all: pip install runcython makecython++ stitch_wrapper.pyx "" "stitch_rects.cpp ./hungarian/hungarian.cpp" hungarian: hungarian/hungarian.so hungarian/hungarian.so: cd hungarian && \ …
AndreaF
  • 11,975
  • 27
  • 102
  • 168
7
votes
1 answer

Python 3.3 C-API and UTF-8 Strings

So, Python 3.3 introduced PEP 393, which changes the implementation of Python str objects (Unicode objects) so that internally, they are represented in a memory-efficient way while still allowing for random access to each character. This is done by…
Siler
  • 8,976
  • 11
  • 64
  • 124
7
votes
2 answers

How Does String Conversion Between PyUnicode String and C String Work?

I have a PyUnicode object I'm trying to convert back to a C string (char *). The way I am trying to do it does not seem to be working. Here is my code: PyObject * objectCompName = PyTuple_GET_ITEM(compTuple, (Py_ssize_t) 0); PyObject * ooCompName =…
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96
7
votes
1 answer

No module named _testcapi

Just create virtualenv on Centos7 and try to run unittests for my app. $ cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) $ python -V Python 2.7.5 gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9) $ py.test -v . …
greggyNapalm
  • 533
  • 5
  • 18
7
votes
1 answer

Python C API: Switch on PyObject type

I have some code to interface Python to C++ which works fine but every time I look at it I think there must be a better way to do it. On the C++ side there is a 'variant' type that can deal with a fixed range of basic types - int, real, string,…
Bob
  • 195
  • 1
  • 11
7
votes
2 answers

What is the PyClass_New equivalent in Python 3?

Previously I have created some Python classes using C API. When I'm going to build the old project with Python 3+ it gives following compile error PyClass_New was not declared in this scope Py_InitModule was not declared in this scope What are the…
Sujith Gunawardhane
  • 1,251
  • 1
  • 10
  • 24
7
votes
1 answer

Why do i need the gil for PyMem_Malloc()?

As per this discussion, PyMem_Malloc() requires the GIL; however, if the function is nothing more than an alias for malloc(), who cares?
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
7
votes
1 answer

python c-api: create bytes using existing buffer without copying

It seems to me the buffer protocol is more for exposing Python buffer to C. I couldn't find a way to create a bytes object using existing buffer without copying in C. Basically what I want is to implement something similar to…
user3761759
  • 715
  • 1
  • 5
  • 8
7
votes
1 answer

Access a Numpy Recarray via the C-API

If we have a Numpy recarray: x = np.array([(1.,2.)], dtype=np.dtype([('a','
Joel Vroom
  • 1,611
  • 1
  • 16
  • 30
7
votes
1 answer

How do I create an array slice using the NumPy C API?

I want to slice through a 1D NumPy from within a C extension. I see all sorts of helper functions in the C API for creating fresh arrays, reshaping, indexing particular values, etc.. But I don't see anything like PyArray_Slice1D(array, start, stop,…
7
votes
1 answer

Nested Python C Extensions/Modules?

How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so that it imported via "import mymodule.bar"? (Sorry if this is poorly phrased, I…
Michael
  • 4,700
  • 9
  • 35
  • 42
7
votes
2 answers

Correct cyclic garbage collection in extension modules

Two sections of Python 2.7's documentation mentioned adding cyclic garbage collection (CGC) support for container objects defined in extension modules. The Python/C API Reference Manual gives two rules, i.e., The memory for the object must be…
liuyu
  • 1,279
  • 11
  • 25
6
votes
1 answer

Embedding python + numpy code into C++ dll callback

I am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll) the problem i am facing is the following. if i have: Py_Initialize(); // some python glue // python…
Pa_
  • 641
  • 1
  • 5
  • 17
6
votes
2 answers

python import path for sub modules if put in namespace package

I have a python modules written in C, it has a main module and a submodule(name with a dot, not sure this can be called real submodule): PyMODINIT_FUNC initsysipc(void) { PyObject *module = Py_InitModule3("sysipc", ...); ... …
fluter
  • 13,238
  • 8
  • 62
  • 100
6
votes
1 answer

PyThreadState_SetAsyncExc has no effect

I'm using multiple interpreters with multiple threads, and I'm trying to use PyThreadState_SetAsyncExc to stop a script, but it has no effect: void stop() { PyThreadState *ts = PyThreadState_Swap(this->tstate); PyObject *exc =…
fferri
  • 18,285
  • 5
  • 46
  • 95