Python 3 Extension Programming with C & Others Languages

1. Objective

In our previous Python tutorial, we have studied Python 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 with C & Others Languages

Python 3 Extension Programming with C & Others Languages

2. 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 

3. 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.

Python 3 Extension Programming with C & Others Languages

Python 3 Extension Programming with C & Others Languages

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.

4. 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])

5. 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.

Python 3 Extension Programming with C & Others Languages

Python 3 Extension Programming with C & Others Languages

a. SWIG

SWIG is an acronym for Simple Wrapper Interface Generator. While it supports many languages, let’s consider Python for now.
i. A makefile
all:
swig -python -c++ -o _swigdemo_module.cc swigdemo.i
python setup.py build_ext –inplace
Read about Python Datetime Module with Quick Examples
ii. SWIG wrapper file
%module swigdemo
%{
#include <stdlib.h>
#include “hello.h”
%}
%include “hello.h”
iii. 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])

b. Pyrex

Pyrex is a hybrid of C and Python. Let’s try wrapping with this.
i. .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)
ii. 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}
)

Python Interview Questions

6. 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.

7. 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. Got a topic in Python you would like us to cover? Let us know in the comments.
Related Topic- XML Processing in Python 3
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *