Cython 源文件名由模块名称组成,之后紧跟
.pyx
扩展名,例如,称为 primes 的模块拥有的源文件名为
primes.pyx
.
Cython 代码,不像 Python,必须被编译。这分 2 阶段发生:
.pyx
文件通过 Cython 编译成
.c
文件。
.c
文件由 C 编译器编译成
.so
文件 (或
.pyd
文件在 Windows)
一旦有编写
.pyx
文件,有 2 种方式将其转换成扩展模块。
以下小节描述构建扩展模块的几种方式,及如何将指令传递给 Cython 编译器。
有 2 种命令行编译方式。
cython
命令接受
.py
or
.pyx
文件并将其编译成 C/C++ 文件。
cythonize
命令接受
.py
or
.pyx
文件并将其编译成 C/C++ 文件。然后将 C/C++ 文件编译成可直接从 Python 导入的扩展模块。
cython
命令
¶
One way is to compile it manually with the Cython compiler, e.g.:
$ cython primes.pyx
This will produce a file called
primes.c
, which then needs to be compiled with the C compiler using whatever options are appropriate on your platform for generating an extension module. For these options look at the official Python documentation.
The other, and probably better, way is to use the
distutils
extension provided with Cython. The benefit of this method is that it will give the platform specific compilation options, acting like a stripped down autotools.
cythonize
命令
¶
运行
cythonize
compiler command with your options and list of
.pyx
files to generate an extension module. For example:
$ cythonize -a -i yourmod.pyx
This creates a
yourmod.c
文件 (或
yourmod.cpp
in C++ mode), compiles it, and puts the resulting extension module (
.so
or
.pyd
, depending on your platform) next to the source file for direct import (
-i
builds “in place”). The
-a
switch additionally produces an annotated html file of the source code.
cythonize
command accepts multiple source files and glob patterns like
**/*.pyx
as argument and also understands the common
-j
option for running multiple parallel build jobs. When called without further options, it will only translate the source files to
.c
or
.cpp
files. Pass the
-h
flag for a complete list of supported options.
There simpler command line tool
cython
only invokes the source code translator.
In the case of manual compilation, how to compile your
.c
files will vary depending on your operating system and compiler. The Python documentation for writing extension modules should have some details for your system. On a Linux system, for example, it might look similar to this:
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \ -I/usr/include/python3.5 -o yourmod.so yourmod.c
(
gcc
will need to have paths to your included header files and paths to libraries you want to link with.)
After compilation, a
yourmod.so
(
yourmod.pyd
for Windows) file is written into the target directory and your module,
yourmod
, is available for you to import as with any other Python module. Note that if you are not relying on
cythonize
or distutils, you will not automatically benefit from the platform specific file extension that CPython generates for disambiguation, such as
yourmod.cpython-35m-x86_64-linux-gnu.so
on a regular 64bit Linux installation of CPython 3.5.
由 Cython 提供的 distutils 扩展允许传递
.pyx
文件直接到
Extension
构造函数在 setup 文件中。
If you have a single Cython file that you want to turn into a compiled extension, say with filename
example.pyx
the associated
setup.py
would be:
from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("example.pyx") )
To understand the
setup.py
more fully look at the official
distutils
documentation. To compile the extension for use in the current directory use:
$ python setup.py build_ext --inplace
If you have include files in non-standard places you can pass an
include_path
参数用于
cythonize
:
from distutils.core import setup from Cython.Build import cythonize setup( name="My hello app", ext_modules=cythonize("src/*.pyx", include_path=[...]), )
Often, Python packages that offer a C-level API provide a way to find the necessary include files, e.g. for NumPy:
include_path = [numpy.get_include()]
注意
Using memoryviews or importing NumPy with
import
numpy
does not mean that you have to add the path to NumPy include files. You need to add this path only if you use
cimport
numpy
.
Despite this, you will still get warnings like the following from the compiler, because Cython is using a deprecated Numpy API:
.../include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
For the time being, it is just a warning that you can ignore.
If you need to specify compiler options, libraries to link with or other linker options you will need to create
Extension
instances manually (note that glob syntax can still be used to specify multiple extensions in one line):
from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension("primes", ["primes.pyx"], include_dirs=[...], libraries=[...], library_dirs=[...]), # Everything but primes.pyx is included here. Extension("*", ["*.pyx"], include_dirs=[...], libraries=[...], library_dirs=[...]), ] setup( name="My hello app", ext_modules=cythonize(extensions), )
Note that when using setuptools, you should import it before Cython as setuptools may replace the
Extension
class in distutils. Otherwise, both might disagree about the class to use here.
Note also that if you use setuptools instead of distutils, the default action when running
python
setup.py
安装
是创建压缩
egg
文件不会工作采用
cimport
for
pxd
文件当试着从依赖包使用它们时。为阻止这种情况,包括
zip_safe=False
在自变量到
setup()
.
If your options are static (for example you do not need to call a tool like
pkg-config
to determine them) you can also provide them directly in your .pyx or .pxd source file using a special comment block at the start of the file:
# distutils: libraries = spam eggs # distutils: include_dirs = /opt/food/include
If you cimport multiple .pxd files defining libraries, then Cython merges the list of libraries, so this works as expected (similarly with other options, like
include_dirs
above).
If you have some C files that have been wrapped with Cython and you want to compile them into your extension, you can define the distutils
sources
parameter:
# distutils: sources = helper.c, another_helper.c
Note that these sources are added to the list of sources of the current extension module. Spelling this out in the
setup.py
file looks as follows:
from distutils.core import setup from Cython.Build import cythonize from distutils.extension import Extension sourcefiles = ['example.pyx', 'helper.c', 'another_helper.c'] extensions = [Extension("example", sourcefiles)] setup( ext_modules=cythonize(extensions) )
Extension
class takes many options, and a fuller explanation can be found in the
distutils documentation
. Some useful options to know about are
include_dirs
,
libraries
,和
library_dirs
which specify where to find the
.h
and library files when linking to external libraries.
Sometimes this is not enough and you need finer customization of the distutils
Extension
. To do this, you can provide a custom function
create_extension
to create the final
Extension
object after Cython has processed the sources, dependencies and
#
distutils
directives but before the file is actually Cythonized. This function takes 2 arguments
template
and
kwds
,其中
template
是
Extension
object given as input to Cython and
kwds
是
dict
with all keywords which should be used to create the
Extension
. The function
create_extension
must return a 2-tuple
(extension,
metadata)
,其中
extension
is the created
Extension
and
metadata
is metadata which will be written as JSON at the top of the generated C files. This metadata is only used for debugging purposes, so you can put whatever you want in there (as long as it can be converted to JSON). The default function (defined in
Cython.Build.Dependencies
) is:
def default_create_extension(template, kwds): if 'depends' in kwds: include_dirs = kwds.get('include_dirs', []) + ["."] depends = resolve_depends(kwds['depends'], include_dirs) kwds['depends'] = sorted(set(depends + template.depends)) t = template.__class__ ext = t(**kwds) metadata = dict(distutils=kwds, module_name=kwds['name']) return ext, metadata
In case that you pass a string instead of an
Extension
to
cythonize()
,
template
will be an
Extension
without sources. For example, if you do
cythonize("*.pyx")
,
template
将是
Extension(name="*.pyx",
sources=[])
.
Just as an example, this adds
mylib
as library to every extension:
from Cython.Build.Dependencies import default_create_extension def my_create_extension(template, kwds): libs = kwds.get('libraries', []) + ["mylib"] kwds['libraries'] = libs return default_create_extension(template, kwds) ext_modules = cythonize(..., create_extension=my_create_extension)
注意
If you Cythonize in parallel (using the
nthreads
argument), then the argument to
create_extension
must be pickleable. In particular, it cannot be a lambda function.
函数
cythonize()
can take extra arguments which will allow you to customize your build.
Cython.Build.
cythonize
(
module_list
,
exclude=None
,
nthreads=0
,
aliases=None
,
quiet=False
,
force=False
,
language=None
,
exclude_failures=False
,
**options
)
¶
Compile a set of source modules into C/C++ files and return a list of distutils Extension objects for them.
| 参数: |
|
|---|
To automatically compile multiple Cython files without listing all of them explicitly, you can use glob patterns:
setup( ext_modules = cythonize("package/*.pyx") )
You can also use glob patterns in
Extension
objects if you pass them through
cythonize()
:
extensions = [Extension("*", ["*.pyx"])] setup( ext_modules = cythonize(extensions) )
It is strongly recommended that you distribute the generated
.c
files as well as your Cython sources, so that users can install your module without needing to have Cython available.
It is also recommended that Cython compilation not be enabled by default in the version you distribute. Even if the user has Cython installed, he/she probably doesn’t want to use it just to install your module. Also, the installed version may not be the same one you used, and may not compile your sources correctly.
This simply means that the
setup.py
file that you ship with will just be a normal distutils file on the generated
.c
files, for the basic example we would have instead:
from distutils.core import setup from distutils.extension import Extension setup( ext_modules = [Extension("example", ["example.c"])] )
This is easy to combine with
cythonize()
by changing the file extension of the extension module sources:
from distutils.core import setup from distutils.extension import Extension USE_CYTHON = ... # command line option, try-import, ... ext = '.pyx' if USE_CYTHON else '.c' extensions = [Extension("example", ["example"+ext])] if USE_CYTHON: from Cython.Build import cythonize extensions = cythonize(extensions) setup( ext_modules = extensions )
If you have many extensions and want to avoid the additional complexity in the declarations, you can declare them with their normal Cython sources and then call the following function instead of
cythonize()
to adapt the sources list in the Extensions when not using Cython:
import os.path def no_cythonize(extensions, **_ignore): for extension in extensions: sources = [] for sfile in extension.sources: path, ext = os.path.splitext(sfile) if ext in ('.pyx', '.py'): if extension.language == 'c++': ext = '.cpp' else: ext = '.c' sfile = path + ext sources.append(sfile) extension.sources[:] = sources return extensions
Another option is to make Cython a setup dependency of your system and use Cython’s build_ext module which runs
cythonize
as part of the build process:
setup( setup_requires=[ 'cython>=0.x', ], extensions = [Extension("*", ["*.pyx"])], cmdclass={'build_ext': Cython.Build.build_ext}, ... )
If you want to expose the C-level interface of your library for other libraries to cimport from, use package_data to install the
.pxd
files, e.g.:
setup( package_data = { 'my_package': ['*.pxd'], 'my_package/sub_package': ['*.pxd'], }, ... )
这些
.pxd
files need not have corresponding
.pyx
modules if they contain purely declarations of external libraries.
Remember that if you use setuptools instead of distutils, the default action when running
python
setup.py
安装
是创建压缩
egg
文件不会工作采用
cimport
for
pxd
文件当试着从依赖包使用它们时。为阻止这种情况,包括
zip_safe=False
在自变量到
setup()
.
In some scenarios, it can be useful to link multiple Cython modules (or other extension modules) into a single binary, e.g. when embedding Python in another application. This can be done through the inittab import mechanism of CPython.
Create a new C file to integrate the extension modules and add this macro to it:
#if PY_MAJOR_VERSION < 3 # define MODINIT(name) init ## name #else # define MODINIT(name) PyInit_ ## name #endif
If you are only targeting Python 3.x, just use
PyInit_
as prefix.
Then, for each of the modules, declare its module init function as follows, replacing
some_module_name
with the name of the module:
PyMODINIT_FUNC MODINIT(some_module_name) (void);
In C++, declare them as
extern
C
.
If you are not sure of the name of the module init function, refer to your generated module source file and look for a function name starting with
PyInit_
.
Next, before you start the Python runtime from your application code with
Py_Initialize()
, you need to initialise the modules at runtime using the
PyImport_AppendInittab()
C-API function, again inserting the name of each of the modules:
PyImport_AppendInittab("some_module_name", MODINIT(some_module_name));
This enables normal imports for the embedded extension modules.
In order to prevent the joined binary from exporting all of the module init functions as public symbols, Cython 0.28 and later can hide these symbols if the macro
CYTHON_NO_PYINIT_EXPORT
is defined while C-compiling the module C files.
Also take a look at the cython_freeze tool. It can generate the necessary boilerplate code for linking one or more modules into a single Python executable.
pyximport
¶
For building Cython modules during development without explicitly running
setup.py
after each change, you can use
pyximport
:
>>> import pyximport; pyximport.install() >>> import helloworld Hello World
This allows you to automatically run Cython on every
.pyx
that Python is trying to import. You should use this for simple Cython builds only where no extra C libraries and no special building setup is needed.
It is also possible to compile new
.py
modules that are being imported (including the standard library and installed packages). For using this feature, just tell that to
pyximport
:
>>> pyximport.install(pyimport=True)
In the case that Cython fails to compile a Python module,
pyximport
will fall back to loading the source modules instead.
注意:不推荐让
pyximport
在最终用户侧构建代码,因为它会挂钩到他们的导入系统。迎合最终用户的最佳方式是提供预构建的二进制包以
wheel
打包格式。
函数
pyximport.install()
can take several arguments to influence the compilation of Cython or Python files.
pyximport.
安装
(
pyximport=True
,
pyimport=False
,
build_dir=None
,
build_in_temp=True
,
setup_args=None
,
reload_support=False
,
load_py_module_on_import_failure=False
,
inplace=False
,
language_level=None
)
¶
Main entry point for pyxinstall.
Call this to install the
.pyx
import hook in your meta-path for a single Python process. If you want it to be installed whenever you use Python, add it to your
sitecustomize
(as described above).
| 参数: |
|
|---|
由于
pyximport
does not use
cythonize()
internally, it currently requires a different setup for dependencies. It is possible to declare that your module depends on multiple files, (likely
.h
and
.pxd
files). If your Cython module is named
foo
and thus has the filename
foo.pyx
then you should create another file in the same directory called
foo.pyxdep
。
modname.pyxdep
file can be a list of filenames or “globs” (like
*.pxd
or
include/*.h
). Each filename or glob must be on a separate line. Pyximport will check the file date for each of those files before deciding whether to rebuild the module. In order to keep track of the fact that the dependency has been handled, Pyximport updates the modification time of your “.pyx” source file. Future versions may do something more sophisticated like informing distutils of the dependencies directly.
pyximport
does not use
cythonize()
. Thus it is not possible to do things like using compiler directives at the top of Cython files or compiling Cython code to C++.
Pyximport does not give you any control over how your Cython file is compiled. Usually the defaults are fine. You might run into problems if you wanted to write your program in half-C, half-Cython and build them into a single library.
Pyximport does not hide the Distutils/GCC warnings and errors generated by the import process. Arguably this will give you better feedback if something went wrong and why. And if nothing went wrong it will give you the warm fuzzy feeling that pyximport really did rebuild your module as it was supposed to.
Basic module reloading support is available with the option
reload_support=True
. Note that this will generate a new module filename for each build and thus end up loading multiple shared libraries into memory over time. CPython has limited support for reloading shared libraries as such, see
PEP 489
.
Pyximport puts both your
.c
file and the platform-specific binary into a separate build directory, usually
$HOME/.pyxblx/
. To copy it back into the package hierarchy (usually next to the source file) for manual reuse, you can pass the option
inplace=True
.
cython.inline
¶
One can also compile Cython in a fashion similar to SciPy’s
weave.inline
。例如:
>>> import cython >>> def f(a): ... ret = cython.inline("return a+b", b=3) ...
Unbound variables are automatically pulled from the surrounding local and global scopes, and the result of the compilation is cached for efficient re-use.
The Sage notebook allows transparently editing and compiling Cython code simply by typing
%cython
at the top of a cell and evaluate it. Variables and functions defined in a Cython cell are imported into the running session. Please check
Sage documentation
了解细节。
You can tailor the behavior of the Cython compiler by specifying the directives below.
It’s possible to compile code in a notebook cell with Cython. For this you need to load the Cython magic:
%load_ext cython
Then you can define a Cython cell by writing
%%cython
on top of it. Like this:
%%cython cdef int a = 0 for i in range(10): a += i print(a)
Note that each cell will be compiled into a separate extension module. So if you use a package in a Cython cell, you will have to import this package in the same cell. It’s not enough to have imported the package in a previous cell. Cython will tell you that there are “undefined global names” at compilation time if you don’t comply.
The global names (top level functions, classes, variables and modules) of the cell are then loaded into the global namespace of the notebook. So in the end, it behaves as if you executed a Python cell.
Additional allowable arguments to the Cython magic are listed below. You can see them also by typing
`%%cython?
in IPython or a Jupyter notebook.
| -a, –annotate | Produce a colorized HTML version of the source. |
| -+, –cplus | Output a C++ rather than C file. |
| -f, –force | Force the compilation of a new module, even if the source has been previously compiled. |
| -3 | Select Python 3 syntax |
| -2 | Select Python 2 syntax |
| -c=COMPILE_ARGS, –compile-args=COMPILE_ARGS | Extra flags to pass to compiler via the extra_compile_args. |
| –link-args LINK_ARGS | Extra flags to pass to linker via the extra_link_args. |
| -l LIB, –lib LIB | Add a library to link the extension against (can be specified multiple times). |
| -L dir | Add a path to the list of library directories (can be specified multiple times). |
| -I INCLUDE, –include INCLUDE | Add a path to the list of include directories (can be specified multiple times). |
| -S, –src | Add a path to the list of src files (can be specified multiple times). |
| -n NAME, –name NAME | Specify a name for the Cython module. |
| –pgo | Enable profile guided optimisation in the C compiler. Compiles the cell twice and executes it in between to generate a runtime profile. |
| –verbose | Print debug information like generated .c/.cpp file location and exact gcc/g++ command invoked. |
Compiler options can be set in the
setup.py
, before calling
cythonize()
,像这样:
from distutils.core import setup from Cython.Build import cythonize from Cython.Compiler import Options Options.docstrings = False setup( name = "hello", ext_modules = cythonize("lib.pyx"), )
Here are the options that are available:
Cython.Compiler.Options.
docstrings
= True
¶
Whether or not to include docstring in the Python extension. If False, the binary size will be smaller, but the
__doc__
attribute of any class or function will be an empty string.
Cython.Compiler.Options.
embed_pos_in_docstring
= False
¶
Embed the source code position in the docstrings of functions and classes.
Cython.Compiler.Options.
emit_code_comments
= True
¶
Copy the original source code line by line into C code comments in the generated code file to help with understanding the output. This is also required for coverage analysis.
Cython.Compiler.Options.
generate_cleanup_code
= False
¶
Decref global variables in each module on exit for garbage collection. 0: None, 1+: interned objects, 2+: cdef globals, 3+: types objects Mostly for reducing noise in Valgrind, only executes at process exit (when all memory will be reclaimed anyways).
Cython.Compiler.Options.
clear_to_none
= True
¶
Should tp_clear() set object fields to None instead of clearing them to NULL?
Cython.Compiler.Options.
annotate
= False
¶
Generate an annotated HTML version of the input source files for debugging and optimisation purposes. This has the same effect as the
annotate
argument in
cythonize()
.
Cython.Compiler.Options.
fast_fail
= False
¶
This will abort the compilation on the first error occurred rather than trying to keep going and printing further error messages.
Cython.Compiler.Options.
warning_errors
= False
¶
Turn all warnings into errors.
Cython.Compiler.Options.
error_on_unknown_names
= True
¶
Make unknown names an error. Python raises a NameError when encountering unknown names at runtime, whereas this option makes them a compile time error. If you want full Python compatibility, you should disable this option and also ‘cache_builtins’.
Cython.Compiler.Options.
error_on_uninitialized
= True
¶
Make uninitialized local variable reference a compile time error. Python raises UnboundLocalError at runtime, whereas this option makes them a compile time error. Note that this option affects only variables of “python object” type.
Cython.Compiler.Options.
convert_range
= True
¶
This will convert statements of the form
for
i
in
range(...)
to
for
i
from
...
当
i
is a C integer type, and the direction (i.e. sign of step) can be determined. WARNING: This may change the semantics if the range causes assignment to i to overflow. Specifically, if this option is set, an error will be raised before the loop is entered, whereas without this option the loop will execute until an overflowing value is encountered.
Cython.Compiler.Options.
cache_builtins
= True
¶
Perform lookups on builtin names only once, at module initialisation time. This will prevent the module from getting imported if a builtin name that it uses cannot be found during initialisation. Default is True. Note that some legacy builtins are automatically remapped from their Python 2 names to their Python 3 names by Cython when building in Python 3.x, so that they do not get in the way even if this option is enabled.
Cython.Compiler.Options.
gcc_branch_hints
= True
¶
Generate branch prediction hints to speed up error handling etc.
Cython.Compiler.Options.
lookup_module_cpdef
= False
¶
Enable this to allow one to write
your_module.foo
=
...
to overwrite the definition if the cpdef function foo, at the cost of an extra dictionary lookup on every call. If this is false it generates only the Python wrapper and no override check.
Cython.Compiler.Options.
embed
= None
¶
Whether or not to embed the Python interpreter, for use in making a standalone executable or calling from external libraries. This will provide a C function which initialises the interpreter and executes the body of this module. See this demo for a concrete example. If true, the initialisation function is the C main() function, but this option can also be set to a non-empty string to provide a function name explicitly. Default is False.
Cython.Compiler.Options.
cimport_from_pyx
= False
¶
Allows cimporting from a pyx file without a pxd file.
Cython.Compiler.Options.
buffer_max_dims
= 8
¶
Maximum number of dimensions for buffers – set lower than number of dimensions in numpy, as slices are passed by value and involve a lot of copying.
Cython.Compiler.Options.
closure_freelist_size
= 8
¶
Number of function closure instances to keep in a freelist (0: no freelists)
Compiler directives are instructions which affect the behavior of Cython code. Here is the list of currently supported directives:
binding
(True / False)
len()
) or, when set to True, more like Python’s functions.
When enabled, functions will bind to an instance when looked up as a
class attribute (hence the name) and will emulate the attributes
of Python functions, including introspections like argument names and
annotations.
Default is False.
boundscheck
(True / False)
wraparound
is False).
Conditions which would normally trigger an IndexError may instead cause
segfaults or data corruption if this is set to False.
Default is True.
wraparound
(True / False)
boundschecks
above),
negative indexing will usually raise an
IndexError
for indices that
Cython evaluates itself.
However, these cases can be difficult to recognise in user code to
distinguish them from indexing or slicing that is evaluated by the
underlying Python array or sequence object and thus continues to support
wrap-around indices.
It is therefore safest to apply this option only to code that does not
process negative indices at all.
Default is True.
initializedcheck
(True / False)
nonecheck
(True / False)
None
. Otherwise a check is inserted and the
appropriate exception is raised. This is off by default for
performance reasons. Default is False.
overflowcheck
(True / False)
overflowcheck.fold
(True / False)
Demos/overflow_perf.pyx
. Default is True.
embedsignature
(True / False)
cdivision
(True / False)
ZeroDivisionError
when the right operand is 0. This has up to
a 35% speed penalty. If set to True, no checks are performed. See
CEP 516
. Default
为 False。
cdivision_warnings
(True / False)
always_allow_keywords
(True / False)
METH_NOARGS
and
METH_O
when constructing
functions/methods which take zero or one arguments. Has no effect
on special methods and functions with more than one argument. The
METH_NOARGS
and
METH_O
signatures provide faster
calling conventions but disallow the use of keywords.
profile
(True / False)
linetrace
(True / False)
CYTHON_TRACE=1
to the C compiler (e.g. using the distutils option
define_macros
). Define
CYTHON_TRACE_NOGIL=1
to also include
nogil
functions and sections.
infer_types
(True / False)
language_level
(2/3/3str)
3str
option enables Python 3 semantics but does
not change the
str
type and unprefixed string literals to
unicode
when the compiled code runs in Python 2.x.
Note that cimported files inherit this setting from the module
being compiled, unless they explicitly set their own language level.
Included source files always inherit this setting.
c_string_type
(bytes / str / unicode)
Globally set the type of an implicit coercion from char* or std::string.
c_string_encoding
(ascii, default, utf-8, etc.)
ascii
or
default
, the latter being utf-8 in Python 3 and
nearly-always ascii in Python 2.
type_version_tag
(True / False)
Py_TPFLAGS_HAVE_VERSION_TAG
. Default is True, meaning that
the cache is enabled for Cython implemented types. To disable it
explicitly in the rare cases where a type needs to juggle with its
tp_dict
internally without paying attention to cache consistency, this option can
be set to False.
unraisable_tracebacks
(True / False)
Whether to print tracebacks when suppressing unraisable exceptions.
iterable_coroutine
(True / False)
optimize.use_switch
(True / False)
if
x
==
1
or
x
==
2:
) into C switch statements. This can have performance
benefits if there are lots of values but cause compiler errors if there are any
duplicate values (which may not be detectable at Cython compile time for all
C constants). Default is True.
optimize.unpack_method_calls
(True / False)
All warning directives take True / False as options to turn the warning on / off.
warn.undeclared
(default False)
cdef
声明
warn.unreachable
(default True)
warn.maybe_uninitialized
(default False)
Warns about use of variables that are conditionally uninitialized.
warn.unused
(default False)
Warns about unused variables and declarations
warn.unused_arg
(default False)
Warns about unused function arguments
warn.unused_result
(default False)
r
=
2;
r
=
1
+
2
warn.multiple_declarators
(default True)
cdef
double*
a,
b
- which, as in C, declares
a
as a pointer,
b
as
a value type, but could be mininterpreted as declaring two pointers.
One can set compiler directives through a special header comment near the top of the file, like this:
# cython: language_level=3, boundscheck=False
The comment must appear before any code (but can appear after other comments or whitespace).
One can also pass a directive on the command line by using the -X switch:
$ cython -X boundscheck=True ...
Directives passed on the command line will override directives set in header comments.
For local blocks, you need to cimport the special builtin
cython
模块:
#!python cimport cython
Then you can use the directives either as decorators or in a with statement, like this:
#!python @cython.boundscheck(False) # turn off boundscheck for this function def f(): ... # turn it temporarily on again for this block with cython.boundscheck(True): ...
警告
These two methods of setting directives are not affected by overriding the directive on the command-line using the -X option.
setup.py
¶
Compiler directives can also be set in the
setup.py
file by passing a keyword argument to
cythonize
:
from distutils.core import setup from Cython.Build import cythonize setup( name="My hello app", ext_modules=cythonize('hello.pyx', compiler_directives={'embedsignature': True}), )
This will override the default directives as specified in the
compiler_directives
dictionary. Note that explicit per-file or local directives as explained above take precedence over the values passed to
cythonize
.