Qiang Zhang
Qiang Zhang

Reputation: 952

cython report bug: expected an identifier

I am using cython to convert py file to pyd file.

My test code is:

# funcA.py
class Window:

    def exec(self):
        pass
    pass

And the setup.py file is:

#setup.py
from setuptools import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
    Extension('funcA', ['funcA.py']),
]

setup(
    name='App',
    ext_modules=ext_modules,
    include_dirs=["App"],
    cmdclass={'build_ext': build_ext},
)

After python setup.py build_ext --inplace, a bug is reported:

Error compiling Cython file:
------------------------------------------------------------
...

class Window:

    def exec(self):
       ^
------------------------------------------------------------

funcA.py:4:8: Expected an identifier
building 'funcA' extension
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IApp -ID:\Anaconda3\envs\work\include -ID:\Anaconda3\envs\work\Includ
e "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include" "-IC:\Prog
ram Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um" "-IC:\Program Files (x
86)\Windows Kits\10\include\10.0.19041.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt" /TcfuncA.c /Fobuild\temp.win-amd64-cpython-39\Release\funcA.obj
funcA.c
funcA.c(1): fatal error C1189: #error:  Do not use this file, it is the result of a failed Cython compilation.
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2


But, if I replace the funcA.py to:

#new funcA.py
class Window:

    pass

Everything is OK.

Why exec function would couse this bug?

Any suggestion is appreciated~~~

------------- update --------------------------------

Thanks @DavidW's suggestion.

The solution is adding #cython: language_level=3 on the top of script.

Upvotes: 0

Views: 713

Answers (1)

DavidW
DavidW

Reputation: 30911

Depending on what Cython version you're using (you don't say...) Cython defaults to Python 2 behaviour. In Python 2 exec was a keyword, so cannot be used as a function name.

Either:

  • pick a different function name,
  • put Cython in Python 3-like mode by setting language_level to 3. Bear in mind this may change some other things (e.g. print, the scope of list comprehensions, ...)
  • Use Cython 3 alpha version where it does default to Python 3 semantics by default.

Upvotes: 1

Related Questions