Now that you've seen the "Hello World" module, let's see how you can write your own module to do something! Then, you can link it against some of your own code. Later, we'll introduce the idea of wrapping your code.

Calling into your C code

Cython is a superset of Python. Although the syntax and keywords will work in the same way, we should be careful when talking about Python and Cython for clarity. To see Cython in action, let's build a hello-world-style module but perform something basic, just to be sure we are on the same page.

Open a file called mycode.c and insert the following code in to it:

#include <stdio.h> int myfunc (int a, int b) { printf ("look we are within your c code!!

"); return a + b; }

This is the C code we will callâjust a simple function to add two integers you've probably seen before. Now let's get Python to call it. Open a file called mycode.h , wherein we will declare our prototypes for Cython as follows:

#ifndef __MYCODE_H__ #define __MYCODE_H__ extern int myfunc (int, int); #endif //__MYCODE_H__

We need this so that Cython can see the prototype for the function we want to call. In practice, you will already have your headers in your own project with your prototypes and declarations.

Open a file called mycodecpy.pyx and insert the following code in to it:

cdef extern from "mycode.h": cdef int myfunc (int, int) def callCfunc (): print myfunc (1,2)

Within this Cython code, we initially have to declare what C code we care about. cdef is a keyword signifying that this is from the C code that will be linked in. Now that we have declared the header with the prototype to squash any undeclared function warnings from our compiler, we can make a wrapper function. At this point, we will specify how Python will call this native code, since calling directly into C code is dangerous. Therefore, Cython handles all type-conversion problems for us. A basic wrapper function, callCfunc , is all we needâit calls the myfunc function and passes the integers 1 and 2 ; then it simply prints the result.

To compile this, use the following:

$ cython mycodecpy.pyx $ gcc -g -O2 -fpic -c mycode.c -o mycode.o $ gcc -g -O2 -fpic -c mycodecpy.c -o mycodecpy `python-config --cflags` $ gcc -shared -o mycodecpy.so mycode.o mycodecpy.o `python-config --libs`

We have to remember to link in the code that has the C function; in this case, mycode.c . If you're not familiar with what I mean here, you may need to revisit some C tutorials on compilation, as every C file is compiled to an object file and then we link all object files into a binary. Therefore, you need to be sure you link in all necessary object files.

[email protected]:~/workspace/cython-book/chapter1/ownmodule$ python Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from mycodepy import callCfunc >>> callCfunc () look we are within your c code!! 3

So, we have now compiled and called our native code from Python code. I hope this gives you an idea of what you can do with it. With this, you can have a native module linking against some native libraries, making bindings to such libraries very simple.