Bind Python and C @ COSCUP 2015

32
Bind Python and C C Extensions for Python 3 StarNight @ COSCUP 2015

Transcript of Bind Python and C @ COSCUP 2015

Page 1: Bind Python and C @ COSCUP 2015

Bind Python and C C Extensions for Python 3

StarNight @ COSCUP 2015

Page 2: Bind Python and C @ COSCUP 2015

Who am I?

潘建宏 / Jian-Hong Pan (StarNight)About Me : http://about.me/StarNight

出沒在~

GitHub : starnightPTT : zack2004plurk : StarNightFacebook : Jian-Hong Pan

目前繼續在種花店當個打雜園丁 ~

Page 3: Bind Python and C @ COSCUP 2015

Outline

● Standing on the shoulders of giants

● What I want

● Binding flow

● Examples:○ Without arguments

○ With passed arguments

○ With variables return

● Difference of binding between Python 2 & 3

Page 4: Bind Python and C @ COSCUP 2015

Stand on the Shoulders of Giants

● Cython - Making Python as Fast as C by Mosky

● The Future of GUI Programming with Python by TP

● "My grand scheme was to build a C API for the web that's what I thought PHP was." by Rasmus Lerdorf (The father of PHP)

● Many languages make wrappers bind C!

Page 5: Bind Python and C @ COSCUP 2015

What I Want

● Python is convenient and good to make prototype.

● I am familier with C more. (是個慣C

● Therefore, I write C with my “right hand”, and write Python with my “left hand”.

From 灌籃高手漫畫

Page 6: Bind Python and C @ COSCUP 2015

What I want (Cont.

● Compiled C program is more efficient than Python.

● My OS default enviroment is “Python 3”.● To be convenient to make prototype and to

have efficiency at the same time, I should make Python 3 call some C libraries written by myself, not Cython.

● Python 3 with C extensions.

Page 7: Bind Python and C @ COSCUP 2015

test.py lib***.hlib***.c

bind.c

setup.py

C extension

importmodule

Binding Flow

Page 8: Bind Python and C @ COSCUP 2015

Binding Flow (cont.

1. test.py is the usual Python application.2. The module is composited with lib***.h and

lib***.c.3. To be imported by Python, the bind.c is the

wrapper of the module.4. setup.py tells compiler how to build and

output the wrapped C extension module.5. test.py imports C extension module.

Page 10: Bind Python and C @ COSCUP 2015

#include <python.h>To access the internal Python API.

PEP 3121

Page 11: Bind Python and C @ COSCUP 2015

00-HelloWorld: Without Arguments

● libmypy.h & libmypy.c:○ Function declaration & Implementation○ PyObject * hello(PyObject *self)

● bind.c:○ Define Python Methods: helloworld_funcs○ Define Python Module: helloworld_mod

○ Initial Module Function: PyInit_helloworld calls PyModule_Create(&helloworld_mod)

points to the returned object points to this module object

Page 12: Bind Python and C @ COSCUP 2015

Define Python Methods

struct PyMethodDef { const char *ml_name; // name of method PyCFunction ml_meth; // pointer to the C implementation int ml_flags; // flag bits const char *ml_doc; // pointer to the description string};typedef struct PyMethodDef PyMethodDef;

PS. flag bits could be:METH_VARARGS, METH_KEYWORDS,METH_NOARGS, METH_O

Page 13: Bind Python and C @ COSCUP 2015
Page 14: Bind Python and C @ COSCUP 2015

Define Python Module

typedef struct PyModuleDef{ PyModuleDef_Base m_base; // PyModuleDef_HEAD_INIT const char* m_name; // pointer to the module name const char* m_doc; // pointer to the module description Py_ssize_t m_size; // -1 in this example PyMethodDef *m_methods; // previous PyMethodDef inquiry m_reload; // should be NULL traverseproc m_traverse; // could be NULL inquiry m_clear; // could be NULL freefunc m_free; // could be NULL}PyModuleDef;

Page 15: Bind Python and C @ COSCUP 2015
Page 16: Bind Python and C @ COSCUP 2015

00-HelloWorld: Without Arguments

● setup.py:from distutils.core import setup, Extensionsetup(

name = "helloworld",version = "1.0",ext_modules = [Extension("helloworld",

["bind.c", "libmypy.c"])]);

Page 17: Bind Python and C @ COSCUP 2015

00-HelloWorld: Without Arguments

● Makefile:python setup.py build_ext --inplace

● test.py:import helloworldprint(helloworld.hello());help(helloworld);

Page 18: Bind Python and C @ COSCUP 2015

module name module description

method namemethod description

Page 19: Bind Python and C @ COSCUP 2015

01-HeyMan: With Passed Arguments

● libmypy.h & libmypy.c:○ Add heyman function○ PyObject * heyman(PyObject *self, PyObject *args);

○ heyman calls PyArg_ParseTuple(args, "is", &num, &name)

● bind.c○ Add heyman to defined Python Methods:

helloworld_funcs

points to passed arguments object

int

characterstring

Page 20: Bind Python and C @ COSCUP 2015
Page 21: Bind Python and C @ COSCUP 2015

01-HeyMan: With Passed Arguments

● test.pyimport helloworldprint(helloworld.hello());print(helloworld.heyman(5, "StarNight"));help(helloworld);

Page 22: Bind Python and C @ COSCUP 2015
Page 23: Bind Python and C @ COSCUP 2015

02-Add: With Variable Return

● libmypy.h & libmypy.c:○ Add add function○ PyObject * add(PyObject *self, PyObject *args);

○ add calls return Py_BuildValue("is", num1 + num2, eq)

● bind.c○ Add add to defined Python Methods:

helloworld_funcs

points to the returned object

characterstring

int

Page 24: Bind Python and C @ COSCUP 2015
Page 25: Bind Python and C @ COSCUP 2015

02-Add: With Variable Return

● test.pyimport helloworldprint(helloworld.hello());print(helloworld.heyman(5, "StarNight"));print(helloworld.add(5, 6));help(helloworld);

Page 26: Bind Python and C @ COSCUP 2015
Page 27: Bind Python and C @ COSCUP 2015

Recap

1. Include python.h2. Declare and implement functions you want.3. Define Python methods’ mapping table with

PyMethodDef structures which wraps the functions.

4. Define Python module with PyModuleDef structure which is the namespace of the methods’ mapping table.

5. Implement the initial function which initials the module.

Page 28: Bind Python and C @ COSCUP 2015

03-CrossVersion

● It is the difference of binding between Python 2 & 3.

● bind.c○ Python 3 has module definition structure

(PyModuleDef), but Python 2 does not.○ Python 3’s Initial funciton:

■ PyInit_<module name> calls PyModule_Create○ Python 2’s Initial funciton:

■ init<module name> calls Py_InitModule3

Page 29: Bind Python and C @ COSCUP 2015

Module Definition in Python 3

Python 3’s initial function

Python 2’s initial function

Page 30: Bind Python and C @ COSCUP 2015
Page 32: Bind Python and C @ COSCUP 2015

Thank you ~