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

How to list all function names of a Python module in C++?

I have a C++ program, I want to import a Python module and list all function names in this module. How can I do it? I used the following code to get the dict from a module: PyDictObject* pDict = (PyDictObject*)PyModule_GetDict(pModule); But how to…
xpadvent
  • 95
  • 5
6
votes
1 answer

How do I create a Python bytes object in the C API

I have a Numpy vector of bools and I'm trying to use the C API to get a bytes object as quickly as possible from it. (Ideally, I want to map the binary value of the vector to the bytes object.) I can read in the vector successfully and I have the…
Mark C.
  • 1,773
  • 2
  • 16
  • 26
6
votes
2 answers

When writing a python extension in C, how does one pass a python function in to a C function?

First off, apologies for the confusing title. What I am trying to achieve is the following: Suppose I have some function foo which takes a function and an integer as input. e.g. int foo(int(*func)(), int i) { int n = func() + i; return…
Mitchell Faas
  • 430
  • 6
  • 19
6
votes
2 answers

Embedding CPython: how do you constuct Python callables to wrap C callback pointers?

Suppose I am embedding the CPython interpreter into a larger program, written in C. The C component of the program occasionally needs to call functions written in Python, supplying callback functions to them as arguments. Using the CPython…
zwol
  • 135,547
  • 38
  • 252
  • 361
6
votes
1 answer

Cannot start dbg on my python C extension

Im using python3.6.1 installed in pyenv. I have problem with running gdb on my code. When I run gdb --args python mycode.py it ends with error "/home/vydra/.pyenv/shims/python": not in executable format: File format not recognized
wyDra
  • 65
  • 5
6
votes
1 answer

What does error_already_set in Boost.python do and how to handle exceptions similarly in Python C API

I have been working on a project where I want to remove the boost dependencies and replace it with the Python C API. I spent some time understanding the Python C API and I saw this catch (error_already_set const &) I read the boost docs but it…
NVS Abhilash
  • 574
  • 7
  • 24
6
votes
2 answers

Suggestions on how to speed up a distance calculation

Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance == 0: self._norm = 1.0 else: self._norm = 1.0 / (2 *…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
6
votes
2 answers

Python C API - Is it thread safe?

I have a C extension that is called from my multithreaded Python application. I use a static variable i somewhere in a C function, and I have a few i++ statements later on that can be run from different Python threads (that variable is only used in…
DenverCoder9
  • 151
  • 8
6
votes
0 answers

Extending and Embedding Python in C++: Is PyImport_AddModule required to import extension module before importing python script?

I am embedding Python in a C++ application and I also need to call back in to the C++ code from Python. I have been able to do this with the following simple example but I'm having a weird problem in my C++ application. Module definitions... static…
naybot
  • 61
  • 3
6
votes
0 answers

Calling a python class method from C++ Extension

I have a python module called my_module, within it I have a class called my_class and my_class has a classmethod called my_method. Based on other examples I have seen around I have come up with the following attempts to call my_method from C++ but…
JMzance
  • 1,704
  • 4
  • 30
  • 49
6
votes
1 answer

Python C API: Using the property decorator from a C extension

I'm writing a python module in C that provides a class, wrapping a C API. The C API has some basic accessor methods that I'd like to present as a property in the python class. For example, say I have: int libspam_get_eggs(spam_t *spam, int…
Jorenko
  • 2,594
  • 2
  • 21
  • 26
6
votes
2 answers

Are there any Python reference counting/garbage collection gotchas when dealing with C code?

Just for the sheer heck of it, I've decided to create a Scheme binding to libpython so you can embed Python in Scheme programs. I'm already able to call into Python's C API, but I haven't really thought about memory management. The way mzscheme's…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
6
votes
1 answer

Python GIL and threads

I have embedded Python3 in my big C++ application. Python gives the user script capability for custom data processing. Problem : I have many threads that interact with Python and I don't really get how to protect my code with GIL. So far, the only…
poukill
  • 540
  • 8
  • 18
6
votes
0 answers

How to inspect python object in visual debugger?

When debugging a python extension in visual debugger, the raw interpretation of a python object in the inspector is not user-friendly. One can hardly see the real content in the object. Is there a way to watch the python representation of the…
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
6
votes
1 answer

Can switching in-and-out PyFrameObjects be a good implementation of continuations?

I'm interested in continuations, specifically in Python's C-API. From what i understand, the nature of continuations requires un-abstracting low-level calling conventions in order to manipulate the call stack as needed. I was fortunate enough to…
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60