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

Redefinition error when putting functions in a dict #3716

Closed
Lothiraldan opened this issue Jun 30, 2020 · 2 comments · Fixed by #3743
Closed

Redefinition error when putting functions in a dict #3716

Lothiraldan opened this issue Jun 30, 2020 · 2 comments · Fixed by #3743

Comments

@Lothiraldan
Copy link

When trying to compile some Python code with Cython, I got the following kind of errors:

error: redefinition of XXX

I've managed to reduce my code to a reproducible example:

# cython: language_level=3

cdef f1(x, r):
    pass

cdef f2(x1, r):
    pass

cdef map = {
    "f1": f1,
    "f2": f2,
}

When trying to compile it with Cython version 0.29.20, I get the following output:

Compiling /home/lothiraldan/cython-bug/test2.pyx because it changed.
[1/1] Cythonizing /home/lothiraldan/cython-bug/test2.pyx
building 'test2' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/lothiraldan/.pyenv/versions/3.7.5/include/python3.7m -c /home/lothiraldan/cython-bug/test2.c -o build/temp.linux-x86_64-3.7/home/lothiraldan/cython-bug/test2.o
/home/lothiraldan/cython-bug/test2.c:1406:13: error: redefinition of ‘__pyx_doc_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap’
 1406 | static char __pyx_doc_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap[] = "wrap(x1, r)";
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1213:13: note: previous definition of ‘__pyx_doc_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap’ was here
 1213 | static char __pyx_doc_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap[] = "wrap(x, r)";
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1407:20: error: redefinition of ‘__pyx_mdef_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap’
 1407 | static PyMethodDef __pyx_mdef_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap = {"wrap", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap, METH_VARARGS|METH_KEYWORDS, __pyx_doc_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap};
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1214:20: note: previous definition of ‘__pyx_mdef_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap’ was here
 1214 | static PyMethodDef __pyx_mdef_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap = {"wrap", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap, METH_VARARGS|METH_KEYWORDS, __pyx_doc_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap};
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1408:18: error: redefinition of ‘__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap’
 1408 | static PyObject *__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1215:18: note: previous definition of ‘__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap’ was here
 1215 | static PyObject *__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1470:18: error: redefinition of ‘__pyx_pf_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap’
 1470 | static PyObject *__pyx_pf_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap(PyObject *__pyx_self, PyObject *__pyx_v_x1, PyObject *__pyx_v_r) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1277:18: note: previous definition of ‘__pyx_pf_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap’ was here
 1277 | static PyObject *__pyx_pf_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap(PyObject *__pyx_self, PyObject *__pyx_v_x, PyObject *__pyx_v_r) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1524:18: error: redefinition of ‘__Pyx_CFunc_object____object____object___to_py’
 1524 | static PyObject *__Pyx_CFunc_object____object____object___to_py(PyObject *(*__pyx_v_f)(PyObject *, PyObject *)) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1331:18: note: previous definition of ‘__Pyx_CFunc_object____object____object___to_py’ was here
 1331 | static PyObject *__Pyx_CFunc_object____object____object___to_py(PyObject *(*__pyx_v_f)(PyObject *, PyObject *)) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1470:18: warning: ‘__pyx_pf_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap’ defined but not used [-Wunused-function]
 1470 | static PyObject *__pyx_pf_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_wrap(PyObject *__pyx_self, PyObject *__pyx_v_x1, PyObject *__pyx_v_r) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1408:18: warning: ‘__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap’ defined but not used [-Wunused-function]
 1408 | static PyObject *__pyx_pw_11cfunc_dot_to_py_46__Pyx_CFunc_object____object____object___to_py_1wrap(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/lothiraldan/cython-bug/test2.c:1331:18: warning: ‘__Pyx_CFunc_object____object____object___to_py’ defined but not used [-Wunused-function]
 1331 | static PyObject *__Pyx_CFunc_object____object____object___to_py(PyObject *(*__pyx_v_f)(PyObject *, PyObject *)) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: command 'gcc' failed with exit status 1

I only get the issue when I the functions have a the same of parameters but different parameters names. If I add type to some parameter of one function, I also don't get an error.

I used the workaround for now, so it's not an urgent issue for me.

@da-woods
Copy link
Contributor

It probably give an error but it seems like a relatively pointless thing to do: you're not putting a bunch of cdef functions in a dict - you're putting a bunch of Python wrappers round cdef functions into a dict. It's pretty much equivalent to if you'd just declared the functions as def/cpdef

@da-woods
Copy link
Contributor

da-woods commented Jun 30, 2020

It doesn't look hugely easy to fix. The utility code is generated

env.use_utility_code(CythonUtilityCode.load(
. Earlier, it tries to reuse self.to_py_function of the function type, however each cdef function has its own instance of the type.


An alternative way of fixing it is

prefix = env.next_id(env.scope_prefix)
the DefNodeWrapper tries to get a unique prefix. However, it only gets a prefix from the utility code module, not the overall module.

scoder pushed a commit that referenced this issue Mar 27, 2021
* Avoid UtilityCode from cimport_from_pyx files
* Unique names for generated cfunc wrappers, avoid pxd pickle code
* Set .in_pxd on nodes prior to analyse_declarations

Fixes #3716
Fixes #3741
Fixes #3734
@scoder scoder added this to the 3.0 milestone Mar 30, 2021
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