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

Python: C++ extension returning multiple values

I am writing a C++ extension for python script and want to return multiple values like what we can do in python function. Simple Example in python: def test(): return 0,0 tuple seems to be the closest answer #include…
Mark Jin
  • 2,616
  • 3
  • 25
  • 37
0
votes
0 answers

Why i'm unable to copy PyArrayObject?

How i can copy existing PyArrayObject ? I've read documentation from this link http://docs.scipy.org/doc/numpy/reference/c-api.array.html#creating-arrays Also I've read this explanations: numpy array C api PyArray_SimpleNewFromData example I've…
Anatoly Strashkevich
  • 1,834
  • 4
  • 16
  • 32
0
votes
3 answers

How can convert PyObject variable to Mat in c++ opencv code

I have a c++ facerecognition code and a python code in opencv. In python code i read frames from a robot and i want to send this fram to my c++ code. I use this link tho call python function in c++ function. my c++ function is embed.cpp: #include…
narges
  • 681
  • 2
  • 7
  • 26
0
votes
0 answers

Segmentation fault while embedding Python into C

Running the code from the answer of another post, I tried running the same on Linux, however I always encounter a segmentation fault. On trying to debug through gdb, it shows that the variable mymodule remains NULL even after appending the path of…
suzaku
  • 11
  • 4
0
votes
1 answer

Cython callback segfaults using python-c-api calls

Working on a control of an eGige camera, having a C library, I've started a cython code project with the idea to have the best things of each language. The library provides a way to listen a heartbeat from the camera to know if it has been…
srgblnch
  • 323
  • 3
  • 11
0
votes
0 answers

Python C Extension - Memory Leak despite Refcount = 1 on returned PyObjects

I'm repeatedly calling a python module I wrote in C++ using the Python C API. My python program repeatedly calls my module's pyParse function, which does a bunch of stuff and returns a PyTuple that contains more PyTuple objects as elements. Every…
0
votes
1 answer

Extracting nested python dictionaries in C

How can access nested dictionaries passed from Python (through scipy.weave) to C? I am developing some complex code which requires interfacing Python code with C code. The python/scipy.weave code passes to the C interface a nested dictionary,…
maurizio
  • 745
  • 1
  • 7
  • 25
0
votes
0 answers

Crash in PyObject_Hash

I am hitting a periodic crash when calling PyObject_Hash. Below is the traceback: (gdb) bt #0 PyObject_Hash (v=0x0) at Objects/object.c:1107 #1 0x00002ac08c50c8e8 in tuplehash (v=) at Objects/tupleobject.c:351 #2 …
aks
  • 304
  • 3
  • 8
0
votes
1 answer

How to create and return a function object in a c extension module?

I am trying to write a python extension module where some of the functions are curried, but I am not quite sure how to go about doing so. The main difficulty being that I am not sure how to create and return a PyFunction object and how to then pass…
0
votes
1 answer

Equivalent of python lambda function for C (Python Extensions)

I'v written a Python extension module with C to speed up computation times. The first step is a 2D integration of a function f(x,y,k), which is very fast and allows me to integrate over y in [y1(x),y2(x)] and x in [a,b] whilst assigning a float to…
0
votes
1 answer

Defining an inner class using the Python C-API

In Python, it’s straightforward to define an inner class: class MyClass(object): class MyInnerClass(object): pass … which the inner class can be accessed as one would expect, e.g. by doing MyClass.MyInnerClass. I am trying to set up…
fish2000
  • 4,289
  • 2
  • 37
  • 76
0
votes
1 answer

How to catch runtime error for a python module written in C?

Say I have a python module of the form .so and it contains something like this: static PyObject* f(PyObject* _1, PyObject* _2) { int a = 0; a = 1/a; return NULL; } After running Python will crash. The command line gives Floating point…
h__
  • 865
  • 1
  • 11
  • 19
0
votes
1 answer

python-c-api reference count

My python code pass some dictionary and lists to C like this: if (!PyArg_ParseTuple(args, "O!lO!O!O!O!O!", &PyList_Type,&list,&los,&PyDict_Type,&entity_len,&PyDict_Type,&inlist_len,&PyList_Type,&inindex,&PyDict_Type,&inlist,&PyList_Type,&tokens)) …
0
votes
2 answers

a null constant in Python c extension

I am developing a python c extension I want to have a constant for my module. If module name is module1 then the constant should be module1.null. When a user passes module1.null I want to perform a particular operation. So what value should I give…
0
votes
2 answers

Having a function set a global variable

Consider the following C++ module (explanation to follow): #include #include "nr3python.h" Doub tau_0; static PyObject* analysis_c_set_parameters(PyObject *self, PyObject *pyargs) { NRpyArgs args(pyargs); Doub tau_0 =…
abcd
  • 10,215
  • 15
  • 51
  • 85