Cython 0.20 發布,兼有C的效率和Python的優美的編程語言

jopen 10年前發布 | 23K 次閱讀 Cython

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.</li>

  • 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.</li>

  • 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.</li> </ul>

    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().

     

     本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!