Python 3 Extension Programming with C & Others Languages
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In our previous Python tutorial, we have studiedPython Database Access. Here, we will see Python 3 Extension Programming with C & Others Languages.
Moreover, we will study the structure of Python Extension Module and setup.py Script.
Along with this, we will discuss how to import Python 3 Extension Programming and how to write extensions for Python.
So, let’s begin Python 3 Extension Programming.
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.
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.
1. 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.
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.
2. 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
3. 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; }
4. 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} };
a. 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])
Wrapping
A wrapper is a function that calls another. Here, a wrapper binds a Python object to a C function.
Let’s see a couple of different ways to do this. So far, we’ve only seen how to do this manually.
1. SWIG
SWIG is an acronym for Simple Wrapper Interface Generator. While it supports many languages, let’s consider Python for now.
a. A makefile
all:
swig -python -c++ -o _swigdemo_module.cc swigdemo.i
python setup.py build_ext –inplace
b. SWIG wrapper file
%module swigdemo
%{
#include <stdlib.h>
#include “hello.h”
%}
%include “hello.h”
c. setup.py script
from distutils.core import setup, Extension
extension_mod = Extension(“_swigdemo”, [“_swigdemo_module.cc”, “hello.c”])
setup(name = “swigdemo”, ext_modules=[extension_mod])
2. Pyrex
Pyrex is a hybrid of C and Python. Let’s try wrapping with this.
a. .pyx file
cdef extern from “hello.h”:
char * hello(char *str) #This takes the symbol ‘hello’ from hello.h.
def hello_fn(str):
return hello(str)
b. setup.py file
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext
setup(
name=”hello”,
ext_modules=[ Extension(“hellomodule”, [“hellomodule.pyx”, “hello.c”]) ],
cmdclass={‘build_ext’: build_ext}
)
How to Import Python 3 Extension
You can import an extension like you’d normally import a module in Python-
import module_func
print(module_func.my_func())
So, this was all about Python 3 Extension Programming. Hope you like our explanation.
Python Interview Questions on Extension Programming
- What is the extension of Python program?
- What is the extension of Python library module?
- Which filename extensions are associated with Python?
- Can Python and C++ work together?
- What is Python C Extension?
Conclusion
Hence, in this Python Extend tutorial, we saw how to perform extension programming using C and Python.
In addition, we learned how to import Python 3 Extension Programming and how to write extensions for Python. That’s all for today.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google
Hello, thanks for all this valuable information in this Python tutorial!
I just wanted to add one thing.
Now you can use Cython to call C/C++ code into Pyhon code. This can potentially ease the interfacing between both programming languages. Moreover, ctypes (https://docs.python.org/2/library/ctypes.html) can also be used to interface C code with Python.
Kevin
Hi Kevin,
We are happy that our content helps you gain knowledge. Thank you for your helpful information!