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 const char * to function

I'm trying to embedd python interpreter in C++ app using Python/C API. In my code exist something like that: string code("python code here"); PyRun_SimpleString( code.c_str() ); Python/C API function expect const char * argument. This code is…
IzZy
  • 364
  • 3
  • 16
0
votes
1 answer

Missing Python headers (Python.h) on Heroku

I'm trying to deploy to Heroku a Python application which requires some C extensions. The problem is that when I deploy the app to Heroku via git push heroku master I get the following error: Counting objects: 9, done. Delta compression using up to…
Nicholas Obert
  • 1,235
  • 1
  • 13
  • 29
0
votes
1 answer

How to translate std::list from c++ to python with SIP

I am using SIP version 6.0.1 with python 3.8 and have a given c++11 API. (on Ubuntu 18.04) The goal is to store e.g. custom struct pointers (the structs I also translated with sip) in std::list using python. I constructed a tiny example to make my…
0
votes
0 answers

cython choose implementation based on Python after cythonize

When cython generates a example.c files using cython example.pyx this file contains a lot of preprocessor directives to conditionally select different implementations based on conditions like the Python Version. Is there an option to do the same…
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
0
votes
1 answer

Partially parse args/kwargs using the Python C Api

I try to implement a Python function using the Python C API, that has a functionality similar to (this implementation is simplified and does not include things like NULL check of keywds): def my_func(arg1, arg2 = 2, **kwargs): if arg2 == 1: #…
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
0
votes
0 answers

Memory being freed when using Python's PyMem-functions

I'm writing a Python C API and in my c-code I allocate a double array which I then wrap into a numpy array and send back to Python. The weird thing is that the memory is being freed in Python if I use PyMem_ functions to allocate the memory, but not…
0
votes
1 answer

Can a Python heap time created with "PyType_FromSpec()" be weak reference compatible

Migrating a C module extension for Python to Py_LIMITED_API (PEP 384) I am moving my static types to heap types created with "PyType_FromSpec()". It works so far, but my previous types were weakref compatible and I don't know how to make new heap…
jcea
  • 648
  • 6
  • 13
0
votes
0 answers

How to send a command and get the result ? API Python/C - PyRun_String

I would like to send a command to the interpreter and get the return of this command. I'm actually using PyRun_String and it's working greatly but I have to pass in args Py_single_input if I want to init a variable like a = 10then I get none. If I…
auguyon
  • 15
  • 5
0
votes
1 answer

How does one use both .tp_getattro / .tp_setattro and .tp_getset in a custom PyTypeObject?

I'm working to extend an existing C application into Python that includes custom data types. I would like the user to have the capability to pass an arbitrary string as an attribute for objects of this custom type. Some special attributes would…
perden
  • 647
  • 5
  • 20
0
votes
1 answer

How to print the object returned by PyRun_String?

I would like to know how to use the return of Pyrun_String. I already tried to use PyUnicode_DATA and PyObject_Str. if (!(pstr = PyRun_String("a = 1", Py_single_input, pGlobal, pdict))) exit(printf("Error while running string\n")); // I tried…
auguyon
  • 15
  • 5
0
votes
1 answer

Python C Extension

I am having issues returning a 2D array from a C extension back to Python. When I allocate memory using malloc the returned data is rubbish. When I just initialise an array like sol_matrix[nt][nvar] the returned data is as expected. #include…
0
votes
0 answers

Cannot find 'Python.h' after installation of python3-dev on ubuntu

I try to include Python.h for a c++ project. I am on ubuntu 18.04, I installed python3-dev (as I have python3.6 on my system) and everything looks good, I can even locate the Python.h as shown in the image below. But cmake still complains it cannot…
shelper
  • 10,053
  • 8
  • 41
  • 67
0
votes
1 answer

Memory leak problem with PyObject or PyFloat conversion

This is my code for Python Wrapper for C function doing some simple calculation with Rotation matrix and Vectors. My function is transform_object, but it's not causing the problem (I was debuging it also without executing this func). static…
rozumir
  • 845
  • 9
  • 20
0
votes
1 answer

How to combine own C-extension in own Python Package

I created own Python Package in which I want to add own Python C Extension, because in Python Package I'm importing this C-Ext. I want to install it from local files, not pypi. I have dist files of C Extension, and I wonder how to do it properly. I…
rozumir
  • 845
  • 9
  • 20
0
votes
1 answer

Python embed into C++

I have python codes ebmedded into C++. Do I need to release memory(Py_XDECREF) PyObject* pValue and PyObject *pArgs? When I do Py_XDECREF(pArgs) and Py_XDECREF(pValue) I have Segmentation Fault (Core dumped). I think python side is still using those…
batuman
  • 7,066
  • 26
  • 107
  • 229