Skip to content

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

Closed
@leofang

Description

@leofang

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)
    print(name)

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
None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions