closes bpo-39605: Fix some casts to not cast away const. (GH-18453)
gcc -Wcast-qual turns up a number of instances of casting away constness of pointers. Some of these can be safely modified, by either:
Adding the const to the type cast, as in:
- return _PyUnicode_FromUCS1((unsigned char*)s, size);
+ return _PyUnicode_FromUCS1((const unsigned char*)s, size);
or, Removing the cast entirely, because it's not necessary (but probably was at one time), as in:
- PyDTrace_FUNCTION_ENTRY((char *)filename, (char *)funcname, lineno);
+ PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
These changes will not change code, but they will make it much easier to check for errors in consts
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 68e4f6a..fdc2ca6 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -172,8 +172,8 @@
#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \
do { \
to_type *_to = (to_type *)(to); \
- const from_type *_iter = (from_type *)(begin); \
- const from_type *_end = (from_type *)(end); \
+ const from_type *_iter = (const from_type *)(begin);\
+ const from_type *_end = (const from_type *)(end);\
Py_ssize_t n = (_end) - (_iter); \
const from_type *_unrolled_end = \
_iter + _Py_SIZE_ROUND_DOWN(n, 4); \
@@ -964,21 +964,21 @@
if ((Py_UCS1) ch != ch)
return -1;
if (direction > 0)
- return ucs1lib_find_char((Py_UCS1 *) s, size, (Py_UCS1) ch);
+ return ucs1lib_find_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
else
- return ucs1lib_rfind_char((Py_UCS1 *) s, size, (Py_UCS1) ch);
+ return ucs1lib_rfind_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
case PyUnicode_2BYTE_KIND:
if ((Py_UCS2) ch != ch)
return -1;
if (direction > 0)
- return ucs2lib_find_char((Py_UCS2 *) s, size, (Py_UCS2) ch);
+ return ucs2lib_find_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
else
- return ucs2lib_rfind_char((Py_UCS2 *) s, size, (Py_UCS2) ch);
+ return ucs2lib_rfind_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
case PyUnicode_4BYTE_KIND:
if (direction > 0)
- return ucs4lib_find_char((Py_UCS4 *) s, size, ch);
+ return ucs4lib_find_char((const Py_UCS4 *) s, size, ch);
else
- return ucs4lib_rfind_char((Py_UCS4 *) s, size, ch);
+ return ucs4lib_rfind_char((const Py_UCS4 *) s, size, ch);
default:
Py_UNREACHABLE();
}
@@ -3420,7 +3420,7 @@
/* Decode via the codec registry */
buffer = NULL;
- if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0)
+ if (PyBuffer_FillInfo(&info, NULL, (const void *)s, size, 1, PyBUF_FULL_RO) < 0)
goto onError;
buffer = PyMemoryView_FromBuffer(&info);
if (buffer == NULL)
@@ -4921,7 +4921,7 @@
/* Help allocation */
const char *_p = p;
while (_p < aligned_end) {
- unsigned long value = *(unsigned long *) _p;
+ unsigned long value = *(const unsigned long *) _p;
if (value & ASCII_CHAR_MASK)
break;
_p += SIZEOF_LONG;
@@ -5472,7 +5472,7 @@
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
- q = (unsigned char *)s;
+ q = (const unsigned char *)s;
e = q + size;
if (byteorder)
@@ -5797,7 +5797,7 @@
PyObject *exc = NULL;
const char *encoding;
- q = (unsigned char *)s;
+ q = (const unsigned char *)s;
e = q + size;
if (byteorder)
@@ -6726,7 +6726,7 @@
const char *errors)
{
/* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
- return _PyUnicode_FromUCS1((unsigned char*)s, size);
+ return _PyUnicode_FromUCS1((const unsigned char*)s, size);
}
/* create or adjust a UnicodeEncodeError */
@@ -13803,7 +13803,7 @@
if (len == -1)
len = strlen(ascii);
- assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128);
+ assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128);
if (writer->buffer == NULL && !writer->overallocate) {
PyObject *str;
@@ -13862,7 +13862,7 @@
{
Py_UCS4 maxchar;
- maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len);
+ maxchar = ucs1lib_find_max_char((const Py_UCS1*)str, (const Py_UCS1*)str + len);
if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1)
return -1;
unicode_write_cstr(writer->buffer, writer->pos, str, len);