platform : linux mint 17 / osx 10.11.3
example.c : the C extension function(s)
There are 3 parts in wrap.c
A. Exported function
- Deal with function forms in C, should be one of the following
- PyObject *MyFunction(PyObject *self, PyObject *args);
- PyObject *MyFunctionWithKeywords(PyObject *self, PyObject *args, PyObject *kw);
- PyObject *MyFunctionWithNoArgs(PyObject *self);
- Deal with args parsing while programming in C.
B. Method list
C. Initialization function
- Initialization function have different forms in python 2.x and 3.x
- Some structure members of PyModuleDef could just set as default value, check it while programming.
To avoid platform dependency problem, I strongly suggested to use python lib "distutils".
for python 2.x
$ python setup.py build
for python 3.x
$ python3 setup.py build
then go to "./build/" to find what you need.
>>> import example
>>> example.fact(6)
720
$ gcc -fPIC -o example.so -shared example.c
>>> from ctypes import *
>>> example = cdll.LoadLibrary("./example.so")
>>> example.fact(6)
720
Deal with args parsing by using ctypes while programming in python.
swig use example.i to generate wrap.c
$ swig -python -o wrap.c example.i
You can omit this step by fill in swig parameters in setup.py
To avoid platform dependency problem, I strongly suggested to use python lib "distutils".
distutils support swig.
http://www.boost.org/doc/libs/master/libs/python/doc/html/index.html
If you are familiar with boost lib, try this.
http://cython.org/
Infact, cython is another language but very similiar to python.
for wrap.c
https://docs.python.org/2.7/extending/index.html#extending-index
https://docs.python.org/2.7/c-api/index.html
https://docs.python.org/3.5/extending/index.html#extending-index
https://docs.python.org/3/c-api/index.html
for setup.py
https://docs.python.org/2/distutils/apiref.html
https://docs.python.org/3.5/distutils/apiref.html
for ctypes
https://docs.python.org/2/library/ctypes.html
https://docs.python.org/3.5/library/ctypes.html
write fit with both python 2.x and 3.x
http://python3porting.com/cextensions.html
Note :
- reference counter
- callback