What does language_level in setup.py for cython do

2019-07-14 06:28发布

If I set language_level=2 in ext_modules = cythonize(extensions, language_level=2), what does that change? Is it just that the code I have written should be interpreted as Python2?

Is the end result exactly the same?

标签: cython
1条回答
我想做一个坏孩纸
2楼-- · 2019-07-14 07:28

Building a cython extension is a two-step proccess:

  1. creating the foo.c-file from foo.pyx file using PythonX+cython-module. X could be here 2.7, 3.7 or whatever version you prefer.
  2. creating the corresponding so-file (or pyd on Windows) with help of compiler and includes of PythonY and corresponding shared library. Here Y doesn't have to be X, but in most cases Y and X are the same.

The resulting extension can be used with PythonY (it doesn't play a role what X was).

However, there is still the question: In which Python-version was the original pyx-file written? If language_level is not set, current Cython-versions assume that the pyx-file was written in the version 2 (btw. this is not the case for IPython-%%cython-magic, where the version with which the file foo.c is cythonized).

This behavior will change in the future, this is the reason you see the somewhat irritating warning, if you build with cython>=0.29:

/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: XXXXXX.pyx
tree = Parsing.p_module(s, pxd, full_module_name)

So you can explicitly set the language_level, so that your extension has the same behavior independent of the Python-version with which it was cythonized.

For some examples of different behavior see, the follwoing example.

Using language_level=3:

%%cython -3
print("I'm", "not a tuple")
print(5/4) 

results in

I'm not a tuple
1.25  

but using language_level=2:

%%cython -2
print("I'm", "not a tuple")
print(5/4) 

results in

("I'm", 'not a tuple')   # yet a tuple!
1                        # integer division in Python2!

Obviously the above are only two examples, there are much more differences (e.g. str & unicode stuff).

查看更多
登录 后发表回答