Python 3 Extension Programming
Malini Shukla
Senior Data Scientist || Hiring || 6M+ impressions || Trainer || Top Data Scientist || Speaker || Top content creator on LinkedIn || Tech Evangelist
Python 3 Extension Programming
Python 3 extension Programming, is any code that we write in any other language like C, C++, or Java Programming Language. We can import or integrate it into a Python script. So, this tutorial is essentially one on how to write and import extensions for Python. Extensions let Python communicate with other languages.
The .dll files (dynamically linked libraries) you may see on your Windows or the. So, files you notice on your Unix are libraries. In effect, extension modules are libraries.
Read about Python Collections Module in detail
Structure of Python 3 Extension Module
A Python 3 extension Programming module will have the following parts:
- Header file- python.h
- An initialization function.
- Functions in other languages.
- A table to map the names of the functions.
Let’s look at each of this one by one.
a. Header File- python.h
Let’s write Python 3 extension for C. So, we must include this header file in our C source file. We put this include before all others. We also succeed this with the Python functions we want to call. This header file lets us access the internal Python API.
In the Python header, all types and functions begin with the prefix ‘Py’/’PY’. For parsing data between Python and C, we have the Python object pointer. This is the PyObject. Here’s an example:
static PyObject* myFunc(PyObject* self)
This header file also has some other functions:
- PyArg_ParseTuple(args, format, …)- This gets arguments from Python.
- Py_BuildValue(format, …)- This turns values into PyObject pointers.
- PyModule_Create(moduleDef)- This initializes the module; wraps method pointers using module definitions.
Follow this link to know Python Built-In Functions with Syntax and Examples
For functions that return nothing, we use the value Py_None.
The PyMethodDef contains the binding information. This structure ends with terminating NULL and 0 values.
b. Initialization Function
When the interpreter loads your extension module, it calls this function. This is the last part of the Python 3 extension. You should name this as following- if you call your module ‘Sound’, then name this function ‘initSound’. It will look like this:
- PyMODINIT_FUNC initModule() {
- Py_InitModule3(func, module_methods, docstring);
- }
Here, we have three parameters-
- func- The function to export
- module_methods- Mapping table
- docstring- Comment
c. C Functions to Call
We can use one of three forms to return a Python object:
- static PyObject *MyFunction( PyObject *self, PyObject *args );
- static PyObject *MyFunctionWithKeywords(PyObject *self,
PyObject *args,
PyObject *kw);
- static PyObject *MyFunctionWithNoArgs( PyObject *self );
When we use the Py_RETURN_NONE macro that the Python headers have to offer, we can have a function return None. This is equivalent to void in C. These are static functions. Here’s an example-
- static PyObject * module_func(PyObject * self, PyObject * args)
- {
- char * input;
- char * result;
- PyObject * ret;
- //Parsing arguments
- if(!PyArg_ParseTuple(args, "s", &input)){
- return NULL;
- }
- //Running actual function
- result=hello(input);
- //Building a Python object from this string
- ret=PyString_FromString(result);
- free(result);
- return ret;
- }
d. Symbol/ Mapping Table
You need to register the function(s) in a symbol table for a module. For Python, all functions live in a module- even C functions.
This is a kind of PyMethodDef:
- static PyMethodDef module_methods[]={
- {"my_func", (PyCFunction)module_func, METH_NOARGS, NULL},
- {NULL, NULL, 0, NULL}
- };
Let’s Explore Python Modules vs Packages
i. Parameters
Now here, we have four parameters-
- Function name- How the interpreter presents it; here, it is my_func
- Function address- Here, it is (PyCFunction)module_func
- Flag- This can be of three kinds:
METH_VARARGS
METH_NOARGS- No arguments
Bitwise OR with METH_KEYWORDS- For working with keyword arguments
- Docstring- When you don’t want to provide one, you can use NULL.
We terminate this with a sentinel holding the NULL and 0. In the example, we have used {NULL, NULL, 0, NULL}.
In this table, we also put pointers to the C functions.
setup.py Script
After writing a Python-callable function, registering it in the module’s symbol table, and writing an initialization function, we write a setup.py script.
from distutils.core import setup, Extension
#Extension module for C/ C++
extension_mod=Extension(“hello”, [“hellomodule.c”, “hello.c”])
setup(name=”hello”, ext_modules=[extension_mod])
Read Complete Article>>
See Also-
- Python – Interview Questions Part 1
- Python – Interview Questions Part 2
- Python – Interview Questions Part 3