I have been trying to understand how cython really works. Very first, I have written test.pyx file containing,
import numpy as np
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("test.pyx"))
python setup.py build_ext --inplace
+06: import numpy as np
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 6, __pyx_L1_error)
if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) __PYX_ERR(0, 6, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
You can run
cython -a test.pyx, and it will open your default web browser with a generated HTML that has each Cython code line annotated with C code that was generated for it.
The primary purpose of this is help you optimize the code by eliminating unnecessary Python calls, so it highlights lines that involve much interaction with Python runtime.
As a Cython newbie myself, I cannot overstate its helpfulness.
Note that as per the "primary purpose" above, it doesn't include boilerplate code outside the part that directly corresponds to source lines. Use the link to the raw C on the generated page to see that.
"numpy"string. As you can see by searching it, Cython creates these upon initialization for all string constants that need to be passed to Python runtime.
__Pyx_Import()is also an autogenerated function that wraps the builtin
Fortunately (=thanks to Cython's author), the autogenerated names are very descriptive, so you rarely need to actually look them up: in most cases, you can guess what the entity is/does. And all Cython-autogenerated entities are helpfully prefixed to distinguish them from e.g. ones from Python C API.