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

Access violation in PyRun_String()

I've got an access violation error in a C code calling Python. I am trying to call a sympy function from Python and handle the result in C. #include int main(int argc, char *argv[]) { PyObject *pmod, *pdict, *pValue; …
franpena
  • 151
  • 11
1
vote
0 answers

Can you convert a c++ rapid json object to a Python Json object?

I am building a C++ project that connects to a customer-owned Python codebase and sending a json object to the python code. Because the python codebase is customer-owned, I cannot modify the python codebase to receive a json string. I am using…
1
vote
1 answer

Segmentation fault error in python embedded code in C++ code of Omnet++ simple module

I want to call a Python function from C++ code in OMNeT++ simple module. I debugged the code using gdb. It passes all lines fine, but at the end the segmentation fault occurs after Py_Finalize();. I found the following issue on GitHub that…
Mouna
  • 11
  • 2
1
vote
1 answer

Python C++ API - how to access/set class atributes from Python?

I have created getsetters for a public variable number_bananas I have in my Box class. number_bananas is public because the box is unlocked, anyone can eat bananas or put more in the box. Here is my PyBox type: typedef struct { PyObject_HEAD …
PintoDoido
  • 1,011
  • 16
  • 35
1
vote
1 answer

Python C API: How to get something from a module

In Python C API, I already know how to import a module via PyImport_ImportModule, as described in Python Documentation: Importing Modules. I also know that there is a lot of ways to create or allocate or initialize a module and some functions for…
Cosmo
  • 836
  • 1
  • 12
  • 27
1
vote
0 answers

Is there any way to reconcile Python C-Extensions in Java

I have some statistical & machine learning models written in Python using some of the standard Python libraries for such things (scikit-learn, pandas, numpy). I have these wrapped and served by a web service in Python to expose the models via REST.…
wrink
  • 11
  • 1
1
vote
1 answer

Return a C String Array from a C Extension

I have a C function that returns an array of strings. How can I call it in the form of a Python C extension which will return the array back to a calling Python function? (I am new to Python C extensions and have minimal experience with the…
c00der
  • 543
  • 1
  • 4
  • 20
1
vote
1 answer

Can't import C extension file in Python

My extension C function, in pyc.c #include static PyObject *add(PyObject* self,PyObject* args) { int a=9; int b=10; int c=a+b; return Py_BuildValue(c); //return Py_BuildValue( "s","hiiiii"); } static char prin_docs[]…
Mahesh
  • 13
  • 5
1
vote
0 answers

Inaccessible memory when parsing two bytes from tuple

I created a small C wrapper function which requires two bytes variables: static PyObject * wrapperfunction(PyObject * self, PyObject * args) { const unsigned char * data; Py_ssize_t datalen; const unsigned char * otherdata; …
reox
  • 5,036
  • 11
  • 53
  • 98
1
vote
1 answer

Python C extension - memory leaks

I'm relatively new to Python and this is my first attempt at writing a C extension. Background In my Python 3.X project I need to load and parse large binary files (10-100MB) to extract data for further processing. The binary content is organized in…
1
vote
0 answers

How to pass multidimensional array from C to embedding python function

This maybe asked before, but I need more clarification. I am working on a project where I need to send a 3D array from C to the embedded Python function. I referred this, but couldn't get it to work for a 3D array. Also, I tried including…
1
vote
0 answers

SIGTERM sent to C Thread in Python Extension Causes Python

I am writing a Python C extension that contains multiple C pthreads. Eventually these threads are sent a SIGTERM in order for them to exit. When I step through the extension in GDB these threads exit successfully, and I return back to the Python…
Dominic
  • 81
  • 4
1
vote
1 answer

Python C-API int128 support

In python one can handle very large integers (for instance uuid.uuid4().int.bit_length() gives 128), but the largest int datastructure the C-API documentation offers is long long, and it is a 64-bit int. I would love to be able to get a C int128…
azmeuk
  • 4,026
  • 3
  • 37
  • 64
1
vote
1 answer

Returning new PyObject * from C++ to Python eventually segfaults

I am writing the C++ and Python side of a library that exposes some functionality in our software written in C++ to Python scripts. I'm compiling some source files of interest and a wrapper file that looks like below into a shared library and…
Einheri
  • 957
  • 1
  • 9
  • 22
1
vote
0 answers

How to convert PyObject to c++ array?

I am working with python-c binding and I face with following problem. In python class there is a attribute called means which is numpy array of shape (2,3). If pInstance is PyObject* of that class, I wanted to do something like this: PyObject*…
user3616359
  • 359
  • 3
  • 20