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

pybind11: Python to C++ Datatype Conversion Not Working

Problem I am trying to convert a list of lists, returned by a python function called inside a C++ code. Though the pybind11 library allows type conversion from python data types to C++ data types, my attempt to convert a list of lists returned by…
0
votes
0 answers

Python C API escape string

I want to use python C API to escape a string. The string would be later used in a python eval as an argument for a python method, so currently I'm using this construct: char* Escape(char* string) { PyObject *var = PyUnicode_FromString(string); …
rfg
  • 1,331
  • 1
  • 8
  • 24
0
votes
1 answer

What is the proper way to include pybind11 embedded module at user class (as part of singleton class)

I have a Linux C++ dynamic library, which is required to pass a compound structure to python, and receive result structure from python to be transferred up to caller application. To get the python interpreter "alive" after library API function…
GM1
  • 380
  • 3
  • 14
0
votes
0 answers

Program crashes when running the PyArray_Max command

I am attempting to pass a numpy array into some C++ boost code. My boost code crashes without any error output when running the PyArray_Max command. Any idea why this is happening? I use the command "python numpy_setup.py build_ext --inplace" to…
0
votes
2 answers

How to convert a python 2 C extension module to python 3

I am trying to compile an extension module where I define a custom type. However, I got hold of some python 2 code for this purpose, but I cannot for the life of me convert it into a python 3 module. So far, I have tweaked the original code I found…
PintoDoido
  • 1,011
  • 16
  • 35
0
votes
1 answer

is PyODict_New supported by Python 3.6?

I can't find it a Python documentation, although it exist at https://github.com/python/cpython/blob/3.6/Objects/odictobject.c
GM1
  • 380
  • 3
  • 14
0
votes
1 answer

Why relasing and acquiring GIL within two threads causes an application crash?

I have developed a Python extenstion using C++. The only function of this module is something like this: static PyObject *TestModule_API1(PyObject *self, PyObject *args) { PyThreadState *_save; _save = PyEval_SaveThread(); try { …
0
votes
0 answers

How to get history from Python C API

I want to get history count (and contents) from Python. From command line it's possible with readline module (installed via pip install pyreadline): import readline x=readline.get_current_history_length() x Yields 2, as it should. However the same…
rfg
  • 1,331
  • 1
  • 8
  • 24
0
votes
1 answer

How to modify the value of a Python variable(like a refrence) in python

I am writing a syscall wrapper for python(just as a fun project to get me used to the API), and when I came upon implementing read(), I am perplexed as to how I can modify a python buffer that is sent to my function. The function is a simple cpython…
Shipof123
  • 233
  • 2
  • 11
0
votes
1 answer

How to (de-)serialize PyObject* in C(++)?

I'm currently working on a multi-threaded python module in C(++). I'm almost done, but one of the last things I need to do is to find a way around the GIL, so that communication between threads becomes possible. To do this, I wish to attempt the…
Mitchell Faas
  • 430
  • 6
  • 19
0
votes
1 answer

Python C-API: segmentation fault on PyDict_GetItem, possible reference problem?

I have a reference in C to a Python list of dictionaries. I am writing a function to calculate the dot product between two members of the list: PyObject *handle; // reference to a list of dictionaries virtual float dot_product () (unsigned i,…
Agargara
  • 902
  • 11
  • 24
0
votes
0 answers

How to propegate exceptions with the Python C API?

I'm working on a project which requires me to interface C(++) code with Python. Now, in general, this is going quite well, but whenever I run into a bug, instead of python/C printing the exception/error that's occurring, it just quits the program as…
Mitchell Faas
  • 430
  • 6
  • 19
0
votes
0 answers

AttributeError: module 'pythoncom' has no attribute 'Coinitialize'

My IDE is Pycharm. In order to use win32com.client in a new thread. I used pythoncom.CoInitialize() as many guys suggested here before calling win32com.client.DispatchEx(). But I got the following error. AttributeError: module 'pythoncom' has no…
0
votes
0 answers

Issue with PyArray_SimpleNewFromData - unknown location?

I am trying to call some np functionality from cpp. I have created array and I am trying to create pyArray: const int SIZE_X = 1000; const int SIZE_Y = 9; npy_intp dims[2]{SIZE_X, SIZE_Y}; const int ND = 2; double* c_arr =…
user3616359
  • 359
  • 3
  • 20
0
votes
0 answers

PyImport_Import seg. faults after calling C that calls Python, from Python

I am calling python code inside a C function and it works fine. However, I wanted to call the C function from python (effectively reuse all the C code) but when it comes to importing the python modules from C I get a seg. fault (Removed all error…