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
0 answers

Undefined reference to PyAST_mod2obj

So i'm doing some stuff with python c api. I'm trying to parse a string with function PyParser_ASTFromString There is no error here, I then tried to change mod_ty i got to PyObject* with functiin PyAST_mod2obj I then compile it with the command: gcc…
Malma
  • 71
  • 1
  • 4
0
votes
0 answers

PyGILState_STATE didn't change after PyGILState_Ensure()/PyGILState_Release()

I have a shared library written in C and called in Python. In a pthread, some Python C API called between PyGILState_Ensure() and PyGILState_Release(). The value of PyGILState_STATE remains the same all the time. c code PyGILState_STATE st =…
Jack
  • 55
  • 2
  • 7
0
votes
1 answer

How can I create a C module with a "static" object of a custom type?

Let's say I want to write a module called countries that contains information about the countries of the world. I want to write it in C. It will expose a member called countries which is a list of objects of type Country. The Country type has two…
Jack M
  • 4,769
  • 6
  • 43
  • 67
0
votes
1 answer

How to build a class in C++ to use in Python?

PyMethodDef from Python.h allows to specify Cpp-built functions to use in Python. However, there is much doubt if this can be applied for Cpp-built classes and I can't find anything like PyClassDef which could presumably help me to do so. All I've…
Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
0
votes
1 answer

How to acquire GIL for the new thread state created using embed python in C++ application

I have a C++ application where we provide a python editor(which uses python 3.7), users can input and execute the python code in the editor. Following is the sample code where we are creating a new thread state and making it current before executing…
Revanth
  • 59
  • 8
0
votes
0 answers

PyImport_Import returns ImportError when called from NASM but not from C

I call PyImport_Import to import the NLTK library. When called from NASM as shown below it returns a null pointer and throws an ImportError, but when called from C as shown below it returns a valid pointer. I get the same thing when importing…
RTC222
  • 2,025
  • 1
  • 20
  • 53
0
votes
1 answer

Python C_API with GCC - Python.h not found

I am testing the Python C_API using the program shown below with Python 3.8 on Ubuntu 18.04. I have tried compiling with Clang-10 and GCC 7.5, but in both cases the compiler is unable to locate Python.h. When I run this string: sudo gcc -v -shared…
RTC222
  • 2,025
  • 1
  • 20
  • 53
0
votes
0 answers

Django crashes when two users run a C extension at the same time

Summary of the problem I am calling my own C extension for Python in Django, which is in charge of some long computations. The extension works fine and even the user can navigate through the Django app while the C extension makes its computations,…
David Duran
  • 1,786
  • 1
  • 25
  • 36
0
votes
1 answer

Creating Python modules from an extension with extra C data structures

I'm working on a custom Python loader that creates Python modules from a particular kind of non-Python file, let's call it a "cheese file". I'm writing my project as a C extension module because these "cheese files" need to be processed by a fairly…
David Z
  • 128,184
  • 27
  • 255
  • 279
0
votes
1 answer

Determining type of decimal object in python with C API and converting to different type

I'm writing a python module in C via the Python C API. What I want to do is perhaps best illustrated by this short python snippet: def convert(value, func): if isinstance(value, decimal.Decimal): return func(value) return…
Bryant
  • 3,011
  • 1
  • 18
  • 26
0
votes
1 answer

Python C API: How to store any Python Object as a void* without knowing its type?

I was wondering if it is possible to store any Python object as a void* in Python C API which will be returned later to python as-is. I tried to do something like this: store_python_object(PyObject *self, PyObject *args) { int retval; …
0
votes
2 answers

Cython not compiling void function - "empty declarator"

I have a very simple c++ class defined in foo.cpp: class Foo { int x; public: Foo() {} Foo(int _x) : x ( _x ) {} int getX() { return x; } void print() { std::cout << "Foo { " << x << "}" << std::endl; } }; In…
OakenDuck
  • 485
  • 1
  • 6
  • 15
0
votes
1 answer

How to check if PyObject* points to the type numpy.uint8

How do I use the Python C-API to check if a PyObject* points to the type numpy.uint8 etc? (Note that I want to check if the PyObject* points to the type numpy.uint8, not if it points to an instance of the type numpy.uint8.)
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
0
votes
0 answers

How to parse extended integer type in python C extension module?

I am trying to pass a (large) integer from python to an extension module, but I am unable to parse pythons arbitrary precision integers to 256-bit unsigned integers uint256. Here is the C callee: #include typedef unsigned _ExtInt(256)…
Kevin
  • 3,096
  • 2
  • 8
  • 37
0
votes
0 answers

Numpy Python/C API - PyArray_SimpleNewFromData hangs

I'm figuring out the Python/C API for a more complex task. Initially, I wrote a simple example of adding two ndarrays of shape = (2,3) and type = float32. I am able to pass two numpy arrays into c functions, read their dimensions and data and…
Abarajithan
  • 333
  • 4
  • 12