Cython 0.20 发布,兼有C的效率和Python的优美的编程语言

jopen 10年前

Cython 是一门基于 Python 的编程语言, 通过额外的语法允许可选择的静态类型声明。 它的目标是成为 Python 的超集, 而Python 赋予它高级,面向对象,函数式以及动态编程等特性。 使用 Cython 编写的源代码会被转换成优化过的 C/C++ 代码, 并且被编译成 Python 的扩展模块。 这样即有非常快的程序执行速度和与外部C程序库的紧密集成, 也可以保持 Python 语言所著称的高程序员生产力。

增加的特性

  • 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. __init__.py 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().