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

Passing python objects as arguments to C/C++ function using ctypes

I have a dll with a function that takes PyObject as argument something like void MyFunction(PyObject* obj) { PyObject *func, *res, *test; //function getAddress of python object func = PyObject_GetAttrString(obj, "getAddress"); res…
Maverick
  • 366
  • 1
  • 5
  • 11
-1
votes
1 answer

Why is Py_None reference count so high?

While reading through the documentation and some SO questions, I learned that Py_INCREF() should be applied to Py_None and that applying Py_DECREF() to Py_None is a bad idea unless you hold a reference to it. But when I look up the reference count…
Nima Mousavi
  • 1,601
  • 2
  • 21
  • 30
-1
votes
2 answers

What is the corresponding C-api name of the following numpy function in Python

I wanna write a C extension lib for Python, aiming to replace Python code with C. and the Python codes has several lines like below: import numpy as np a = np.array([1,3,12,0.43,234,-3,-4]) b = a[[1,3,5]] print(b) # array([ 3. , 0.43, -3. …
dongrixinyu
  • 172
  • 2
  • 14
-1
votes
1 answer

Does this Python C++ extension method leak or cause a segfault?

So I have this c extension function which loads a python module and uses a list of c++ strings to get a specific global attribute from that module: PyObject* get_global_constant(const char* module_name, std::vector constant_names) { …
-1
votes
2 answers

Is it possible use C python API without a Python Installation?

I am a composition student, and I am developing py4pd https://github.com/charlesneimog/py4pd, a code that can run Python with Puredata. One of the problems is that I currently need a Python installation for the code to load. My question: is it…
-1
votes
2 answers

is there pybind11 method that has similar properties of PyString_AsString

I am trying to convert the c++ project from boost to pybind11 QString r = QString(PyString_AsString(result));" QString r = QString(py::str(result));
gino247
  • 1
  • 2
-1
votes
1 answer

PyObject_CallMethod: call static member function from c++

I've embedded Python (3.6) code into my C++ application successfully. I use the Python/C API to call methods from this library. An overview of the python module is as follows: class MyClass(object): def __init__(args: str) -> None: ... …
Obibhand
  • 11
  • 3
-1
votes
1 answer

Develop a C module for applying selection sort

I am trying to develop a C module for applying a selection sort algorithm to a list of data and returning the sorted list back to the Python program as a Python list format. However, I found that the result of the program below will be something…
-1
votes
1 answer

PyImport_Import fails to import module that contains imports

I'm working on a c++ project that needs some functionalities included in a python library (scipy.stats). I created my python script that uses the libray and my idea is to pass the data from c++ to the python script, call the function and get the…
-1
votes
1 answer

getting a error message with Python/C API

I was looking for a way to get error message from executing python code in C++. I tried some answers from How to get Python exception text, but any of them worked for me. Can someone explain me what I'm doing wrong? #include #include…
IzZy
  • 364
  • 3
  • 16
-1
votes
1 answer

Why does wheel installation put shared objects in site-packages folder instead of package folder?

I've a python binary distribution [wheel] created via python setup.py bdist_wheel The wheel looks as follows unzip -l dist/-1.0.0-cp36-cp36m-linux_x86_64.whl Archive: dist/-1.0.0-cp36-cp36m-linux_x86_64.whl Length …
Chaitanya Bapat
  • 3,381
  • 6
  • 34
  • 59
-1
votes
1 answer

Embedding Python into a C++

I am a bit confused for my implementation. I have a Python script to be embedded into C++. import urllib.request import ssl import suds.transport.http from suds.client import Client class…
batuman
  • 7,066
  • 26
  • 107
  • 229
-1
votes
1 answer

Python calling Cython-function result in core dump

I'm currently working on a project the requires Python C-API So I created a class in the C side and inherited from it python side. one of the C functions in the future should return either None or dict. the function: static PyObject*…
Rami Hassan
  • 149
  • 12
-1
votes
1 answer

Python C Api failing silently

I'm trying to make a Python interface for my C-application, but when I run it the shell just resets and no error is printed. Here is my code: #include #include #include #include static PyObject…
jotjern
  • 460
  • 5
  • 18
-1
votes
1 answer

Wrong first argument when using PyArg_ParseTuple

I write methods for a c extension type: static PyObject * RawGraphState_apply_C_L(RawGraphState * self , PyObject * args) { npy_uint8 vop = 0xdeadbeef; npy_intp i;// = 0xdeadbeef; if(!PyArg_ParseTuple(args, "II",…
LittleByBlue
  • 436
  • 9
  • 18