C API from Extension Module in Python | Set 1
Last Updated :
09 Sep, 2024
Suppose given a C extension module that internally defines a variety of useful functions that can be exported as a public C API for use elsewhere. Now if we want to use these functions inside other extension modules. Then, it is important to know how to link them together but doing it with the C compiler/linker seems excessively complicated.
Code #1 : [C code] Point objects including some utility functions
C
# Destructor function for points
static void del_Point(PyObject *obj)
{
free(PyCapsule_GetPointer(obj, "Point"));
}
static PyObject *PyPoint_FromPoint(Point *p, int must_free)
{
return PyCapsule_New(p, "Point", must_free ? del_Point : NULL);
}
# Utility functions
static Point *PyPoint_AsPoint(PyObject *obj)
{
return (Point *) PyCapsule_GetPointer(obj, "Point");
}
Now, the issue to deal with is how to handle the exportation of the PyPoint_AsPoint()
and PyPoint_FromPoint()
functions as an API that can be used by and can link to other extension modules. (For example - any other extensions also want to use the wrapped Point objects).
Code #2 : Introducing a new header file called Pythonsample.h
for the work
extension.
C
//pythonsample.h
#include "Python.h"
#include "work.h"
#ifdef __cplusplus
extern "C" {
#endif
// Public API Table
typedef struct
{
Point *(*aspoint)(PyObject *);
PyObject *(*frompoint)(Point *, int);
} _PointAPIMethods;
#ifndef PYTHONSAMPLE_MODULE
/* Method table in external module */
static _PointAPIMethods *_point_api = 0;
Code #3 : Import the API table from "work"
C
static int import_sample(void)
{
_point_api = (_PointAPIMethods *) PyCapsule_Import("work._point_api", 0);
return (_point_api != NULL) ? 1 : 0;
}
/* Macros to implement the programming interface */
#define PyPoint_AsPoint(obj) (_point_api->aspoint)(obj)
#define PyPoint_FromPoint(obj) (_point_api->frompoint)(obj)
#endif
#ifdef __cplusplus
}
#endif
_PointAPIMethods table of function pointers is the most important feature as it will be initialized in the exporting module and found by importing modules. The code below shows how to change the original extension module to populate the table and export it.
Code #4 : Destructor and Utility Function
C
// pythonsample.c
# include "Python.h"
# define PYTHONSAMPLE_MODULE
# include "pythonsample.h"
// Destructor function for points
static void del_Point(PyObject * obj)
{
printf("Deleting point\n");
free(PyCapsule_GetPointer(obj, "Point"));
}
// Utility functions
static Point * PyPoint_AsPoint(PyObject * obj)
{
return (Point *) PyCapsule_GetPointer(obj, "Point");
}
static PyObject * PyPoint_FromPoint(Point * p, int free)
{
return PyCapsule_New(p, "Point", free ? del_Point : NULL);
}
static _PointAPIMethods _point_api =
{
PyPoint_AsPoint,
PyPoint_FromPoint
};
Code #5 : Module function
C
// Module initialization function
PyMODINIT_FUNC
PyInit_sample(void)
{
PyObject *m;
PyObject *py_point_api;
m = PyModule_Create(&samplemodule);
if (m == NULL)
return NULL;
// Add the Point C API functions
py_point_api = PyCapsule_New((void *) &_point_api, "work._point_api", NULL);
if (py_point_api)
{
PyModule_AddObject(m, "_point_api", py_point_api);
}
return m;
}
Similar Reads
C API from Extension Module in Python | Set 2 Prerequisite: C API from Extension Module in Python | Set 1 Let's see an example of a new extension module that loads and uses these API functions that we build up in the previous article. Code #1 : C #include "pythonsample.h" /* An extension function that uses the exported API */ static P
2 min read
C Extension Module using Python Writing a simple C extension module directly using Pythonâs extension API and no other tools. It is straightforward to make a handcrafted extension module for a simple C code. But first, we have to make sure that the C code has a proper header file. Code #1 : C #include <math.h> extern int gcd
4 min read
How to create modules in Python 3 ? Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t
4 min read
Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read
Introduction to Python Typing-Extensions Module The typing-extensions module provides backports of the latest typing features to ensure that developers working with older versions of Python can still leverage these advanced tools. This module acts as a bridge between future releases of Python and existing codebases, enabling us to stay up to date
8 min read
Python | Passing Filenames to Extension in C Filename has to be encoded according to the systemâs expected filename encoding before passing filenames to C library functions. Code #1 : To write an extension function that receives a filename CPP static PyObject* py_get_filename(PyObject* self, PyObject* args) { PyObject* bytes; char* filename; P
2 min read