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] error: invalid conversion from 'const char*' to 'char*' #3964

leofang opened this issue Jan 2, 2021 · 4 comments

[BUG] error: invalid conversion from 'const char*' to 'char*' #3964

leofang opened this issue Jan 2, 2021 · 4 comments


Copy link

leofang commented Jan 2, 2021

Describe the bug
By inspecting the generated .c/.cpp file, I found an intermediate variable of type char * is created to hold the return value of type const char *, causing compiler warnings (or errors with stricter compiler warning checks like -Werror).

To Reproduce
Code to reproduce the behaviour:

cimport cpython

def get_name(object capsule):
    cdef const char* name = cpython.PyCapsule_GetName(capsule)

With a simple cythonize a warning is raised:

$ cythonize -i -3 test_cython_bug.pyx
/home/leofang/dev/cupy_cuda10.2/test_cython_bug.c:1242:13: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   __pyx_t_1 = PyCapsule_GetName(__pyx_v_capsule); if (unlikely(__pyx_t_1 == ((char *)NULL) && PyErr_Occurred())) __PYX_ERR(0, 4, __pyx_L1_error)

and the reason is __pyx_t_1 is declared as char *, not const char *, so in a production environment with more sophisticated build system, it's turned to an error:

    cupy/core/dlpack.cpp:4298:32: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
       __pyx_t_4 = PyCapsule_GetName(__pyx_v_dltensor); if (unlikely(__pyx_t_4 == ((char *)NULL) && PyErr_Occurred())) __PYX_ERR(0, 179, __pyx_L1_error)
    error: command 'gcc' failed with exit status 1

making any functions returning const char * not usable in Cython.

Expected behavior
It should just work by respecting the constness if declared in a cdef variable. Currently I don't even know how to work around it after scratching my head for a long time, which is rare to me...

Environment (please complete the following information):

  • OS: Linux
  • Python version 3.7.8
  • Cython version 0.29.21

Additional context

Copy link

da-woods commented Jan 2, 2021

I suspect this has been very recently fixed in #3940 and it probably just needs backporting. I'll leave this open as a reminder to backport that patch to 0.29.x

@da-woods da-woods added this to the 0.29.22 milestone Jan 2, 2021
Copy link

leofang commented Jan 3, 2021

Thank you, @da-woods! This patch came right in time. I'm glad it's just a simple error in the function signatures...

Copy link

scoder commented Feb 18, 2021

I cherry-picked it over, will push 591044b soon.

Copy link

leofang commented Feb 18, 2021

Thanks, @scoder!

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

No branches or pull requests

3 participants