Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython compiler directive language_level not respected

I am working with compiler directives for Cython (http://docs.cython.org/en/latest/src/reference/compilation.html#globally).

$ 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 language_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 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

Any idea how the import statement is making the language_level to be respected?

I have raised this same issue on the Cython GitHub repository as well?

like image 268
Saim Raza Avatar asked May 02 '18 08:05

Saim Raza


People also ask

How do I compile a PYX file?

To compile the example. pyx file, select Tools | Run setup.py Task command from the main menu. In the Enter setup.py task name type build and select the build_ext task. See Create and run setup.py for more details.

Does Cython require a compiler?

pyx extension, for example a module called primes would have a source file named primes. pyx . Cython code, unlike Python, must be compiled.

What C compiler does Cython use?

Windows The CPython project recommends building extension modules (including Cython modules) with the same compiler that Python was built with. This is usually a specific version of Microsoft Visual C/C++ (MSVC) - see https://wiki.python.org/moin/WindowsCompilers.


1 Answers

As commented this bug is fixed:

$ /mnt/c/Python36/Scripts/cython.exe --version
Cython version 0.29.8
$ /mnt/c/Python36/Scripts/cythonize.exe -a -i temp.pyx
Compiling C:\Users\name\Documents\code\benchmark\temp.pyx because it changed.
[1/1] Cythonizing C:\Users\name\Documents\code\benchmark\temp.pyx
running build_ext
building 'temp' extension
creating C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release
creating C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users
creating C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name
creating C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents
creating C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents\code
creating C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents\code\benchmark
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Ic:\users\name\appdata\local\programs\python\python36\inc
lude -Ic:\users\name\appdata\local\programs\python\python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows K
its\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)
\Windows Kits\8.1\include\winrt" /TcC:\Users\name\Documents\code\benchmark\temp.c /FoC:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents
\code\benchmark\temp.obj
temp.c
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:c:\users\e1220
41\appdata\local\programs\python\python36\libs /LIBPATH:c:\users\name\appdata\local\programs\python\python36\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visu
al Studio 14.0\VC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64
" /EXPORT:PyInit_temp C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents\code\benchmark\temp.obj /OUT:C:\Users\name\Documents\code\benc
hmark\temp.cp36-win_amd64.pyd /IMPLIB:C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents\code\benchmark\temp.cp36-win_amd64.lib
temp.obj : warning LNK4197: export 'PyInit_temp' specified multiple times; using first specification
   Creating library C:\Users\name\Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents\code\benchmark\temp.cp36-win_amd64.lib and object C:\Users\name\
Documents\code\benchmark\tmpw0giz82d\Release\Users\name\Documents\code\benchmark\temp.cp36-win_amd64.exp
Generating code
Finished generating code
like image 97
Cees Timmerman Avatar answered Oct 17 '22 07:10

Cees Timmerman