Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler directive language_level is ignored if no Python statement is written before it. #2230

Closed
rsaim opened this issue May 2, 2018 · 1 comment

Comments

@rsaim
Copy link

rsaim commented May 2, 2018

$ cat temp.pyx
# cython: language_level=3
print("abc", "def", sep=" ,") # invalid in python 2

Compiling:

$ cythonize -i world_dep.pyx
Error compiling Cython file:
------------------------------------------------------------
...
# cython: language_level=3

print("abc", "def", sep=" ,")                      ^
------------------------------------------------------------
temp.pyx:4:23: Expected ')', found '='

So lanugage_level directive is not getting respected. Thus, cythonize ends up using Python 2 semantics and the error is thrown as the print statement above is invalid in Python 2.

However, including any Python statement before the print statement makes this work:

 $ cat temp.pyx
 # cython: language_level=3
 import os
 print("abc", "def", sep=" ,")
Compiling and executing:

$ cythonize -i temp.pyx; python -c "import temp"
abc, def
@pdebuyl
Copy link

pdebuyl commented May 2, 2018

I confirm with cython 0.26.1 on Python 2.7 and 3.6 (debian, x86_64) and cython 0.28.2 (from pip).

As a test, if indeed the directive "skips" the first line, I checked the following:

# cython: language_level=3
print 1
print("abc", "def", sep=" ,")

And it compiles and runs properly. cythonize will also build the snippet above when given the -3 option but cython will not (with option -3).

@scoder scoder added this to the 0.29 milestone May 18, 2018
@scoder scoder closed this as completed in 385b37f May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants