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
9
votes
3 answers

Passing arguments to tp_new and tp_init from subtypes in Python C API

I originally asked this question on the Python capi-sig list: How to pass arguments to tp_new and tp_init from subtypes? I'm reading the Python PEP-253 on subtyping and there are plenty of good recommendations on how to structure the types, call…
mloskot
  • 37,086
  • 11
  • 109
  • 136
8
votes
1 answer

Embedded Python 2.7.2 Importing a module from a user-defined directory

I'm embedding Python into a C/C++ application that will have a defined API. The application needs to instantiate classes defined in a script, which are structured roughly like this: class userscript1: def __init__(self): ##do something…
Fritz
  • 274
  • 3
  • 16
8
votes
1 answer

Do PyImport_ImportModule and import statement load into different namespace?

Here is canonical example of a program extending embedded Python 3.x in C/C++: #include //// Definition of 'emb' Python module //////////////////// static PyObject* emb_foo(PyObject *self, PyObject *args) { char const* n = "I am…
mloskot
  • 37,086
  • 11
  • 109
  • 136
8
votes
1 answer

How to pip install a package from a read-only directory?

I am trying to install a Python (C-extension, if it matters) package that is located in a mounted directory, which for security reasons is read-only. However, by default pip will build the package in a subdirectory called build in its own location,…
augustomen
  • 8,977
  • 3
  • 43
  • 63
8
votes
2 answers

How to create an Enum object in Python C API?

I'm struggling how to create a python Enum object inside the Python C API. The enum class has assigned tp_base to PyEnum_Type, so it inherits Enum. But, I can't figure out a way to tell the Enum base class what items are in the enum. I want to allow…
Jelle
  • 99
  • 1
8
votes
2 answers

Python C-API Object Allocation

I want to use the new and delete operators for creating and destroying my objects. The problem is python seems to break it into several stages. tp_new, tp_init and tp_alloc for creation and tp_del, tp_free and tp_dealloc for destruction. However c++…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
8
votes
4 answers

Create a Python type from C that implements a __dict__?

How is a type created to have a __dict__ as per a "normal" class would have were it defined in Python? Are there any examples of non-dynamic types with __dict__s? Do types defined via Python's PyTypeObject pass through type_new? There is a tp_dict…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
8
votes
1 answer

.so module doesnt import in python: dynamic module does not define init function

I am trying to write a python wrapper for a C function. After writing all the code, and getting it to compile, Python can't import the module. I am following the example given here. I reproduce it here, after fixing some typos. There is a file…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
8
votes
2 answers

Can't import numpy from C

I am failing to open numpy with the Python C API. I have the following code #include int main() { Py_Initialize(); PyRun_SimpleString("import numpy"); PyObject* numpy = PyImport_ImportModule("numpy"); Py_Finalize(); …
MatthewJohnHeath
  • 393
  • 2
  • 12
8
votes
1 answer

Is there a complete list of built-in functions that cannot be called with keyword argument?

People mentioned in answers a1, a2 that Due to the way the Python C-level APIs developed, a lot of built-in functions and methods don't actually have names for their arguments. I found it really annoying cause I'm not be able to know it by…
laike9m
  • 18,344
  • 20
  • 107
  • 140
8
votes
2 answers

numpy array C api

I have a C++ function returning a std::vector and I want to use it in python, so I'm using the C numpy api: static PyObject * py_integrate(PyObject *self, PyObject *args){ ... std::vector integral; cpp_function(integral); //…
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
8
votes
1 answer

Should {tp_alloc, tp_dealloc} and {tp_new, tp_free} be considered as pairs?

Is it true that whatever is created in tp_alloc should be destroyed in tp_dealloc? And similarly for {tp_new, tp_free}? It looks like an obvious symmetry, but I would be grateful for clarification. My actual use case is this: I have: class OSClass…
P i
  • 29,020
  • 36
  • 159
  • 267
8
votes
2 answers

Const correctness of Python's C API

It seems that the Python C API is not consistent with the const correctness of character arrays. For example, PyImport_ImportFrozenModule accepts a char*, whereas PyImport_ImportModule accepts a const char*. The implication of all this is that in…
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
8
votes
4 answers

Class property using Python C-API

What is the best way to create class properties (as here and here) using the Python C-API? Static properties would also work in my case. Follow up: I tried to implement yak's suggestion. I defined a class P with get and set functions in its…
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
7
votes
2 answers

Global Interpreter Lock and access to data (eg. for NumPy arrays)

I am writing a C extension for Python, which should release the Global Interpreter Lock while it operates on data. I think I have understood the mechanism of the GIL fairly well, but one question remains: Can I access data in a Python object while…
Daniel
  • 924
  • 1
  • 11
  • 16