Python extension programming with c

Python Programming/Extending with C

Python modules can be written in pure Python but they can also be written in the C language. The following shows how to extend Python with C.

Contents

Using the Python/C API [ edit | edit source ]

A minimal example [ edit | edit source ]

To illustrate the mechanics, we will create a minimal extension module containing a single function that outputs «Hello» followed by the name passed in as the first parameter.

We will first create the C source code, placing it to hellomodule.c:

#include static PyObject* say_hello(PyObject* self, PyObject* args)  const char* name; if (!PyArg_ParseTuple(args, "s", &name)) return NULL; printf("Hello %s!\n", name); Py_RETURN_NONE; > static PyMethodDef HelloMethods[] =  "say_hello", say_hello, METH_VARARGS, "Greet somebody.">, NULL, NULL, 0, NULL> >; PyMODINIT_FUNC inithello(void)  (void) Py_InitModule("hello", HelloMethods); > 

Then we will need a setup file, setup.py:

from distutils.core import setup, Extension module1 = Extension('hello', sources = ['hellomodule.c']) setup (name = 'PackageName', version = '1.0', description = 'This is a demo package', ext_modules = [module1]) 

Then we can build the module using a procedure whose details depends on the operating system and the compiler suite.

Building with GCC for Linux [ edit | edit source ]

Before our module can be compiled, you must install the Python development headers if you have not already. On Debian and Debian-based systems such as Ubuntu, these can be installed with the following command:

$ sudo apt install python-dev

On openSUSE, the required package is called python-devel and can be installed with zypper :

$ sudo zypper install python-devel

Now that Python.h is available, we can compile the module source code we created in the previous section as follows:

The will compile the module to a file called hello.so in build/lib.linux-i686-x.y.

Building with GCC for Microsoft Windows [ edit | edit source ]

Microsoft Windows users can use MinGW to compile the extension module from the command line. Assuming gcc is in the path, you can build the extension as follows:

python setup.py build -cmingw32

The above will produce file hello.pyd, a Python Dynamic Module, similar to a DLL. The file will land in build\lib.win32-x.y.

An alternate way of building the module in Windows is to build a DLL. (This method does not need an extension module file). From cmd.exe, type:

gcc -c hellomodule.c -I/PythonXY/include gcc -shared hellomodule.o -L/PythonXY/libs -lpythonXY -o hello.dll

where XY represents the version of Python, such as «24» for version 2.4.

Building using Microsoft Visual C++ [ edit | edit source ]

With VC8, distutils is broken. Therefore, we will use cl.exe from a command prompt instead:

cl /LD hellomodule.c /Ic:\Python24\include c:\Python24\libs\python24.lib /link/out:hello.dll

Using the extension module [ edit | edit source ]

Change to the subdirectory where the file hello.so resides. In an interactive Python session you can use the module as follows.

>>> import hello >>> hello.say_hello("World") Hello World!

A module for calculating Fibonacci numbers [ edit | edit source ]

In this section, we present a module for Fibonacci numbers, thereby expanding on the minimal example above. Compared to the minimal example, what is worth noting is the use of «i» in PyArg_ParseTuple() and Py_BuildValue().

The C source code in (fibmodule.c):

#include int _fib(int n)  if (n  2) return n; else return _fib(n-1) + _fib(n-2); > static PyObject* fib(PyObject* self, PyObject* args)  int n; if (!PyArg_ParseTuple(args, "i", &n)) return NULL; return Py_BuildValue("i", _fib(n)); > static PyMethodDef FibMethods[] =  "fib", fib, METH_VARARGS, "Calculate the Fibonacci numbers.">, NULL, NULL, 0, NULL> >; PyMODINIT_FUNC initfib(void)  (void) Py_InitModule("fib", FibMethods); > 
from distutils.core import setup, Extension module1 = Extension('fib', sources = ['fibmodule.c']) setup (name = 'PackageName', version = '1.0', description = 'This is a demo package', ext_modules = [module1]) 

Using SWIG [ edit | edit source ]

SWIG is a tool that helps a variety of scripting and programming languages call C and C++ code. SWIG makes creation of C language modules much more straightforward.

To use SWIG, you need to get it up and running first.

You can install it on an Ubuntu system as follows:

$ sudo apt-get install swig $ sudo apt-get install python-dev

To get SWIG for Windows, you can use binaries available from the SWIG download page.

Once you have SWIG, you need to create the module source file and the module interface file:

#include void say_hello(const char* name)  printf("Hello %s!\n", name); > 
%module hello extern void say_hello(const char* name); 

Then we let SWIG do its work:

The above produces files hello.py and hello_wrap.c.

The next step is compiling; substitute /usr/include/python2.4/ with the correct path to Python.h for your setup:

gcc -fpic -c hellomodule.c hello_wrap.c -I/usr/include/python2.4/

As the last step, we do the linking:

gcc -shared hellomodule.o hello_wrap.o -o _hello.so -lpython

The module is used as follows:

>>> import hello >>> hello.say_hello("World") Hello World!

Источник

Extending and Embedding the Python Interpreter¶

This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can not only define new functions but also new object types and their methods. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.

This document assumes basic knowledge about Python. For an informal introduction to the language, see The Python Tutorial . The Python Language Reference gives a more formal definition of the language. The Python Standard Library documents the existing object types, functions and modules (both built-in and written in Python) that give the language its wide application range.

For a detailed description of the whole Python/C API, see the separate Python/C API Reference Manual .

This guide only covers the basic tools for creating extensions provided as part of this version of CPython. Third party tools like Cython, cffi, SWIG and Numba offer both simpler and more sophisticated approaches to creating C and C++ extensions for Python.

The Python Packaging User Guide not only covers several available tools that simplify the creation of binary extensions, but also discusses the various reasons why creating an extension module may be desirable in the first place.

Creating extensions without third party tools¶

This section of the guide covers creating C and C++ extensions without assistance from third party tools. It is intended primarily for creators of those tools, rather than being a recommended way to create your own C extensions.

Оцените статью