Cython 0.20 发布,Python 的 C 语言扩展

编辑: oschina
2014-01-21 00:00:00

Cython是单独的一门语言,专门用来写在Python里面import用的扩展库。实际上Cython的语法基本上跟Python一致,而 Cython有专门的“编译器”先将 Cython代码转变成C(自动加入了一大堆的C-Python API),然后使用C编译器编译出最终的Python可调用的模块。

Cython 0.20 发布了,详细改进记录如下:

  • Support for CPython 3.4.

  • Support for calling C++ template functions.

  • yield is supported in finally clauses.

  • The C code generated for finally blocks is duplicated for each exit case to allow for better optimisations by the C compiler.

  • Cython tries to undo the Python optimisationism of assigning a bound method to a local variable when it can generate better code for the direct call.

  • Constant Python float values are cached.

  • String equality comparisons can use faster type specific code in more cases than before.

  • String/Unicode formatting using the '%' operator uses a faster C-API call.

  • bytearray has become a known type and supports coercion from and to C strings.  Indexing, slicing and decoding is optimised. Note that this may have an impact on existing code due to type inference.

  • Using cdef basestring stringvar and function arguments typed asbasestring is now meaningful and allows assigning exactlystr and unicode objects, but no subtypes of these types.

  • Support for the __debug__ builtin.

  • Assertions in Cython compiled modules are disabled if the running Python interpreter was started with the "-O" option.

  • Some types that Cython provides internally, such as functions and generators, are now shared across modules if more than one Cython implemented module is imported.

  • The type inference algorithm works more fine granular by taking the results of the control flow analysis into account.

  • A new script in bin/cythonize provides a command line frontend to the cythonize() compilation function (including distutils build).

  • The new extension type decorator @cython.no_gc_clear prevents objects from being cleared during cyclic garbage collection, thus making sure that object attributes are kept alive until deallocation.

  • During cyclic garbage collection, attributes of extension types that cannot create reference cycles due to their type (e.g. strings) are no longer considered for traversal or clearing.  This can reduce the processing overhead when searching for or cleaning up reference cycles.

  • Package compilation (i.e. files) now works, starting with Python 3.3.

  • The cython-mode.el script for Emacs was updated.  Patch by Ivan Andrus.

  • An option common_utility_include_dir was added to cythonize() to save oft-used utility code once in a separate directory rather than as part of each generated file.

  • unraisable_tracebacks directive added to control printing of tracebacks of unraisable exceptions.

Bugs fixed

  • Abstract Python classes that subtyped a Cython extension type failed to raise an exception on instantiation, and thus ended up being instantiated.

  • set.add(a_tuple) and set.discard(a_tuple) failed with a TypeError in Py2.4.

  • The PEP 3155 __qualname__ was incorrect for nested classes and inner classes/functions declared as global.

  • Several corner cases in the try-finally statement were fixed.

  • The metaclass of a Python class was not inherited from its parent class(es).  It is now extracted from the list of base classes if not provided explicitly using the Py3 metaclass keyword argument. In Py2 compilation mode, a __metaclass__ entry in the class dict will still take precedence if not using Py3 metaclass syntax, but only after creating the class dict (which may have been done by a metaclass of a base class, see PEP 3115).  It is generally recommended to use the explicit Py3 syntax to define metaclasses for Python types at compile time.

  • The automatic C switch statement generation behaves more safely for heterogeneous value types (e.g. mixing enum and char), allowing for a slightly wider application and reducing corner cases.  It now always generates a 'default' clause to avoid C compiler warnings about unmatched enum values.

  • Fixed a bug where class hierarchies declared out-of-order could result in broken generated code.

  • Fixed a bug which prevented overriding const methods of C++ classes.

  • Fixed a crash when converting Python objects to C++ strings fails.

Other changes

  • In Py3 compilation mode, Python2-style metaclasses declared by a__metaclass__ class dict entry are ignored.

  • In Py3.4+, the Cython generator type uses tp_finalize() for safer cleanup instead of tp_del().

19 收藏
4 评论
19 收藏