summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-02-12 02:47:20 (GMT)
committerGitHub <noreply@github.com>2020-02-12 02:47:20 (GMT)
commit190433d8150bf719fa0ba972dbacf2214942f54e (patch)
tree542d09915c36b8e496c0c66950eb0bf29704e271
parent0f0d2e496205c345b182b6572ee09db23f8f9daf (diff)
downloadcpython-190433d8150bf719fa0ba972dbacf2214942f54e.zip
cpython-190433d8150bf719fa0ba972dbacf2214942f54e.tar.gz
cpython-190433d8150bf719fa0ba972dbacf2214942f54e.tar.bz2
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 (cherry picked from commit e6be9b59a911626d6597fe148c32f0342bd2bd24) Co-authored-by: Andy Lester <andy@petdance.com>
-rw-r--r--Modules/_io/textio.c4
-rw-r--r--Objects/bytes_methods.c16
-rw-r--r--Objects/memoryobject.c8
-rw-r--r--Objects/stringlib/asciilib.h2
-rw-r--r--Objects/stringlib/codecs.h4
-rw-r--r--Objects/stringlib/find_max_char.h2
-rw-r--r--Objects/unicodeobject.c30
-rw-r--r--Python/marshal.c2
-rw-r--r--Python/pyhash.c2
9 files changed, 35 insertions, 35 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 2f4a524..a4bf7cd 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1985,7 +1985,7 @@ find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch)
{
if (kind == PyUnicode_1BYTE_KIND) {
assert(ch < 256);
- return (char *) memchr((void *) s, (char) ch, end - s);
+ return (char *) memchr((const void *) s, (char) ch, end - s);
}
for (;;) {
while (PyUnicode_READ(kind, s, 0) > ch)
@@ -2003,7 +2003,7 @@ _PyIO_find_line_ending(
int translated, int universal, PyObject *readnl,
int kind, const char *start, const char *end, Py_ssize_t *consumed)
{
- Py_ssize_t len = ((char*)end - (char*)start)/kind;
+ Py_ssize_t len = (end - start)/kind;
if (translated) {
/* Newlines are already translated, only search for \n */
diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c
index 7d13184..db030be 100644
--- a/Objects/bytes_methods.c
+++ b/Objects/bytes_methods.c
@@ -12,7 +12,7 @@ PyObject*
_Py_bytes_isspace(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
/* Shortcut for single character strings */
@@ -42,7 +42,7 @@ PyObject*
_Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
/* Shortcut for single character strings */
@@ -72,7 +72,7 @@ PyObject*
_Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
/* Shortcut for single character strings */
@@ -123,7 +123,7 @@ _Py_bytes_isascii(const char *cptr, Py_ssize_t len)
/* 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) {
Py_RETURN_FALSE;
}
@@ -154,7 +154,7 @@ PyObject*
_Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
/* Shortcut for single character strings */
@@ -184,7 +184,7 @@ PyObject*
_Py_bytes_islower(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
int cased;
@@ -218,7 +218,7 @@ PyObject*
_Py_bytes_isupper(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
int cased;
@@ -254,7 +254,7 @@ PyObject*
_Py_bytes_istitle(const char *cptr, Py_ssize_t len)
{
const unsigned char *p
- = (unsigned char *) cptr;
+ = (const unsigned char *) cptr;
const unsigned char *e;
int cased, previous_is_cased;
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index a873ac1..9205079 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -1681,8 +1681,8 @@ unpack_single(const char *ptr, const char *fmt)
switch (fmt[0]) {
/* signed integers and fast path for 'B' */
- case 'B': uc = *((unsigned char *)ptr); goto convert_uc;
- case 'b': ld = *((signed char *)ptr); goto convert_ld;
+ case 'B': uc = *((const unsigned char *)ptr); goto convert_uc;
+ case 'b': ld = *((const signed char *)ptr); goto convert_ld;
case 'h': UNPACK_SINGLE(ld, ptr, short); goto convert_ld;
case 'i': UNPACK_SINGLE(ld, ptr, int); goto convert_ld;
case 'l': UNPACK_SINGLE(ld, ptr, long); goto convert_ld;
@@ -2683,8 +2683,8 @@ unpack_cmp(const char *p, const char *q, char fmt,
switch (fmt) {
/* signed integers and fast path for 'B' */
- case 'B': return *((unsigned char *)p) == *((unsigned char *)q);
- case 'b': return *((signed char *)p) == *((signed char *)q);
+ case 'B': return *((const unsigned char *)p) == *((const unsigned char *)q);
+ case 'b': return *((const signed char *)p) == *((const signed char *)q);
case 'h': CMP_SINGLE(p, q, short); return equal;
case 'i': CMP_SINGLE(p, q, int); return equal;
case 'l': CMP_SINGLE(p, q, long); return equal;
diff --git a/Objects/stringlib/asciilib.h b/Objects/stringlib/asciilib.h
index d0fc18d..8f83614 100644
--- a/Objects/stringlib/asciilib.h
+++ b/Objects/stringlib/asciilib.h
@@ -18,7 +18,7 @@
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
#define STRINGLIB_STR PyUnicode_1BYTE_DATA
#define STRINGLIB_LEN PyUnicode_GET_LENGTH
-#define STRINGLIB_NEW(STR,LEN) _PyUnicode_FromASCII((char*)(STR),(LEN))
+#define STRINGLIB_NEW(STR,LEN) _PyUnicode_FromASCII((const char*)(STR),(LEN))
#define STRINGLIB_CHECK PyUnicode_Check
#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact
diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h
index d6f2b98..269a558 100644
--- a/Objects/stringlib/codecs.h
+++ b/Objects/stringlib/codecs.h
@@ -46,7 +46,7 @@ STRINGLIB(utf8_decode)(const char **inptr, const char *end,
/* Read a whole long at a time (either 4 or 8 bytes),
and do a fast unrolled copy if it only contains ASCII
characters. */
- unsigned long value = *(unsigned long *) _s;
+ unsigned long value = *(const unsigned long *) _s;
if (value & ASCII_CHAR_MASK)
break;
#if PY_LITTLE_ENDIAN
@@ -515,7 +515,7 @@ STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
/* Fast path for runs of in-range non-surrogate chars. */
const unsigned char *_q = q;
while (_q < aligned_end) {
- unsigned long block = * (unsigned long *) _q;
+ unsigned long block = * (const unsigned long *) _q;
if (native_ordering) {
/* Can use buffer directly */
if (block & FAST_CHAR_MASK)
diff --git a/Objects/stringlib/find_max_char.h b/Objects/stringlib/find_max_char.h
index 8ccbc30..f4e0a77 100644
--- a/Objects/stringlib/find_max_char.h
+++ b/Objects/stringlib/find_max_char.h
@@ -28,7 +28,7 @@ STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end)
/* Help register allocation */
const unsigned char *_p = p;
while (_p < aligned_end) {
- unsigned long value = *(unsigned long *) _p;
+ unsigned long value = *(const unsigned long *) _p;
if (value & UCS1_ASCII_CHAR_MASK)
return 255;
_p += SIZEOF_LONG;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 0556eff..8ba379e 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -171,8 +171,8 @@ extern "C" {
#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); \
@@ -919,21 +919,21 @@ findchar(const void *s, int kind,
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();
}
@@ -3353,7 +3353,7 @@ PyUnicode_Decode(const char *s,
/* 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)
@@ -4861,7 +4861,7 @@ ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
/* 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;
@@ -5406,7 +5406,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
- q = (unsigned char *)s;
+ q = (const unsigned char *)s;
e = q + size;
if (byteorder)
@@ -5731,7 +5731,7 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
PyObject *exc = NULL;
const char *encoding;
- q = (unsigned char *)s;
+ q = (const unsigned char *)s;
e = q + size;
if (byteorder)
@@ -6660,7 +6660,7 @@ PyUnicode_DecodeLatin1(const char *s,
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 */
@@ -13708,7 +13708,7 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
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;
@@ -13767,7 +13767,7 @@ _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
{
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);
diff --git a/Python/marshal.c b/Python/marshal.c
index c6a06e8..d3fee32 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -734,7 +734,7 @@ r_byte(RFILE *p)
else {
const char *ptr = r_string(1, p);
if (ptr != NULL)
- c = *(unsigned char *) ptr;
+ c = *(const unsigned char *) ptr;
}
return c;
}
diff --git a/Python/pyhash.c b/Python/pyhash.c
index 4c0b929..ba224ee 100644
--- a/Python/pyhash.c
+++ b/Python/pyhash.c
@@ -366,7 +366,7 @@ static PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * SIZEOF_PY_HASH_T,
static uint64_t
siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
uint64_t b = (uint64_t)src_sz << 56;
- const uint8_t *in = (uint8_t*)src;
+ const uint8_t *in = (const uint8_t*)src;
uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;