The most widely used Python to C compiler https://cython.org
  • Cython 51.6%
  • Python 41.6%
  • C 6.5%
  • C++ 0.2%
Find a file
Pierre-Yves Le Borgne 774fc896b2
Release critical section and lock arguments on every exit, after unlocking (#8157)
Fixes #8156

`with cython.critical_section(...)` and `with lock:` disposed of their
arguments after their try/finally:

- `break` and `continue` leave the block through the finally clause,
which jumps straight to the loop's label and skips that code, so they
leaked the references held for the arguments.
- `return` released them too early.

Both statements now keep these objects in temps allocated with
`manage_ref=False`, as `WithStatNode` does for `exit_var`, so that
`return` leaves them alone, and their finally clause releases them after
ending the critical section or releasing the lock.
2026-12-01 22:10:39 +00:00
.github [3.3.x] Run OpenMP tests on clang on non-OSX (#8130) (#8164) 2026-11-30 08:01:12 +00:00
bin Remove some remaining references to old Python versions (#7367) 2025-11-28 15:17:43 +00:00
Cython Release critical section and lock arguments on every exit, after unlocking (#8157) 2026-12-01 22:10:39 +00:00
Demos Benchmarks: For Cython compilation timing, try to pre-import Cython's module in the expected order. 2026-08-27 18:34:44 +02:00
Doc/s5 Shrink some PNG images. 2019-02-02 17:53:02 +01:00
docs Remove docs suggestion with gil inside a PyMutex is unsafe (#8154) 2026-11-29 13:36:05 +00:00
pyximport pyximport: Search also pythonpath when searching for .pyx/.py file (#6315) 2024-08-10 15:35:19 +02:00
tests Release critical section and lock arguments on every exit, after unlocking (#8157) 2026-12-01 22:10:39 +00:00
Tools Update the default GCC version to GCC 11 (GH-8119) 2026-11-29 14:46:45 +00:00
.codespellrc Store module strings as ompressed concatenation (GH-6971) 2025-06-20 09:14:24 +02:00
.editorconfig Include header file types in editor config. 2018-09-17 22:28:34 +02:00
.gitattributes Use .gitattributes to avoid Windows-style line endings (#7400) 2025-12-13 14:53:13 +00:00
.gitignore Fix graalpy CI (#7445) 2026-01-15 08:00:05 +00:00
.hgignore Rename BUILD to TEST_TMP for bazel support. 2016-09-16 13:56:30 -07:00
.hgtags Added tag 0.21.2 for changeset ab1a3afb8775 2014-12-27 17:13:02 +01:00
.mailmap Add initial .mailmap file. 2015-09-16 23:06:10 -07:00
.readthedocs.yaml [3.2.x] Update OS version for 3.2.x docs (#7814) 2026-07-12 09:31:18 +01:00
BUILD.bazel fix implicit __init__.py creation warnings (#8132) 2026-11-22 07:26:58 +01:00
CHANGES.rst Update changelog. 2026-11-25 06:37:26 +01:00
CONTRIBUTING.md Add github contributing file. 2024-09-16 08:13:56 +02:00
COPYING.txt Clarify license position with respect to compilation output. 2014-07-22 21:50:57 -07:00
cygdb.py change shebang python -> python3 (#7053) 2025-07-31 07:43:11 +01:00
cython.py change shebang python -> python3 (#7053) 2025-07-31 07:43:11 +01:00
cythonize.py change shebang python -> python3 (#7053) 2025-07-31 07:43:11 +01:00
doc-requirements.txt Change documentation theme to Clarity (#7564) 2026-03-23 22:05:36 +01:00
INSTALL.txt Avoid suggesting "setup.py install" as installation command. 2023-07-29 12:23:06 +02:00
LICENSE.txt Update URLs to https:// in docs and comments 2019-01-11 13:26:03 -08:00
Makefile build: Do not generate a universal py2+py3 wheel but only one for Py3. 2023-11-26 15:02:41 +01:00
MANIFEST.in Make sure we include Shadow.pyi in the sdist. 2023-07-23 14:19:22 +02:00
pylintrc Avoid the terms whitelist and blacklist. 2020-12-18 12:31:44 -08:00
pyproject.toml Pin build requirements versions through PIP_BUILD_CONSTRAINT (GH-7665) 2026-06-23 21:45:36 +00:00
README.rst Update the README, moving Pyrex out of the initial presentation. 2025-07-25 08:22:43 +02:00
runcodestyle.py Add a separate script that just runs code style tests (#6092) 2024-10-19 11:04:35 +01:00
runtests.py [3.3.x] Run OpenMP tests on clang on non-OSX (#8130) (#8164) 2026-11-30 08:01:12 +00:00
setup.cfg Tests: Exclude the shared utility module from coverage analysis. 2026-07-30 21:29:39 +02:00
setup.py Build: Mark Py3.15 as officially supported in package metadata. 2026-11-30 09:45:00 +01:00
test-requirements-313.txt Get module-state working on multi-phase init (#6463) 2024-11-23 09:20:51 +00:00
test-requirements-cpython.txt Set up arm+windows test on CI (#7094) 2025-08-18 17:48:23 +01:00
test-requirements.txt Build: Use latest abi3audit 0.0.25 which supports Py3.9+. 2025-12-11 08:34:11 +01:00
ToDo.txt Remove trailing whitespace. 2010-12-13 11:06:26 -08:00
tox.ini Remove Py2/Py3.[3456] legacy support (GH-5801) 2023-11-08 14:36:50 +01:00
USAGE.txt Remove trailing whitespace. 2010-12-13 11:06:26 -08:00

Welcome to Cython!
==================

Cython is an optimising Python compiler that makes writing C extensions for
Python as easy as Python itself.

Cython translates Python code to C/C++ code, but additionally supports calling
C functions and declaring C types on variables and class attributes.
This allows broad to fine-grained manual tuning that lets the compiler
generate very efficient C code from Cython code.

This makes Cython the ideal language for wrapping external C libraries, and
for fast C modules that speed up the execution of Python code.

* Official website: https://cython.org/
* Documentation: https://docs.cython.org/
* Github repository: https://github.com/cython/cython
* Wiki: https://github.com/cython/cython/wiki

Cython has `more than 70 million downloads <https://pypistats.org/packages/cython>`_
per month on PyPI.  You can **support the Cython project** via
`Github Sponsors <https://github.com/users/scoder/sponsorship>`_ or
`Tidelift <https://tidelift.com/subscription/pkg/pypi-cython>`_.


Installation:
-------------

If you already have a C compiler, just run following command::

   pip install Cython

otherwise, see `the installation page <https://docs.cython.org/en/latest/src/quickstart/install.html>`_.


License:
--------

The original Pyrex program, which Cython is based on, was licensed "free of restrictions" (see below).
Cython itself is licensed under the permissive **Apache License**.

See `LICENSE.txt <https://github.com/cython/cython/blob/master/LICENSE.txt>`_.


Contributing:
-------------

Want to contribute to the Cython project?
Here is some `help to get you started <https://github.com/cython/cython/blob/master/docs/CONTRIBUTING.rst>`_.


Differences to other Python compilers
-------------------------------------

Started as a project in the early 2000s, Cython has outlived
`most other attempts <https://wiki.python.org/moin/PythonImplementations#Compilers>`_
at producing static compilers for the Python language.

Similar projects that have a relevance today include:

* `PyPy <https://www.pypy.org/>`_, a Python implementation with a JIT compiler.

  * Pros: JIT compilation with runtime optimisations, fully language compliant,
    good integration with external C/C++ code
  * Cons: non-CPython runtime, relatively large resource usage of the runtime,
    limited compatibility with CPython extensions, non-obvious performance results

* `Numba <http://numba.pydata.org/>`_, a Python extension that features a
  JIT compiler for a subset of the language, based on the LLVM compiler
  infrastructure (probably best known for its ``clang`` C compiler).
  It mostly targets numerical code that uses NumPy.

  * Pros: JIT compilation with runtime optimisations
  * Cons: limited language support, relatively large runtime dependency (LLVM),
    non-obvious performance results

* `Pythran <https://pythran.readthedocs.io/>`_, a static Python-to-C++
  extension compiler for a subset of the language, mostly targeted
  at numerical computation.  Pythran can be (and is probably best) used
  as an additional
  `backend for NumPy code <https://cython.readthedocs.io/en/latest/src/userguide/numpy_pythran.html>`_
  in Cython.

* `mypyc <https://mypyc.readthedocs.io/>`_, a static Python-to-C extension
  compiler, based on the `mypy <http://www.mypy-lang.org/>`_ static Python
  analyser.  Like Cython's
  `pure Python mode <https://cython.readthedocs.io/en/latest/src/tutorial/pure.html>`_,
  mypyc can make use of PEP-484 type annotations to optimise code for static types.

  * Pros: good support for language and PEP-484 typing, good type inference,
    reasonable performance gains
  * Cons: no support for low-level optimisations and typing,
    opinionated Python type interpretation, reduced Python compatibility
    and introspection after compilation

* `Nuitka <https://nuitka.net/>`_, a static Python-to-C extension compiler.

  * Pros: highly language compliant, reasonable performance gains,
    support for static application linking (similar to
    `cython_freeze <https://github.com/cython/cython/blob/master/bin/cython_freeze>`_
    but with the ability to bundle library dependencies into a self-contained
    executable)
  * Cons: no support for low-level optimisations and typing

In comparison to the above, Cython provides

* fast, efficient and highly compliant support for almost all
  Python language features, including dynamic features and introspection
* full runtime compatibility with all still-in-use and future versions
  of CPython
* "generate once, compile everywhere" C code generation that allows for
  reproducible performance results and testing
* C compile time adaptation to the target platform and Python version
* support for other C-API implementations, including PyPy and Pyston
* seamless integration with C/C++ code
* broad support for manual optimisation and tuning down to the C level
* a large user base with thousands of libraries, packages and tools
* more than two decades of bug fixing and static code optimisations


The following is from Pyrex:
------------------------------------------------------
Cython was originally based on `Pyrex <https://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_
by Greg Ewing, with the following written in the Pyrex readme document:

This is a development version of Pyrex, a language
for writing Python extension modules.

For more info, take a look at:

* Doc/About.html for a description of the language
* INSTALL.txt    for installation instructions
* USAGE.txt      for usage instructions
* Demos          for usage examples

Comments, suggestions, bug reports, etc. are most
welcome!

Copyright stuff: Pyrex is free of restrictions. You
may use, redistribute, modify and distribute modified
versions.

The latest version of Pyrex can be found `here <https://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_.

| Greg Ewing, Computer Science Dept
| University of Canterbury
| Christchurch, New Zealand

 A citizen of NewZealandCorp, a wholly-owned subsidiary of USA Inc.