Use Cython for Python write extension 1: First knowledge of Cython
Cython makes Writing C extensions for Python as easy as writing Python code. Widely used in mathematical software packages, SAGE, as a fast and scalable operation. It provides a secure and maintainable way to build a native Python module by automatically generating the required code.
We often use Cython to bind the C/C ++ Implementation System to Python, so that we can use Python to handle high-level logic and native modules to process underlying code.
Sample Code
Http://git.oschina.net/erhuabushuo/learning-cython
Version:
- Python: 3.3.
- Cython: 0.19
Note:: If the version you are using is inconsistent, you may need to modify the example as needed.
Install Cython
Install Cython first. Think of Cython like Bison, flex, or GCC. It accepts the input source code and generates other compilation and link code:
- Fedora: yum install Cython3
- Ubuntu/Debian: apt-get install Cython3
- Curl-O http://www.cython.org/release/Cython-0.19.tar.gztar xzvf Cython-0.19.tar.gzcd Cython-0.19sudo python3 setup. py install
- Windows http://wiki.cython.org/InstallingOnWindows.
- Pip: pip3 install cython
Hello China
Run the following command to check cython installation:
$ cython3 --versionCython version 0.19
Hello. pyx
Print ("Hello China! ")
Here, the Cython Code simply prints "Hello China! "
Makefile
all: cython3 -3 -o hello.c hello.pyx gcc -g -O2 -fpic -c hello.c -o hello.o `python3.3-config --cflags` gcc -g -O2 -shared -o hello.so hello.o `python3.3-config --libs`
Compile Link
$ make
Now we have created the hello. so module.
Use
Python3Python 3.3.2 + (default, Feb 28 2014, 00:52:16) [GCC 4.8.1] on linuxType "help", "copyright", "credits" or "license" for more information. >>> import hello China! >>>
We directly import the module through import
Explanation
Cython uses. pyx and. pxd as the file extension. Now we only care about the. pyx file. Later, I will introduce the use of. pxd.
Let's see how Cython works.
Create your own module
Now we have seen the "Hello China" module. Let's see how to write your own module. You can link some of your own code. Later, we will introduce how to wrap your code.
Execute your C code
When Cython is used, the Python superset. Although the syntax and keywords both work the same, we should also find out the difference between Python and Cython. Let's build a module similar to the hello-world style, but do some other simple things.
Mycode. c
# Include <stdio. h> int myfunc (int a, int B) {printf ("now in C Code \ n"); return a + B ;}
This is the C code we want to call. Execute a simple operation to calculate the sum of two numbers. Now we use Python to call it. Open the mycode. h file and define the Cython prototype.
Mycode. h
#ifndef __MYCODE_H__#define __MYCODE_H__extern int myfunc(int, int);#endif //__MYCODE_H__
We need to let Cython know the prototype of the function to be called. In practice, you may have defined the prototype in the project header file. Create mycodecpy. pyx and write the following code:
cdef extern from "mycode.h": cdef int myfunc(int, int)def callCfunc(): print(myfunc(1, 2))
In Cython code, we define the C code we want to use,CdefThe keyword identifies the C code to be linked. Now we have defined the header file to be used. I will explain in detail how Python calls native code because it is dangerous to directly call C code. Therefore, Cython handles all types of conversions for us. The basic function packaging, callCfunc, calls the myfunc function and passes an integer, and then simply prints the result.
Compile with the following command:
$ cython3 mycodecpy.pyx$ gcc -g -O2 -fpic -c mycode.c -o mycode.o$ gcc -g -O2 -fpic -c mycodecpy.c -o mycodecpy.o `python3.3-config --cflags`$ gcc -shared -o mycodecpy.so mycode.o mycodecpy.o `python3.3-config --libs`
We want to link mycode. c code. Compile all C files as object files and link all object files into a binary file. Therefore, make sure that you have linked all the required object files.
$ Python3 >>> from mycodecpy import callCfunc >>> callCfunc () Now we are in C code 3
Therefore, we have compiled and used Python code to call our native code. You have begun to bind some other native modules.
Type conversion
You may have noticed that we call the Cython function without parameters. What if we want to include parameters?
def callCfunc(int x, int y): print(myfunc(x, y))
Now we have added two int parameters to our defined Python packaging function. The Python code type must be safely converted to the C type in PyObjects. When you create a Python integer object. This type is not integer but PyObject. If you want to use it in C, you need to obtain the value through Python c api, but Cython automatically does this for you. For example, if you pass an invalid parameter, you will get:
>>> callCfunc(1, 'string')Traceback (most recent call last): File "<stdin>", line 1, in <module> File "mycodecpy.pyx", line 4, in mycodecpy.callCfunc (mycodecpy.c:687) def callCfunc(int x, int y):TypeError: an integer is required
When you add more types of security objects, you will find that the speed and code will be improved. Because the Cython compiler can optimize the code to avoid calling using Python.