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?
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?
Building a cython extension is a two-step proccess:
foo.c
-file from foo.pyx
file using PythonX+cython-module. X
could be here 2.7, 3.7 or whatever version you prefer.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).