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

Python C Api check if PyObject points to a specific PyCFunction

I have a module written using the Python C API: static PyObject* my_func1(PyObject* /*self*/, PyObject* args) { Py_RETURN_NONE; } static PyObject* my_func2(PyObject* /*self*/, PyObject* args) { Py_RETURN_NONE; } static PyMethodDef methods[] =…
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
1
vote
0 answers

Python C API Allow threads block with static variables

I am wrapping library that uses global defined function as callback. void library_callback(size_t id, char *buf, size_t len); This library_callback function is called internally by library but I can set this function via public API. I want to…
ventaquil
  • 2,780
  • 3
  • 23
  • 48
1
vote
0 answers

Cyclic garbage collection for a concurrent queue (Python C extension)

I am trying to wrap a concurrent queue implementation in C with Python. It has two methods: push adds a generic void pointer (in this case a PyObject pointer) to an internal linked list and increments its reference count by one. pop removes an…
1
vote
0 answers

Python's 'setuptools' doesn't include auto-generated SWIG Python files

Background Due to performance reasons in my Python application, I will eventually be trying to write a couple of functions in the C language to convert integers to/from a VLQ binary string representation. Since I don't want to deal with…
Mr. Minty Fresh
  • 229
  • 1
  • 6
1
vote
1 answer

name of init function of native python module

So i'm trying to make a python binding module in C, but confused on what is the naming rule of the module init function, which is declared by PyMODINIT_FUNC, i've seen init_foo, and initfoo, and someone else is using PyInit_foo, does the name…
fluter
  • 13,238
  • 8
  • 62
  • 100
1
vote
1 answer

Passing dynamic array as an argument in PyObject_CallObject in Python/C API

I want to access some python functions that I wrote from my C code. One of my python functions receives a numpy array as an input. In my C code, this array is allocated in the dynamic memory. Whenever my python function is called I receive a…
Dawood
  • 11
  • 3
1
vote
0 answers

Python C API error restarting interpreter

I'm developing a little DLL as a proxy between a C++ application and Python scripts. I would like to exposed a function to restart the python interpreter. I have tried something like this: void initialize() { Py_Initialize(); } …
1
vote
0 answers

Embedding python script in c++

I have a python script called web_interface.py import urllib.request import ssl import suds.transport.http from suds.client import Client class UnverifiedHttpsTransport(suds.transport.http.HttpTransport): def __init__(self, *args, **kwargs): …
batuman
  • 7,066
  • 26
  • 107
  • 229
1
vote
0 answers

Return a char array from Python extension module

I'm trying to build a simple C extension for Python, initially I'm trying to return the contents of an unsigned char array (binary data). unsigned char frame_out[200]; // test data for (int i = 0; i < 199; i++) { frame_out[i] =…
Garry
  • 11
  • 2
1
vote
0 answers

How do I build a C extension as a submodule to a C extension main module with distutils?

I have two files, module.c and submodule.c. I have the following code in setup.py: from distutils.core import setup, Extension module = Extension('module', sources = ['module.c']) submodule = Extension('submodule', sources =…
gny-001f2
  • 29
  • 5
1
vote
1 answer

How to specify the dynamic library location when writing a setup file for python c-extension?

I'm trying to write a c-extension for python and I'm using a library (FFTW) which has some dynamic library files (.dll). So I wrote some c code and also a setup.py file to install it and when I run the setup file everything goes well and my…
whoAmI
  • 358
  • 4
  • 16
1
vote
0 answers

Unable to import the library when I call python from c++

I want to do the following. 1. I want to call python from C++. 2. return the value to C++ when I'm done with python. Compile the c++ file and run the executable. Then I get an error in the python file. It was caused by a failure of imort. When I…
tanik12
  • 11
  • 3
1
vote
1 answer

Unit test for memory leaks in Python

Is there a way to run a unit test for memory leaks in Python? I've written some C++ binded code and want to make sure it doesn't leak memory. For example, I would like a test like the following: memory_usage_0 = 0 memory_usage_1 = 0 memory_usage_0 =…
Daishisan
  • 290
  • 1
  • 6
1
vote
1 answer

How to return a pointer to a C++ object to python using boost if the class of that object is declared in another boost module?

Okay, maybe I'm not finding an answer because I'm not sure how to word it, but I have a class called Info and another class called Packet, both are compiled into Python extensions using boost, and I would like to return a pointer to an Info object…
qwerty_99
  • 640
  • 5
  • 20
1
vote
1 answer

How to convert a Python deque to a C++ container?

I have to pass a Python deque to a C++ function but I can't find any documentation on how to convert a deque to a C++ container (for this case, either a deque or a vector would do). Searched everywhere but I couldn't find a solution. There's gotta…
qwerty_99
  • 640
  • 5
  • 20