Introduction to python c Extension

Source: Internet
Author: User
Tags ffi

Python is a very convenient Dynamic Language. Many of the lines of code you need to use c or java may have been completed after several lines of python. Therefore, the python community has always had a slogan saying, I use python ", but it is convenient, but it also brings speed problems. Python is the most criticized for its program running speed. Therefore, many solutions have been developed in combination with c's speed and python's convenience. Note that python is written in c, so the most fundamental solution is to use the native python c api to write the c program and compile it into a link library file (the so file in linux ), then it is called directly in python, and other solutions are basically centered around this idea, just a lot of repetitive work for you. This section briefly introduces the use of python c api, swig, sip, ctypes, cython, and cffi.

 

Python c api

First, we can see that python c api is used.

# Include <Python. h>
 
StaticPyObject * add (PyObject * self, PyObject * args ){
Inta = 0;
Intb = 0;
If (! PyArg_ParseTuple (args, "I | I", & a, & B ))
ReturnNULL;
ReturnPy_BuildValue ("I", a + B );
}
 
StaticPyObject * sub (PyObject * self, PyObject * args ){
Inta = 0;
Intb = 0;
If (! PyArg_ParseTuple (args, "I | I", & a, & B ))
ReturnNULL;
ReturnPy_BuildValue ("I", a-B );
}
 
StaticPyMethodDef addMethods [] = {
{"Add", add, METH_VARARGS },
{"Sub", sub, METH_VARARGS },
{NULL, NULL, 0, NULL}
};
 
Voidinitmytest (){
Py_InitModule ("mytest", addMethods );
}
First, introduce Python. h: This header file. Therefore, you must introduce the python library during compilation. The python objects are represented by PyObject in c. The add and sub methods are defined in the program, compile the init function. Note that the init function is added with your module name, and then call the Py_InitModule function to tell python what functions you have defined. Then compile it into the so file.

Gcc mytest. c-shared-lpython2.7-L/usr/lib/python2.7/-I/usr/include/python2.7/-o mytest. so
In this way, you can import mytest in python to reference it in the same way as using other python modules.

 

Swig

The first thing to note is that swig can perform call conversion in many languages, not only for python to call c. Both swig and sip are called wrapper, which means they wrap your original functions. We can see that in the python c api method, we must strictly follow the python c api method to write code, compromising the readability of the original c program, therefore, the wrapper idea is to package the native c program into the python c api code and then generate the so file. Therefore, we need to write the c file first.

Intadd (inta, intb ){
Returna + B;
}
Intsub (inta, intb ){
Returna-B;
}
Then write an interface file in swig format.

% Module mytest
% {
Extern int add (int a, int B );
Extern int sub (int a, int B );
%}
 
Extern int add (int a, int B );
Extern int sub (int a, int B );
Then you can run swig, which will automatically generate the code written in python c api and automatically compile the so file for calling.

 

Sip

In terms of sip, sip is developed by swig to facilitate python to call c, so the basic usage is similar, but the interface files are slightly different.

% Module (name = mytest, language = "C ")
Int add (int a, int B );
Int sub (int a, int B );


Ctypes

Ctypes provides another way to call c Programs. First, ctypes is the standard library of python, So if you use ctypes, you do not need additional things. Ctypes allows you to directly write code in python to load the so file in the c library. That is to say, if you use the so file without the source file, you can still use ctypes to call it.

Fromctypesimport *
 
F = 'mytest. so'
Cdll. LoadLibrary (f)
Api = CDLL (f)
Api. add. argtypes = [c_int, c_int]
Api. add. restype = c_int
Api. sub. argtypes = [c_int, c_int]
Api. sub. restype = c_int
 
Printapi. add (3, 2)
Printapi. sub (3, 2)
It is a bit like writing interface files in python. Because it is a standard library of python, this method is still quite a lot.

 

Cython

Cython uses a syntax similar to python to write the interface that calls the c program, and can conveniently use both c and python functions. See the code to understand.

Intsub (inta, intb ){
Returna-B;
}
We can have some code written in c.

Cdef externfrom 'test. C ':
Intsub (inta, intb)
 
Defadd (inta, intb ):
Returna + B
 
Defmysub (a, B ):
Returnsub (a, B)
Then in cython, we can introduce the c file to call the functions in the c file, or call the functions in python. When cython is called, it will become a pure c file, then compile it into the so file and you can use it.

 

Cffi

Finally, cffi, cffi is similar to ctypes, which calls the c program directly in the python program, but it is more convenient than ctypes and does not need to be compiled into so before calling, note that all the methods above need to be compiled into the so file before calling in python, and cffi allows you to directly call the c file to use the functions in it. Why is it so amazing, in fact, cffi helps you compile c into a so file during the interpretation process...

FromcffiimportFFI
Ffi = FFI ()
Ffi. cdef ("""
Int add (int a, int B );
Int sub (int a, int B );
""")
Lib = ffi. verify ('# include "mytest. c "')
Printlib. add (1, 2)
This is basically the case. The last thing I feel is that the native python c api is the most difficult to write, but it is easier to control it if advanced usage is required; it is convenient to use wrapper. The advantage of ctypes is that it is a standard module of python and does not need to write additional programs (such as interface programs ); the advantage of cython is that it can conveniently call both c and python functions at the same time, and is a class of python syntax, which is very convenient to use. The advantage of CFFI is that it is more convenient to call c without compiling so. At last, this article only gives a brief introduction to various usage and does not thoroughly compare the advantages and disadvantages of each usage. Therefore, if you want to learn more, go to the official documentation...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.