Repository: cython/cython

Issue:
Mystery casts to `char *`
### Describe your issue

We've got a whole load of mystery casts to `(char *)`. For example:

https://github.com/cython/cython/blob/0d0cc9987f1c7e4d757d974be681e0288f0640e9/Cython/Utility/Printing.c#L23

https://github.com/cython/cython/blob/0d0cc9987f1c7e4d757d974be681e0288f0640e9/Cython/Utility/ImportExport.c#L347

(but also loads of other similar places).

I don't really understand these - in most cases the argument type is `const char*` so the cast seems unnecessary, and just serves to generate warnings about casting away a constant. I'd like to remove them, but they've presumably been put there for a reason.


Discussion:
My guess is that these C-API functions had no "const" in their argument declaration at some point and thus required non-const arguments - so we cast away the warning. The declaration might have changed at some point, and now we get a warning for casting to non-const on our side.

That sounds plausible. I can find a commit adding `const` to a bunch of them from 2007: https://github.com/python/cpython/commit/f308132cc29eea9b7fe899deb575d29d607a48f4

I'll have a cleanup of these then.

Resolving patch:
diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py
--- a/Cython/Compiler/Code.py
+++ b/Cython/Compiler/Code.py
@@ -1666,9 +1666,9 @@ def generate_num_constants(self):
             if py_type == 'float':
                 function = 'PyFloat_FromDouble(%s)'
             elif py_type == 'long':
-                function = 'PyLong_FromString((char *)"%s", 0, 0)'
+                function = 'PyLong_FromString("%s", 0, 0)'
             elif Utils.long_literal(value):
-                function = 'PyInt_FromString((char *)"%s", 0, 0)'
+                function = 'PyInt_FromString("%s", 0, 0)'
             elif len(value.lstrip('-')) > 4:
                 function = "PyInt_FromLong(%sL)"
             else:
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -11296,7 +11296,7 @@ def generate_result_code(self, code):
         code.put_gotref(format_temp, py_object_type)
 
         buildvalue_fmt = " __PYX_BUILD_PY_SSIZE_T " * len(shapes)
-        code.putln('%s = Py_BuildValue((char*) "(" %s ")", %s); %s' % (
+        code.putln('%s = Py_BuildValue("(" %s ")", %s); %s' % (
             shapes_temp,
             buildvalue_fmt,
             ", ".join(shapes),
@@ -11304,7 +11304,7 @@ def generate_result_code(self, code):
         ))
         code.put_gotref(shapes_temp, py_object_type)
 
-        code.putln('%s = __pyx_array_new(%s, %s, PyBytes_AS_STRING(%s), (char *) "%s", (char *) %s); %s' % (
+        code.putln('%s = __pyx_array_new(%s, %s, PyBytes_AS_STRING(%s), "%s", (char *) %s); %s' % (
             self.result(),
             shapes_temp, itemsize, format_temp, self.mode, self.operand.result(),
             code.error_goto_if_null(self.result(), self.pos),
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -2712,7 +2712,7 @@ def generate_getset_table(self, env, code):
                 else:
                     doc_code = "0"
                 code.putln(
-                    '{(char *)%s, %s, %s, (char *)%s, 0},' % (
+                    '{%s, %s, %s, %s, 0},' % (
                         entry.name.as_c_string_literal(),
                         entry.getter_cname or "0",
                         entry.setter_cname or "0",
@@ -3616,7 +3616,7 @@ def generate_module_creation_code(self, env, code):
                 Naming.builtins_cname,
                 code.error_goto_if_null(Naming.builtins_cname, self.pos)))
         code.putln(
-            '%s = __Pyx_PyImport_AddModuleRef((const char *) "cython_runtime"); %s' % (
+            '%s = __Pyx_PyImport_AddModuleRef("cython_runtime"); %s' % (
                 Naming.cython_runtime_cname,
                 code.error_goto_if_null(Naming.cython_runtime_cname, self.pos)))
         code.putln(


Test patch: