1 ==============================================
2 The Cython Compiler for C-Extensions in Python
3 ==============================================
10 http://www.cython.org/
12 cython-dev@codespeak.net
14 .. footer:: Dr. Stefan Behnel, EuroPython 2008, Vilnius/Lietuva
16 .. include:: <s5defs.txt>
22 * Passionate Python developer since 2002
24 * after Basic, Logo, Pascal, Prolog, Scheme, Java, ...
26 * CS studies in Germany, Ireland, France
28 * PhD in distributed systems in 2007
30 * Language design for self-organising systems
32 * Darmstadt University of Technologies, Germany
34 * Current occupations:
36 * Employed by Senacor Technologies AG, Germany
38 * IT transformations, SOA design, Java-Development, ...
40 * »lxml« OpenSource XML toolkit for Python
42 * http://codespeak.net/lxml/
52 * an Open-Source project
56 * a Python compiler (almost)
58 * an enhanced, optimising fork of Pyrex
60 * an extended Python language for
62 * writing fast Python extension modules
64 * interfacing Python with C libraries
67 A little bit of history
68 =======================
70 * April 2002: release of Pyrex 0.1 by Greg Ewing
72 * Greg considers Pyrex a language in design phase
74 * Pyrex became a key language for many projects
76 * over the years, many people patched their Pyrex
78 * not all patches were answered by Greg (not in time)
80 * minor forks and enhanced branches followed
82 * March 2006: my fork of Pyrex for »lxml« XML toolkit
84 * November 2006: »SageX« fork of Pyrex
86 * by Robert Bradshaw, William Stein (Univ. Seattle, USA)
88 * context: »Sage«, a free mathematics software package
90 * 28th July 2007: official Cython launch
92 * integration of lxml's Pyrex fork into SageX
94 * the rest is in http://hg.cython.org/cython-devel/
97 Major Cython Developers
98 =======================
100 * Robert Bradshaw and Stefan Behnel
106 * main developer and maintainer of Pyrex
108 * we happily share code and discuss ideas
110 * Dag Sverre Seljebotn
112 * Google Summer-of-Code developer
114 * NumPy integration, many ideas and enhancements
116 * many, *many* others - see
120 * the mailing list archives of Cython and Pyrex
126 * you write Python code
128 * Cython translates it into C code
130 * your C compiler builds a shared library for CPython
132 * you import your module
134 * Cython has support for
136 * compile-time includes/imports
138 * with dependency tracking
142 * *optionally* compile Python code from setup.py!
145 Example: compiling Python code
146 ==============================
152 .. sourcecode:: python
154 class HardWorker(object):
156 def __init__(self, task):
167 * translates to 842 line `.c file <ep2008/worker.c>`_ (Cython 0.9.8)
169 * lots of portability #define's
171 * tons of helpful C comments with Python code snippets
173 * a lot of code that you don't want to write yourself
179 * Cython compiler generates C code that compiles
181 * with all major compilers (C and C++)
183 * on all major platforms
185 * in Python 2.3 to 3.0 beta1
187 * Cython language syntax follows Python 2.6
189 * optional Python 3 syntax support is on TODO list
191 * get involved to get it quicker!
193 \... the fastest way to port Python 2 code to Py3 ;-)
196 Python 2 feature support
197 ========================
199 * most of Python 2 syntax is supported
201 * top-level classes and functions
205 * object operations, arithmetic, ...
207 * plus some Py3/2.6 features:
209 * keyword-only arguments
211 * unicode literals via ``__future__`` import
213 * in recent developer branch:
217 * closures (i.e. local classes and functions) are close!
223 Cython generates very efficient C code
225 * according to PyBench:
227 * conditions and loops run 2-8x faster than in Py2.5
229 * most benchmarks run 30%-80% faster
231 * overall more than 30% faster for plain Python code
233 * optional type declarations
235 * let Cython generate plain C instead of C-API calls
237 * make code several times faster than the above
243 * »cdef« keyword declares
245 * local variables with C types
247 * functions with C signatures
249 * classes as builtin extension types
253 def stupid_lower_case(char* s):
254 cdef Py_ssize_t size, i
257 for i in range(size):
258 if s[i] >= 'A' and s[i] <= 'Z':
270 $ cat stupidlowercase.py
274 def stupid_lower_case(char* s):
275 cdef Py_ssize_t size, i
278 for i in range(size):
279 if s[i] >= 'A' and s[i] <= 'Z':
285 $ cython --annotate stupidlowercase.py
287 => `stupidlowercase.html <ep2008/stupidlowercase.html>`_
295 cdef extern from "Python.h":
296 # copied from the Python C-API docs:
297 object PyUnicode_DecodeASCII(
298 char* s, Py_ssize_t size, char* errors)
300 cdef extern from "string.h":
304 cdef slightly_better_lower_case(char* s):
305 cdef Py_ssize_t i, size = strlen(s)
306 for i in range(size):
307 if s[i] >= 'A' and s[i] <= 'Z':
309 return PyUnicode_DecodeASCII(s, size, NULL)
315 * Dynamic classes and functions with closures
317 .. sourcecode:: python
320 def closure_function(c):
322 return closure_function
324 * Native support for new ``buffer`` protocol
326 * part of NumPy integration by Dag Seljebotn
328 .. sourcecode:: python
330 def inplace_negative_grayscale_image(
331 ndarray[unsigned char, 2] image):
333 for i in range(image.shape[0]):
334 for j in range(image.shape[1]):
335 arr[i, j] = 255 - arr[i, j]
338 Huge pile of great ideas
339 ========================
341 * Cython Enhancement Proposals (CEPs)
343 * http://wiki.cython.org/enhancements
345 * native pickle support for extension classes
347 * meta-programming facilities
349 * type inference strategies
351 * compile-time assertions for optimisations
353 * object-like C-array handling
355 * improved C++ integration
363 * Cython is a tool for
365 * efficiently translating Python code to C
367 * easily interfacing to external C libraries
371 * speed up existing Python modules
373 * concentrate on optimisations, not rewrites!
375 * write C extensions for CPython
377 * don't change the language just to get fast code!
379 * wrap C libraries *in Python*
381 * concentrate on the mapping, not the glue!
384 ... but Cython is also
385 ======================
389 * a very open playground for great ideas!
397 **C-Extensions in Python**
399 \... use it, and join the project!