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

Python C API: How to get PyRun_String with Py_eval_input to use imported modules?

PyRun_String("random.randint(1,10)", Py_eval_input, globals, globals); returns error with: Traceback (most recent call last): File "", line 1, in NameError: name 'random' is not defined earlier in the code, I did:…
David
  • 65
  • 4
4
votes
1 answer

How to return a Python Enum from C extension?

I'm writing a C extension for Python that wraps a C library. The C library has a few enum types and I've written corresponding IntEnums for these, for example: from enum import IntEnum # _enum_consts is a C extension creating Python # constants for…
Dominick Pastore
  • 4,177
  • 2
  • 17
  • 29
4
votes
1 answer

Python C Extension Need to Py_INCREF a Borrowed Reference if not returning it to Python Land?

If you obtain a borrowed reference but do NOT return it to Python land, should you be doing a Py_INCREF when you obtain it and then a Py_DECREF once you are done with it, or is this superfluous? For example, this trivial example gets a borrowed…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
4
votes
0 answers

Python C Extension How to test reference counting and memory leaks?

Is there an idiomatic way to test a python C extension such that you can have high confidence that your reference counting is correct and that you will not have any memory leaks? I currently just uncomment some printf statements in my deallocator…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
4
votes
2 answers

Building a Python-C-Extension on Windows with a debug Python installation

If I build CPython from source on Windows I encounter problems when I want to pip install a package that contains a C-Extension. It seems like the error happens while linking the libraries. For example when installing cython (but it also crashes…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
4
votes
1 answer

Is incrementing Py_True/Py_False refcount always necessary?

I'm new to the Python C-API and browsing through some source code to pick parts of it up. Here is a minimal version of a function that I found, in the C source of a package that contains extension modules: #define PY_SSIZE_T_CLEAN #include…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
4
votes
1 answer

Implementing unit tests for a Python C Extension

So, I have a repo to build a python C extension laid out as follows: setup.py demo.c MANIFEST.in The contents of the C file are: #include static PyObject* print_message(PyObject* self, PyObject* args) { const char* str_arg; …
Josh Weinstein
  • 2,788
  • 2
  • 21
  • 38
4
votes
2 answers

How does one acces numpy multidimensionnal array in c extensions?

I have been struggling for a few day to understand the access to numpy arrays in C extension, but I've trouble understanding the documentation. Edit: Here is the code I would like to port to c (the grav function) import numpy as np def grav(p, M): …
Camion
  • 1,264
  • 9
  • 22
4
votes
1 answer

Manipulate JSON without deserializing it

An aiohttp application fetches a JSON from external resource and need to use it to perform another request passing the JSON as the request body. To avoid serialization/deserialization overhead ujson is used and then the JSON object is just passed to…
Rodrigo Oliveira
  • 1,452
  • 4
  • 19
  • 36
4
votes
1 answer

Python C Extensions adding Attributes to an Exception

I'm wrapping a C library which returns one of a finite number of error codes upon failure. When the error happens, I would like to add the error code as an attribute on the C exception, such that the Python code can retrieve it and map the error…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
4
votes
1 answer

How to interrupt python interpreter embedded in C++ application

We have a C++ QT application, we embedded python in it. We provided two interfaces to the user 1. Execute file 2. Stop execution. We execute a python file in a non GUI thread, using PyRun_FileExFlags. We would like to interrupt python…
srinivas M
  • 55
  • 7
4
votes
0 answers

When should I call PyGILState_Ensure?

I am writting performance aware code in C and wrap the functions parameter with Python C API and ctypes. I am confused about PyGILState_Ensure. What does it protect? When should I lock or release the GIL. For example: extern "C" DLL PyObject *…
worldterminator
  • 2,968
  • 6
  • 33
  • 52
4
votes
2 answers

ImportError: C extension: No module named 'parsing' not built

I have been trying to find a solution to this import error regarding the pandas library when it says no module named "parsing." Every library should be installed correctly from the interpreter and they are all the latest version. This is what the…
Shayaan Khan
  • 43
  • 1
  • 6
4
votes
1 answer

How to sort a list with a custom key using the Python C API?

The Python C API provides the follow method for sorting lists: int PyList_Sort(PyObject *list) Sort the items of list in place. Return 0 on success, -1 on failure. This is equivalent to list.sort(). However, no argument is available for…
Ryan Gabbard
  • 2,269
  • 2
  • 24
  • 37
4
votes
1 answer

Error in writing Python functions in Crystal-lang

I am trying to write some python function in crystal-lang through the C Python API. My code follows: METH_VARARGS = 0x0001 @[Link("python3.5m")] lib Python alias PyObject = Void* struct PyMethodDef name : UInt8* func : Void* …
Max
  • 145
  • 9