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

[BUG] invalid pure-python-to-C code generated when function declared in finally block #4651

Closed
eli-schwartz opened this issue Feb 20, 2022 · 0 comments · Fixed by #4652
Closed

Comments

@eli-schwartz
Copy link
Contributor

I have this example file:

$ cat testcase.py
try:
    print('in try')
finally:
    def func():
        print('in func()')
    func()
print('finished')

I have this real life code: https://github.com/mesonbuild/meson/blob/5d64a15be7e66cfa5573924efeff5dc1e3a39b3e/mesonbuild/modules/python.py#L261-L268

I tried to compile it using cython 0.29.27, and it failed. I tried again with cython 3.0.0a10, and it failed there too:

$ cythonize -b testcase.py
Compiling /tmp/testcase.py because it changed.
[1/1] Cythonizing /tmp/testcase.py
/home/eschwartz/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py:346: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /tmp/testcase.py
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'testcase' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto -fPIC -I/usr/include/python3.10 -c /tmp/testcase.c -o build/temp.linux-x86_64-3.10/tmp/testcase.o
/tmp/testcase.c: In function ‘__pyx_pymod_exec_testcase’:
/tmp/testcase.c:2526:43: error: ‘__pyx_mdef_8testcase_1func’ undeclared (first use in this function); did you mean ‘__pyx_mdef_8testcase_3func’?
 2526 |         __pyx_t_3 = __Pyx_CyFunction_New(&__pyx_mdef_8testcase_1func, 0, __pyx_n_s_func, NULL, __pyx_n_s_testcase, __pyx_d, ((PyObject *)__pyx_codeobj__4)); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 4, __pyx_L6_error)
      |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                           __pyx_mdef_8testcase_3func
/tmp/testcase.c:2526:43: note: each undeclared identifier is reported only once for each function it appears in
error: command '/usr/bin/gcc' failed with exit code 1

(It happens with finally, but not with except, and not with try: pass.)

To be honest, I don't understand the purpose of this bizarre looking code in mesonbuild/modules/python.py, but... Python itself apparently accepts it, and and cython does not, even though it accepts similarly odd design in an except block.

da-woods added a commit to da-woods/cython that referenced this issue Feb 20, 2022
Fixes cython#4651.

This creates two copies of the function, one for the exception
cause and one for the non-exception case. It's probably inefficient
but the simplest solution
scoder pushed a commit that referenced this issue Feb 24, 2022
This creates two copies of the function, one for the exception
case and one for the non-exception case. It's probably inefficient
but the simplest solution.

Fixes #4651.
@scoder scoder added this to the 0.29.29 milestone Feb 24, 2022
scoder pushed a commit that referenced this issue Feb 24, 2022
This creates two copies of the function, one for the exception
case and one for the non-exception case. It's probably inefficient
but the simplest solution.

Fixes #4651.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants