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

Declaration in C considered definition in C++

I am working on an open-source C file containing the following declaration static PyTypeObject Bitarraytype; followed later by the definition static PyTypeObject Bitarraytype = { /* A bunch of stuff */ }; I am porting this code to C++…
Throckmorton
  • 564
  • 4
  • 17
3
votes
1 answer

Python object not fully initialized using C API

In the following scenario, the object is not meant to be instantiated within Python (hence no tp_new or tp_init). Calling the ThingType as a function using PyObject_CallObject results in a segfault in _PyObject_FastCallDict, I believe because there…
Scott
  • 4,070
  • 3
  • 21
  • 16
3
votes
0 answers

Undefined symbol in Clang-compiled Python extension using OpenMP

I am facing a weird problem when compiling a Python extension featuring OpenMP with Clang. Minimal Example I managed to boil down my actual problem to the following code: The Python extension could not be much simpler, while still featuring…
Wrzlprmft
  • 4,234
  • 1
  • 28
  • 54
3
votes
1 answer

Does PyDict_SetItem increase the reference count of the key, and if so, where in the code does it take place?

TLDR: PyDict_SetItem increments the key and the value, but where in the code does this happen? PyDict_SetItem makes a call to insertdict. insertdict immediately performs Py_INCREF on both the key and the value. However, at the end of the success…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
3
votes
3 answers

How to check if an object in Python is a PyCapsule?

I have a C Extension that receives and accepts PyCapsule objects. In my python wrapper, how can I check if a python object is of the PyCapsule type object? >>> # My C extension >>> foo = Foo() >>> capsule = foo.to_capsule() # returns a PyCapsule…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
3
votes
1 answer

Python C Extension - Any harm in adding Py_BEGIN_ALLOW_THREADS around every pure C call?

It's recommended to wrap pure C functions that block on IO, or even pure C functions that spend substantial time in CPU, in Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS blocks. Is there any harm to wrapping pretty much all pure C functions (with…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
3
votes
1 answer

Using format_exc from Python 3.8.1 C API returns NoneType

I'm trying to retreive an exception + traceback printout in a C extension. I want to do the same thing as try: print(0/0) # Throw a divide by zero except: traceback_string = traceback.format_exc() but in the C API so I can pass the…
RFairey
  • 736
  • 3
  • 9
3
votes
0 answers

Documenting a Python C Extension on ReadTheDocs

I have a Python package including a couple C extensions whose documentation I would like to host on ReadTheDocs, but the build fails, apparently because the C extensions are not being compiled. The C extension modules include functions with…
Geoff Ryan
  • 353
  • 1
  • 2
  • 7
3
votes
2 answers

Any way to create a NumPy matrix with C API?

I read the documentation on NumPy C API I could find, but still wasn't able to find out whether there is a possibility to construct a matrix object with C API — not a two-dimensional array. The function is intended for work with math matrices, and I…
Maxim
3
votes
1 answer

How do I check if a PyObject is a list?

I am new to the Python/C API and while I got some basic functions to work, I am struggling with this one. PyObject* sum_elements(PyObject*, PyObject *o) { Py_ssize_t n = PyList_Size(o); long total = 0; if (n < 0) { return…
kleka
  • 364
  • 3
  • 14
3
votes
1 answer

Issue with SWIG for creating Python bindings for C library

I want to use SWIG to create Python bindings for C library, but I have some troubles with it. I used the following .c and .i files from tutorial. example.c /* Compute factorial of n */ int fact(int n) { if (n <= 1) return 1; else return…
Judicator
  • 59
  • 6
3
votes
2 answers

How to use setuptools packages and ext_modules with the same name?

I got the following file structure for my Python C Extension project: . ├── setup.py ├── source    ├── cppimplementation    │   └── fastfile.cpp    └── fastfilepackage    ├── __init__.py    └── version.py And I use the following…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
3
votes
1 answer

How to access constants from objects' methods with the Python C API?

I have a follow up for another question. That question is about how to access constants added to a C extension module for Python. To paraphrase that question and answer, if you added constants RED and BLUE to your module in the initialization…
Dominick Pastore
  • 4,177
  • 2
  • 17
  • 29
3
votes
2 answers

Python C API: T_INT was not declared in this scope

I'm following the official tutorial of Python API to create a simple extension type in C++ for Python. But I can't get my code successfully compiled. Because when I use T_INT in my code I got an error said 'T_INT' was not declared in this scope.…
Cosmo
  • 836
  • 1
  • 12
  • 27
3
votes
2 answers

Creating an inheritable Python type with PyCxx

A friend and I have been toying around with various Python C++ wrappers lately, trying to find one that meets the needs of both some professional and hobby projects. We've both honed in on PyCxx as a good balance between being lightweight and easy…
Toji
  • 33,927
  • 22
  • 105
  • 115